Class: TwitterTopicBot::ApiClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/twitter_topic_bot/api_client.rb

Constant Summary collapse

MAX_TWEET_LENGTH =
140

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ ApiClient

Returns a new instance of ApiClient.



15
16
17
18
19
# 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

Instance Method Details

#follow(*users) ⇒ Object



35
36
37
# File 'lib/twitter_topic_bot/api_client.rb', line 35

def follow(*users)
  twitter_client.follow(*users.map(&:id))
end

#max_tweet_lengthObject



21
22
23
# File 'lib/twitter_topic_bot/api_client.rb', line 21

def max_tweet_length
  MAX_TWEET_LENGTH
end

#mentionsObject



48
49
50
51
52
53
# File 'lib/twitter_topic_bot/api_client.rb', line 48

def mentions
  filter_tweets(
    twitter_client.mentions_timeline,
    allow_links: false
  )
end

#repliesObject



62
63
64
65
66
# 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) ⇒ Object



29
30
31
32
33
# 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) ⇒ Object



39
40
41
42
43
44
45
46
# 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 = {}) ⇒ Object



25
26
27
# File 'lib/twitter_topic_bot/api_client.rb', line 25

def tweet(str, options = {})
  twitter_client.update(str, options)
end

#tweetsObject



55
56
57
58
59
60
# File 'lib/twitter_topic_bot/api_client.rb', line 55

def tweets
  twitter_client.user_timeline(
    username,
    count: max_num_tweets_per_query
  )
end

#usernameObject



68
69
70
# File 'lib/twitter_topic_bot/api_client.rb', line 68

def username
  credentials['username']
end