replace retrieving 'all' results with var max_results limit to fix loop in test.

Brian Petro преди 9 години
родител
ревизия
4dff06ee9a
променени са 2 файла, в които са добавени 33 реда и са изтрити 19 реда
  1. 14 4
      app/models/agents/twitter_search_agent.rb
  2. 19 15
      spec/models/agents/twitter_search_agent_spec.rb

+ 14 - 4
app/models/agents/twitter_search_agent.rb

@@ -15,6 +15,8 @@ module Agents
15 15
       
16 16
       Set `result_type` to specify which [type of search results](https://dev.twitter.com/rest/reference/get/search/tweets) you would prefer to receive. Options are "mixed", "recent", and "popular". (default: `mixed`)
17 17
 
18
+      Set `max_results` to limit the amount of results to retrieve per run(default: `500`. The API rate limit is ~18,000 per 15 minutes. [Click here to learn more about rate limits](https://dev.twitter.com/rest/public/rate-limiting).
19
+
18 20
       Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
19 21
 
20 22
       Set `starting_at` to the date/time (eg. `Mon Jun 02 00:38:12 +0000 2014`) you want to start receiving tweets from (default: agent's `created_at`)
@@ -74,20 +76,28 @@ module Agents
74 76
       end
75 77
     end
76 78
 
79
+    def max_results
80
+      if interpolated['max_results'].present?
81
+        interpolated['max_results'].to_i
82
+      else 
83
+        500
84
+      end
85
+    end
86
+
77 87
     def check
78 88
       since_id = memory['since_id'] || nil
79 89
       opts = {include_entities: true}
80 90
       opts.merge! result_type: interpolated[:result_type] if interpolated[:result_type].present?
81
-      opts.merge! :since_id => since_id unless since_id.nil?
91
+      opts.merge! since_id: since_id unless since_id.nil?
82 92
 
83 93
       # http://www.rubydoc.info/gems/twitter/Twitter/REST/Search
84
-      tweets = twitter.search(interpolated['search'], opts).to_a
94
+      tweets = twitter.search(interpolated['search'], opts).take(max_results)
85 95
 
86 96
       tweets.each do |tweet|
87
-        if tweet.created_at >= starting_at
97
+        if (tweet.created_at >= starting_at)
88 98
           memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
89 99
 
90
-          create_event :payload => tweet.attrs
100
+          create_event payload: tweet.attrs
91 101
         end
92 102
       end
93 103
 

+ 19 - 15
spec/models/agents/twitter_search_agent_spec.rb

@@ -3,35 +3,39 @@ require 'spec_helper'
3 3
 describe Agents::TwitterSearchAgent do
4 4
   before do
5 5
     # intercept the twitter API request
6
-    stub_request(:any, /freebandnames/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/search_tweets.json")), :status => 200)
6
+    stub_request(:any, /freebandnames/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/search_tweets.json")), status: 200)
7 7
 
8 8
     @opts = {
9
-      :search => "freebandnames",
10
-      :expected_update_period_in_days => "2",
11
-      :starting_at => "Jan 01 00:00:01 +0000 2000",
12
-      :consumer_key => "---",
13
-      :consumer_secret => "---",
14
-      :oauth_token => "---",
15
-      :oauth_token_secret => "---"
9
+      search: "freebandnames",
10
+      expected_update_period_in_days: "2",
11
+      starting_at: "Jan 01 00:00:01 +0000 2000",
12
+      max_results: '3',
13
+      consumer_key: "---",
14
+      consumer_secret: "---",
15
+      oauth_token: "---",
16
+      oauth_token_secret: "---"
16 17
     }
17 18
 
18
-    @checker = Agents::TwitterSearchAgent.new(:name => "search freebandnames", :options => @opts)
19
-    @checker.service = services(:generic)
20
-    @checker.user = users(:bob)
21
-    @checker.save!
22 19
   end
20
+  let(:checker) {
21
+    _checker = Agents::TwitterSearchAgent.new(name: "search freebandnames", options: @opts)
22
+    _checker.service = services(:generic)
23
+    _checker.user = users(:bob)
24
+    _checker.save!
25
+    _checker
26
+  }
23 27
 
24 28
   describe "#check" do
25 29
     it "should check for changes" do
26
-      expect { @checker.check }.to change { Event.count }.by(100)
30
+      expect { checker.check }.to change { Event.count }.by(3)
27 31
     end
28 32
   end
29 33
 
30 34
   describe "#check with starting_at=future date" do
31 35
     it "should check for changes starting_at a future date, thus not find any" do
32
-      opts = @opts.merge({ :starting_at => "Jan 01 00:00:01 +0000 2999" })
36
+      opts = @opts.merge({ starting_at: "Jan 01 00:00:01 +0000 2999" })
33 37
 
34
-      checker = Agents::TwitterSearchAgent.new(:name => "searching freebandnames", :options => opts)
38
+      checker = Agents::TwitterSearchAgent.new(name: "search freebandnames", options: opts)
35 39
       checker.service = services(:generic)
36 40
       checker.user = users(:bob)
37 41
       checker.save!