class Ruboty::Handlers::Tweetstream
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/ruboty/handlers/tweetstream.rb, line 24 def initialize(*args) super restart end
Public Instance Methods
add(message)
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 29 def add(message) id = generate_id username = message[:username] channel = message.channel if stream_exists?(username, channel) message.reply("'@#{username}' stream is already started.") else stream = Ruboty::Tweetstream::Stream.new( message.original.except(:robot).merge( username: username, twitter_id: twitter_id(username), channel: channel ) ) stream.start(robot) running_streams[id] = stream streams[id] = stream.hash message.reply("'@#{username}' stream is started.") end rescue Twitter::Error::Forbidden message.reply('Unable to verify your credentials.') end
delete(message)
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 55 def delete(message) id = message[:id].to_i if streams.has_key?(id) running_streams[id].stop running_streams.delete(id) streams.delete(id) message.reply("'#{id}' stream is stopped.") else message.reply("'#{id}' stream is not found.") end end
generate_id()
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 120 def generate_id loop do id = rand(1000) break id unless streams.has_key?(id) end end
list(message)
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 68 def list(message) if streams.empty? message.reply("The stream doesn't exist.") else sorted_streams = streams.sort_by {|_id, hash| hash[:username]} if message[:all_flag] stream_list = sorted_streams.map do |id, hash| stream = "#{id}: @#{hash[:username]}" stream << " (#{hash[:channel]})" unless hash[:channel].nil? stream end else stream_list = sorted_streams.map { |id, hash| # Skip the channel that registered from other channel. next unless hash[:channel] == message.channel "#{id}: @#{hash[:username]}" }.compact end message.reply(stream_list.join("\n"), code: true) end end
restart()
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 93 def restart streams.each do |id, hash| stream = Ruboty::Tweetstream::Stream.new(hash) stream.start(robot) running_streams[id] = stream end end
running_streams()
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 106 def running_streams @running_streams ||= {} end
stream_exists?(username, channel)
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 127 def stream_exists?(username, channel) streams.each do |_id, hash| return true if hash[:username] == username && hash[:channel] == channel end return false end
streams()
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 102 def streams robot.brain.data[Ruboty::Tweetstream::NAMESPACE] ||= {} end
twitter_id(username)
click to toggle source
# File lib/ruboty/handlers/tweetstream.rb, line 110 def twitter_id(username) client = Twitter::REST::Client.new do |config| config.consumer_key = Ruboty::Tweetstream::CONSUMER_KEY config.consumer_secret = Ruboty::Tweetstream::CONSUMER_SECRET config.access_token = Ruboty::Tweetstream::ACCESS_TOKEN config.access_token_secret = Ruboty::Tweetstream::ACCESS_TOKEN_SECRET end client.user(username).id end