Class: Twroute::PollingSearch

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/twroute/polling_search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, password, post_arg_hash = {}) ⇒ PollingSearch

Returns a new instance of PollingSearch.



11
12
13
14
15
# File 'lib/twroute/polling_search.rb', line 11

def initialize(user, password, post_arg_hash = {})
  @user = user
  @password = password
  @post_arg_hash = post_arg_hash
end

Instance Attribute Details

#since_idObject

Returns the value of attribute since_id.



9
10
11
# File 'lib/twroute/polling_search.rb', line 9

def since_id
  @since_id
end

Instance Method Details

#query_stringObject



21
22
23
24
25
26
# File 'lib/twroute/polling_search.rb', line 21

def query_string
  @post_arg_hash['since_id'] = @since_id if @since_id
  @post_arg_hash.keys.collect do |key|
    "#{key}=#{URI.escape(@post_arg_hash[key].to_s)}"
  end.sort_by { |x| x }.join('&')
end

#send_queryObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/twroute/polling_search.rb', line 36

def send_query
  results = Yajl::HttpStream.get( twitter_search_uri,
                                  :symbolize_keys => true)
  results = results[:results] || []
  results.reverse!
  return if results.empty?      
  @since_id = results.last[:id]
  results.each do |tweet|
    changed
    notify_observers( tweet )        
  end
end

#startObject



28
29
30
31
32
33
34
# File 'lib/twroute/polling_search.rb', line 28

def start
  puts twitter_search_uri.to_s
  loop do
    send_query
    sleep 30
  end
end

#twitter_search_uriObject



17
18
19
# File 'lib/twroute/polling_search.rb', line 17

def twitter_search_uri
  URI.parse("http://#{@user}:#{@password}@search.twitter.com/search.json?#{self.query_string}")     
end