Class: TwitterAdapter

Inherits:
Adapter show all
Includes:
Logging
Defined in:
lib/business/adapter/twitter_adapter.rb

Overview

Self implementation from Adapter class to make it works with Twitter Streaming API

Author:

  • Daniel Machado Fernandez

Version:

  • 1.0

Constant Summary

Constants included from Logging

Logging::KermitPFC

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initializeTwitterAdapter

Configures the dao with the apropiates params



17
18
19
20
21
22
# File 'lib/business/adapter/twitter_adapter.rb', line 17

def initialize

	logger.info('Starting TwitterAdapter...')
	@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

Parameters:

  • stream (Integer) (defaults to: 1)

    the number of the stream to identify it (use it when you have got more than one)



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 27

def connect_stream (stream=1)

	logger.debug('retrieving...')
	puts 'retrieving... '
	track = Settings.twitter.track.send("track#{stream}")

	EventMachine::run {

		stream = Twitter::JSONStream.connect(
		   	:path    => "/1/statuses/filter.json?track=#{track}",
	    	:auth    => "#{Settings.twitter.}:#{Settings.twitter.pass}",
	    	:ssl     => true,
	    	:port    => Settings.twitter.port
	  	)

	  	stream.each_item do |status|

	  		persist status
			
	  	end
	  
		stream.on_error do |message|
		    logger.error("#{message}")
		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

Parameters:

  • status (String)

    tweet retrieved previously



69
70
71
72
73
# File 'lib/business/adapter/twitter_adapter.rb', line 69

def persist status
	
	@dao.save_status status

end