Created
February 16, 2015 03:06
-
-
Save leadhkr/9f7f604c3df8d9c96021 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setup.rb makes sure we're setting up our environment correctly, i.e., | |
# requiring the necessary gems, connecting to the correct database, etc. | |
require './setup' | |
# database.rb is where we're defining our DataMapper models | |
require './database' | |
# DATA SCHEME | |
# user [name, location, join date, retweets, followers_count] | |
# posts [statuses, user_id] | |
class User | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String, required: true | |
property :screen_name, String, required: true | |
property :location, String, required: true | |
property :created_at, String, required: true | |
property :followers_count, Integer, required: true | |
has n, :posts | |
end | |
class Post | |
include DataMapper::Resource | |
property :id, Serial | |
property :statuses, String, required: true | |
belongs_to :user | |
end | |
DataMapper.finalize | |
DataMapper.auto_upgrade! | |
def twitter_client | |
return @client unless @client.nil? | |
@client = Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] | |
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] | |
config.access_token = ENV['TWITTER_ACCESS_TOKEN'] | |
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET'] | |
end | |
end | |
def get_twitter_user(screen_name) | |
twitter_client.user(screen_name.sub(/^@/, "")) | |
end | |
# HOME PAGE - ENTER USER NAME | |
get '/' do | |
@page_id = "background" | |
erb :home | |
end | |
# CREATE USER NAME | |
post '/' do | |
user = get_twitter_user(params[:screen_name]) | |
posts = [] | |
twitter_client.user_timeline(params[:screen_name], :count => 100).each do |tweet| | |
posts << tweet.text | |
end | |
posts = posts.join | |
User.create( | |
:name => user.name, | |
:screen_name => user.screen_name, | |
:location => user.location, | |
:created_at => user.created_at, | |
:followers_count => user.followers_count | |
) | |
Post.create(:statuses => posts) | |
redirect '/' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment