Class: TwitterAdapter
- Includes:
- Logging
- Defined in:
- lib/business/adapter/twitter_adapter.rb
Overview
Self implementation from Adapter class to make it works with Twitter Streaming API
Constant Summary
Constants included from Logging
Instance Method Summary collapse
-
#connect_stream(stream = 1) ⇒ Object
Connects to the Twitter Streaming API and retrieves statuses with a track word previously defined.
-
#initialize ⇒ TwitterAdapter
constructor
Configures the dao with the apropiates params.
-
#persist(status) ⇒ Object
Saves the tweet into the database.
Methods included from Logging
Constructor Details
#initialize ⇒ TwitterAdapter
Configures the dao with the apropiates params
17 18 19 20 21 |
# File 'lib/business/adapter/twitter_adapter.rb', line 17 def initialize @dao = DAO.new 'twitter' end |
Instance Method Details
#connect_stream(stream = 1) ⇒ Object
Connects to the Twitter Streaming API and retrieves statuses with a track word previously defined
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/business/adapter/twitter_adapter.rb', line 26 def connect_stream (stream=1) puts 'retrieving... ' track = Settings.twitter.track.send("track#{stream}") logger.info("Starting Twitter adapter ##{track}") EventMachine::run { stream = Twitter::JSONStream.connect( :path => "/1/statuses/filter.json?track=#{track}", :auth => "#{Settings.twitter.login}:#{Settings.twitter.pass}", :ssl => true, :port => Settings.twitter.port ) stream.each_item do |status| persist status end stream.on_error do || logger.error("#{}") end stream.on_reconnect do |timeout, retries| logger.warn("reconnecting in: #{timeout} seconds\n") end stream.on_max_reconnects do |timeout, retries| logger.fatal("Failed after #{retries} failed reconnects\n") end trap('TERM') { stream.stop } } end |
#persist(status) ⇒ Object
Saves the tweet into the database
69 70 71 72 73 |
# File 'lib/business/adapter/twitter_adapter.rb', line 69 def persist status @dao.save_status status end |