class TweetBot::Bot
Constants
- DefaultFrequency
- Noop
Attributes
response_frequency[RW]
twitter_auth[RW]
Public Class Methods
new()
click to toggle source
# File lib/tweetbot/bot.rb, line 10 def initialize() self.response_frequency = DefaultFrequency @responses_for_phrases = Hash.new { |hash, key| hash[key] = [] } @blocks_for_phrases = {} end
Public Instance Methods
add_responses_for_phrase(phrase, *responses)
click to toggle source
# File lib/tweetbot/bot.rb, line 38 def add_responses_for_phrase(phrase, *responses) @responses_for_phrases[phrase.downcase] += responses end
alert_status_captured(status)
click to toggle source
# File lib/tweetbot/bot.rb, line 20 def alert_status_captured(status) find_phrase_value(@blocks_for_phrases, status.text, Noop).call(status) end
find_phrase_value(hash, text, default)
click to toggle source
# File lib/tweetbot/bot.rb, line 82 def find_phrase_value(hash, text, default) hash.select { |phrase, _| text =~ /#{phrase.downcase}/i }.values[0] || default end
on_status_captured(key, &block)
click to toggle source
# File lib/tweetbot/bot.rb, line 16 def on_status_captured(key, &block) @blocks_for_phrases[key] = block end
phrases_to_search()
click to toggle source
# File lib/tweetbot/bot.rb, line 24 def phrases_to_search @responses_for_phrases.keys + @blocks_for_phrases.keys end
rate_limited!()
click to toggle source
# File lib/tweetbot/bot.rb, line 65 def rate_limited! puts "Starting rate limit throttling!" @rate_limited_until = Time.now + 3600 end
respond_to_phrase(phrase) { |responses| ... }
click to toggle source
# File lib/tweetbot/bot.rb, line 32 def respond_to_phrase(phrase) responses = [] yield responses add_responses_for_phrase(phrase, *responses) end
response_for(tweet)
click to toggle source
# File lib/tweetbot/bot.rb, line 42 def response_for(tweet) responses = responses_for_tweet(tweet) "@#{tweet.user.screen_name} #{responses.sample}" end
responses_for(phrase)
click to toggle source
# File lib/tweetbot/bot.rb, line 28 def responses_for(phrase) @responses_for_phrases[phrase] end
responses_for_tweet(tweet)
click to toggle source
# File lib/tweetbot/bot.rb, line 78 def responses_for_tweet(tweet) find_phrase_value(@responses_for_phrases, tweet.text, []) end
should_i_respond_to?(tweet)
click to toggle source
# File lib/tweetbot/bot.rb, line 47 def should_i_respond_to?(tweet) if under_rate_limit_pause? puts "Under rate limit pause. Will let up at #{@rate_limited_until.to_s}" return false end matches = tweet_matches?(tweet) unless matches puts "Tweet does not match" return false end frequency_check = (rand(100) < self.response_frequency) unless frequency_check puts "Frequency check failed" return false end true end
tweet_matches?(tweet)
click to toggle source
# File lib/tweetbot/bot.rb, line 74 def tweet_matches?(tweet) responses_for_tweet(tweet).any? end
under_rate_limit_pause?()
click to toggle source
# File lib/tweetbot/bot.rb, line 70 def under_rate_limit_pause? @rate_limited_until && (Time.now < @rate_limited_until) end