Class: Tweetable::Queue

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

Class Method Summary collapse

Class Method Details

.add_to_search_queue(queue_name, queries, &block) ⇒ Object



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

def self.add_to_search_queue(queue_name, queries, &block)
  queue = Tweetable::SearchCollection.find_or_create(:name, queue_name)
  return unless queue.searches.empty?

  queries.each do |query|
    search = Tweetable::Search.find_or_create(:query, query)

    yield search if block_given?

    queue.searches << search.id
  end

  queue
end

.add_to_user_queue(queue_name, screen_names, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tweetable/queue.rb', line 32

def self.add_to_user_queue(queue_name, screen_names, &block)
  queue = Tweetable::UserCollection.find_or_create(:name, queue_name)
  return unless queue.users.empty?

  screen_names.each do |screen_name|
    user = Tweetable::User.find_or_create(:screen_name, screen_name)

    yield user if block_given?        

    queue.users << user.id
  end
end

.clear_search_queue(queue_name) ⇒ Object



3
4
5
6
7
8
# File 'lib/tweetable/queue.rb', line 3

def self.clear_search_queue(queue_name)
  collection = Tweetable::SearchCollection.find(:name, queue_name).first
  return true if collection.nil?
  collection.searches.size.times{|i| collection.searches.pop}
  true
end

.clear_user_queue(queue_name) ⇒ Object



10
11
12
13
14
15
# File 'lib/tweetable/queue.rb', line 10

def self.clear_user_queue(queue_name)
  collection = Tweetable::UserCollection.find(:name, queue_name).first
  return true if collection.nil?
  collection.users.size.times{|i| collection.users.pop}
  true
end

.process_search(search) ⇒ Object



73
74
75
76
77
# File 'lib/tweetable/queue.rb', line 73

def self.process_search(search)
  pull_from_queue_safely do
    return search.update_all(true)          
  end
end

.process_user(user) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tweetable/queue.rb', line 79

def self.process_user(user)
  pull_from_queue_safely do
    messages = user.update_all(true)
    user.tags.each do |tag|
      message_collection = Tweetable::MessageCollection.find_or_create(:name, tag)
      user_collection = Tweetable::UserCollection.find_or_create(:name, tag)                    

      message_collection.update(:updated_at => Time.now.utc.to_s)
      user_collection.update(:updated_at => Time.now.utc.to_s)

      user_collection.user_set.add(user)
      messages.each{|m| message_collection.messages << m.id}
    end
    return messages
  end
end

.pull_from_queue_safelyObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tweetable/queue.rb', line 96

def self.pull_from_queue_safely
  begin
    yield
  rescue Tweetable::TweetableError => e      
    raise TemporaryPullFromQueueError.new("Twitter unavailable error: #{e}")
  rescue Twitter::Unavailable => e      
    raise TemporaryPullFromQueueError.new("Twitter unavailable error: #{e}")      
  rescue URI::InvalidURIError => e      
    raise PullFromQueueError.new("Bad uri error: #{e}")
  rescue ArgumentError => e
    raise PullFromQueueError.new("Argument error: #{e}")
  rescue Crack::ParseError => e
    raise PullFromQueueError.new("Parsing error: #{e}")
  rescue Twitter::NotFound => e
    raise PullFromQueueError.new("Account does not exist: #{e}")
  rescue Twitter::Unauthorized => e
    raise PullFromQueueError.new("Not authorized error: #{e}")
  rescue Errno::ETIMEDOUT => e
    raise PullFromQueueError.new("Connection timed out error: #{e}")
  rescue Exception => e
    HoptoadNotifier.notify(e)          
    raise PullFromQueueError.new("General error: #{e}")
  end
end

.pull_from_search_queue(queue_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tweetable/queue.rb', line 45

def self.pull_from_search_queue(queue_name)
  queue = Tweetable::SearchCollection.find(:name, queue_name).first
  return 0 if (queue.nil? or queue.searches.empty?)

  count = 0
  while !queue.searches.empty?        
    search = Tweetable::Search[queue.searches.pop] # have to find object by id in List
    process_search(search)
    count += 1
  end     

  return count
end

.pull_from_user_queue(queue_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tweetable/queue.rb', line 59

def self.pull_from_user_queue(queue_name)
  queue = Tweetable::UserCollection.find(:name, queue_name).first
  return if (queue.nil? or queue.users.empty?)

  count = 0
  while !queue.users.empty?        
    user = Tweetable::User[queue.users.pop] # have to find object by id in List
    process_user(user)
    count += 1
  end     

  return count
end