Class: Messed::Interface::Adapter::TwitterStreaming

Inherits:
TwitterConsumer show all
Includes:
Logger::LoggingModule
Defined in:
lib/messed/interface/adapter/twitter_streaming.rb

Constant Summary

Constants inherited from Messed::Interface::Adapter

Registry

Instance Attribute Summary

Attributes inherited from TwitterConsumer

#errors, #last_error, #last_ok, #last_status, #packets_processed, #started_at

Attributes inherited from Messed::Interface::Adapter

#interface

Instance Method Summary collapse

Methods included from Logger::LoggingModule

included, #logger, #logger=

Methods inherited from TwitterConsumer

#init, #status, #type

Methods inherited from Messed::Interface::Adapter

for_name, #init, #initialize, #load_state, register_for_name, #save_state, #send, #state_file, #type

Constructor Details

This class inherits a constructor from Messed::Interface::Adapter

Instance Method Details

#startObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/messed/interface/adapter/twitter_streaming.rb', line 10

def start
  stream = Twitter::JSONStream.connect(
    :path    => "/1/statuses/#{interface.configuration.options['streaming']['type']}.json?track=%23classicmoviequotes",
    :auth    => "#{interface.configuration.options['username']}:#{interface.configuration.options['password']}"
  )
  
  stream.each_item do |item|
    self.last_ok = Time.new
    result = JSON.parse(item)
    message = Message::Twitter.new do |m|
      m.body = result['text']
      m.from = result['from_user']
      m.to = result['to_user_id']
      m.created_at = Time.rfc2822(result['created_at'])
      m.profile_image_url = result['profile_image_url']
      m.id = result['id']
      m.geo = result['geo']
      m.from_user_id = result['from_user_id']
      m.iso_language_code = result['iso_language_code']
      m.source = result['source']
    end
    self.packets_processed += 1
    interface.application.incoming << message
  end

  stream.on_error do |message|
    logger.error "message #{message.inspect}"
    self.errors += 1
    self.last_error = message
  end

  stream.on_max_reconnects do |timeout, retries|
    logger.error "message -> #{timeout} #{retries}"
    # Something is wrong on your side. Send yourself an email.
  end
end