class Ruboty::TwitterTrack::Stream
Public Class Methods
new(robot)
click to toggle source
# File lib/ruboty/twitter_track/stream.rb, line 7 def initialize(robot) TweetStream.configure do |config| config.consumer_key = CONSUMER_KEY config.consumer_secret = CONSUMER_SECRET config.oauth_token = ACCESS_TOKEN config.oauth_token_secret = ACCESS_TOKEN_SECRET config.auth_method = :oauth end @robot = robot @client = TweetStream::Client.new @client.on_inited do log(:info , "Connected to twitter stream.") # Every day reconnect the stream to prevent from shutdown. EM::PeriodicTimer.new(60 * 60 * 24) do @client.stream.immediate_reconnect end end @client.on_reconnect do |timeout, retrial| log(:info , "Reconnected to twitter stream: #{timeout} sec.") end @client.on_error do |message| log(:error, message) end end
Public Instance Methods
restart(message, terms)
click to toggle source
# File lib/ruboty/twitter_track/stream.rb, line 48 def restart(message, terms) return start(message, terms) unless @client.stream query = terms.map { |w| w.join(' ') } @client.stream.update(params: {:track => query.join(',')}) end
start(message, terms)
click to toggle source
# File lib/ruboty/twitter_track/stream.rb, line 37 def start(message, terms) return if terms.empty? Thread.start do query = terms.map { |w| w.join(' ') } @client.track(*query) do |object| Message.new(message.merge(robot: @robot)).reply(u(object)) end end end
Private Instance Methods
log(level, message)
click to toggle source
# File lib/ruboty/twitter_track/stream.rb, line 60 def log(level, message) Ruboty.logger.send(level, "[#{Time.now}] ruboty-twitter_track: #{message}") end
u(object)
click to toggle source
# File lib/ruboty/twitter_track/stream.rb, line 56 def u(object) "https://twitter.com/#{object.user.screen_name}/status/#{object.id}" end