Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created April 3, 2015 23:29
Show Gist options
  • Save garybernhardt/f341739d54586403a4ce to your computer and use it in GitHub Desktop.
Save garybernhardt/f341739d54586403a4ce to your computer and use it in GitHub Desktop.
module TwitterLib
def self.authenticate
libs = [Twitter, TweetStream]
libs.each do |lib|
lib.configure do |config|
config.consumer_key = ENV.fetch("TWITTER_CONSUMER_KEY")
config.consumer_secret = ENV.fetch("TWITTER_CONSUMER_SECRET")
config.oauth_token = ENV.fetch("TWITTER_OAUTH_TOKEN")
config.oauth_token_secret = ENV.fetch("TWITTER_OAUTH_SECRET")
end
end
end
def self.timeline_tweets
Twitter.home_timeline(:count => 200).map do |status|
create_tweet(status)
end
end
def self.create_user(user)
User.new(user.id, user.screen_name)
end
def self.create_tweet(status)
user = create_user(status.user)
Tweet.new(status.id, user, CGI.unescapeHTML(status.text))
end
def self.update(text)
Twitter.update(text)
end
def self.userstream(&block)
TweetStream.userstream do |status|
tweet = create_tweet(status)
block.call(tweet)
end
end
def self.friends
friends = [create_user(Twitter.current_user)]
loop do
response = Twitter.friend_ids
ids = response.collection
friends += Twitter.users(*ids).map { |user| create_user(user) }
break if response.last?
end
Friends.new(friends)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment