class TwitterTopicBot::ApiClient

Constants

MAX_TWEET_LENGTH

Attributes

credentials[R]
max_num_tweets_per_query[R]
tweet_filterer[R]

Public Class Methods

new(credentials) click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 15
def initialize(credentials)
  @credentials = Hash[credentials.map { |k, v| [k.to_s, v] }]
  @tweet_filterer = TwitterTopicBot::TweetFilterer.new
  @max_num_tweets_per_query = 60
end

Public Instance Methods

follow(*users) click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 35
def follow(*users)
  twitter_client.follow(*users.map(&:id))
end
max_tweet_length() click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 21
def max_tweet_length
  MAX_TWEET_LENGTH
end
mentions() click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 48
def mentions
  filter_tweets(
    twitter_client.mentions_timeline,
    allow_links: false
  )
end
replies() click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 62
def replies
  tweets.select do |tweet|
    tweet.in_reply_to_status_id.to_s.match(/\d+/)
  end
end
retweet(*tweets) click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 29
def retweet(*tweets)
  twitter_client.retweet(*tweets.map(&:id))
rescue Twitter::Error::Forbidden
  false
end
search_recent_tweets(query_str) click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 39
def search_recent_tweets(query_str)
  filter_tweets(
    twitter_client.search(
      query_str,
      result_type: 'recent'
    ).take(max_num_tweets_per_query)
  )
end
tweet(str, options = {}) click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 25
def tweet(str, options = {})
  twitter_client.update(str, options)
end
tweets() click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 55
def tweets
  twitter_client.user_timeline(
    username,
    count: max_num_tweets_per_query
  )
end
username() click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 68
def username
  credentials['username']
end

Private Instance Methods

filter_tweets(tweets, allow_links: true) click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 78
def filter_tweets(tweets, allow_links: true)
  allowed_languages = ['en']
  tweets.select do |tweet|
    allowed_languages.include?(tweet.lang) &&
      tweet_filterer.acceptable_tweet?(tweet.text, allow_links: allow_links)
  end
end
twitter_client() click to toggle source
# File lib/twitter_topic_bot/api_client.rb, line 86
def twitter_client
  @twitter_client ||= Twitter::REST::Client.new do |config|
    config.consumer_key = credentials['consumer_key']
    config.consumer_secret = credentials['consumer_secret']
    config.access_token = credentials['access_token']
    config.access_token_secret = credentials['access_token_secret']
  end
end