class Twords::TwitterClient
Twitter REST API client
Attributes
client[R]
A Twitter::REST::Client that provides a direct interface to the Twitter API
@api public @return [Twitter::REST::Client]
Public Class Methods
new(&block)
click to toggle source
Initializes a new Twords::TwitterClient
object and assigns to the @client instance variable
Twords::TwitterClient.new do |twitter| twitter.consumer_key = "YOUR_CONSUMER_KEY" twitter.consumer_secret = "YOUR_CONSUMER_SECRET" twitter.access_token = "YOUR_ACCESS_TOKEN" twitter.access_token_secret = "YOUR_ACCESS_SECRET" end
@api public for block { |twitter| … } @yield [Twitter::REST::Client] yields the Twitter::REST::Client for configuration @see Twords::Configuration#twitter_client
@see github.com/sferik/twitter#configuration
# File lib/twords/twitter_client.rb, line 30 def initialize(&block) @client = Twitter::REST::Client.new(&block) end
Public Instance Methods
filter_tweets(screen_names)
click to toggle source
Fetches the timelines for an array of screen names and filters them by the configured time range.
@api public @param screen_names [Array<String>] the twitter screen names from which to pull the tweets @return [Array<Twitter::Tweet>]
# File lib/twords/twitter_client.rb, line 40 def filter_tweets(screen_names) full_timeline(screen_names).each_with_object([]) do |tweet, memo| next if tweet.created_at > up_to_time memo << tweet if age_of_tweet_in_days(tweet) <= range end end
Private Instance Methods
age_of_tweet_in_days(tweet)
click to toggle source
@api private
# File lib/twords/twitter_client.rb, line 68 def age_of_tweet_in_days(tweet) (up_to_time - tweet.created_at) / 86_400 end
fetch_older_tweets(user_timeline, screen_name)
click to toggle source
@api private
# File lib/twords/twitter_client.rb, line 83 def fetch_older_tweets(user_timeline, screen_name) return user_timeline if age_of_tweet_in_days(user_timeline.last) > range first_count = user_timeline.count user_timeline += client.user_timeline( screen_name, tweet_mode: 'extended', max_id: user_timeline.last.id - 1, count: 200 ) return user_timeline if user_timeline.count == first_count fetch_older_tweets(user_timeline, screen_name) end
fetch_user_timeline(screen_name)
click to toggle source
@api private
# File lib/twords/twitter_client.rb, line 55 def fetch_user_timeline(screen_name) return [] if screen_name.to_s.empty? user_timeline = client.user_timeline(screen_name, tweet_mode: 'extended', count: 200) return user_timeline if user_timeline.empty? user_timeline = fetch_older_tweets(user_timeline, screen_name) puts "Fetched #{screen_name}'s timeline" user_timeline rescue Twitter::Error::TooManyRequests puts 'Rate limit exceeded, waiting 5 minutes' && sleep(300) fetch_user_timeline(screen_name) end
full_timeline(screen_names)
click to toggle source
@api private
# File lib/twords/twitter_client.rb, line 50 def full_timeline(screen_names) screen_names.map { |screen_name| fetch_user_timeline(screen_name) }.flatten.uniq end
range()
click to toggle source
@api private
# File lib/twords/twitter_client.rb, line 78 def range config.range end
up_to_time()
click to toggle source
@api private
# File lib/twords/twitter_client.rb, line 73 def up_to_time config.up_to_time end