Class: Tweetable::Search

Inherits:
Persistable show all
Defined in:
lib/tweetable/search.rb

Constant Summary collapse

SEARCH_PER_PAGE_LIMIT =
100
SEARCH_START_PAGE =
1

Instance Method Summary collapse

Methods inherited from Persistable

#client, #config, find_or_create, #needs_update?

Instance Method Details

#update_all(force = false) ⇒ Object



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

def update_all(force = false)
  return unless needs_update?(force)
  self.updated_at = Time.now.utc.to_s
  self.save
  update_messages
end

#update_messages(pages = 15) ⇒ Object

Perform the search and update messages if any new exist Do up to 15 requests to collect as many historical messages as possible Because search API is different than the resto f the Twitter API this needs to be custom (i.e. cannot use xxx_from_timeline methods)



22
23
24
25
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
# File 'lib/tweetable/search.rb', line 22

def update_messages(pages = 15)
  most_recent_message = self.messages.first(:order => 'DESC', :by => :message_id) 
  since_id = most_recent_message.nil? ? 0 : most_recent_message.message_id

  search_messages = []
  pages.times do |page|
    s = search(self.query, since_id, SEARCH_PER_PAGE_LIMIT, page+1)
    break if s.results.nil?
    search_messages += s.results
    break if s.results.size < 99                      
  end
  
  search_messages.each do |message|        
    m = Message.find_or_create(:message_id, message.id)
    
    m.update(
    :message_id => message.id, 
    :favorited => message.favorited, 
    :photos_parsed => '0',
    :links_parsed => '0',          
    :created_at => Time.now.utc.to_s, 
    :sent_at => message.created_at,
    :text => message.text, 
    :from_screen_name => message.from_user) # we explicitly don't include the user_id provided by search since it's bullshit: http://code.google.com/p/twitter-api/issues/detail?id=214
      
    next if !m.valid?
      
    # create the user for this message
    u = User.find_or_create(:screen_name, message.from_user)
    u.update(
      :user_id => message.from_user_id,
      :profile_image_url => message.profile_image_url)

    self.messages << m unless self.messages.include?(m)
  end
  
  search_messages.flatten
end