class Mihatter::RestWatcher
Attributes
config[R]
Public Class Methods
new(config = {})
click to toggle source
# File lib/mihatter/rest_watcher.rb, line 5 def initialize(config = {}) @config = Mihatter.configuration.dup.merge(config) end
Public Instance Methods
run!() { |tweet| ... }
click to toggle source
# File lib/mihatter/rest_watcher.rb, line 11 def run! raise ArgumentError, '`Mihatter::RestWatcher#run!` require block' unless block_given? begin connect assign_since_id unless @config.since_id loop do search.each do |tweet| yield tweet @config.since_id = tweet.id end sleep @config.wait_time end rescue Twitter::Error::TooManyRequests => e sleep e.rate_limit.reset_in end end
since_id()
click to toggle source
# File lib/mihatter/rest_watcher.rb, line 29 def since_id @config.since_id end
Private Instance Methods
assign_since_id()
click to toggle source
# File lib/mihatter/rest_watcher.rb, line 44 def assign_since_id result = @client.search(@config.keyword, { result_type: 'recent', count: 1, lang: @config.lang, }.delete_if { |_, v| v.nil? }).first @config.since_id = (result ? result.id : 0) end
connect()
click to toggle source
# File lib/mihatter/rest_watcher.rb, line 35 def connect @client = Twitter::REST::Client.new do |config| config.consumer_key = @config.consumer_key config.consumer_secret = @config.consumer_secret config.access_token = @config.access_token config.access_token_secret = @config.access_token_secret end end
search(max_id: nil)
click to toggle source
# File lib/mihatter/rest_watcher.rb, line 53 def search(max_id: nil) results = @client.search(@config.keyword, { result_type: 'recent', count: 100, lang: @config.lang, max_id: max_id, since_id: @config.since_id, }.delete_if {|_, v| v.nil?} ).take(100).sort do |a, b| a.id <=> b.id end results = search(max_id: results.first.id - 1) + results if results.size == 100 results end