Class: Tweetable::TwitterStreamingClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tweetable/twitter_streaming_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, parser = nil) ⇒ TwitterStreamingClient

Returns a new instance of TwitterStreamingClient.



12
13
14
15
16
# File 'lib/tweetable/twitter_streaming_client.rb', line 12

def initialize(username, password, parser = nil)
  @client = TweetStream::Client.new(username, password, parser)
  setup
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



8
9
10
# File 'lib/tweetable/twitter_streaming_client.rb', line 8

def method_missing(sym, *args, &block)
  @client.send sym, *args, &block
end

Instance Method Details

#run(query_params) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tweetable/twitter_streaming_client.rb', line 18

def run(query_params)
  query_params = query_params.call if query_params.kind_of?(Proc)
  keywords = query_params[:track]
  keywords = [keywords] unless keywords.kind_of?(Array)
  
  Tweetable.log.debug("Tracking keywords: #{query_params.inspect}")
  
  self.filter(query_params) do |status|
    keywords.each do |keyword|
      store(status, keyword)
    end
  end
end

#start(query_params = {}, daemon_options = {}) ⇒ Object

:nodoc:



32
33
34
35
36
37
# File 'lib/tweetable/twitter_streaming_client.rb', line 32

def start(query_params = {}, daemon_options = {}) #:nodoc:
  Daemons.run_proc('tweetable', daemon_options) do
    Tweetable.log.debug("Starting...")
    run(query_params)
  end
end

#store(status, keyword) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tweetable/twitter_streaming_client.rb', line 39

def store(status, keyword)
  if status.text =~ /#{keyword}/i
    message = Message.create_from_timeline(status, true)            
    Tweetable.log.debug("[#{keyword}] #{message.sent_at} #{message.text} (#{message.message_id})")
    
    search = Search.find_or_create(:query, keyword.downcase)
    search.update(:updated_at => Time.now.utc.to_s)
    search.messages << message unless search.messages.include?(message)
  end
  
  message
end