class Ruboty::Tweetstream::Stream

Attributes

hash[R]

Public Class Methods

new(hash) click to toggle source
# File lib/ruboty/tweetstream/stream.rb, line 15
def initialize(hash)
  @hash = hash
  @thread = nil

  @client = Twitter::Streaming::Client.new do |config|
    config.consumer_key        = CONSUMER_KEY
    config.consumer_secret     = CONSUMER_SECRET
    config.access_token        = ACCESS_TOKEN
    config.access_token_secret = ACCESS_TOKEN_SECRET
  end
end

Public Instance Methods

interval_reconnect() { || ... } click to toggle source

Twitter::Streaming disconnects over time

# File lib/ruboty/tweetstream/stream.rb, line 40
def interval_reconnect
  parent = Thread.start do
    loop do
      child = Thread.start do
        yield
      end
      sleep 3600 # Disconnect every hour
      child.kill
    end
  end

  parent
end
start(robot) click to toggle source
# File lib/ruboty/tweetstream/stream.rb, line 27
def start(robot)
  @thread = interval_reconnect do
    @client.filter(follow: hash[:twitter_id].to_s) do |object|
      if object.is_a?(Twitter::Tweet) && object.is_pure_user_tweet?(hash[:username])
        Message.new(
          hash.except(:username, :twitter_id).merge(robot: robot)
        ).reply("@#{hash[:username]}> #{object.full_text}")
      end
    end
  end
end
stop() click to toggle source
# File lib/ruboty/tweetstream/stream.rb, line 54
def stop
  @thread.kill
end