Merge branch 'master' into scenarios

Andrew Cantino %!s(int64=10) %!d(string=hace) años
padre
commit
cca78da090
Se han modificado 2 ficheros con 55 adiciones y 16 borrados
  1. 40 14
      app/models/agents/twitter_user_agent.rb
  2. 15 2
      spec/models/agents/twitter_user_agent_spec.rb

+ 40 - 14
app/models/agents/twitter_user_agent.rb

@@ -17,11 +17,15 @@ module Agents
17 17
 
18 18
       You must also provide the `username` of the Twitter user to monitor.
19 19
 
20
+      Set `include_retweets` to `false` to not include retweets (default: `true`)
21
+
20 22
       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.
23
+
24
+      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`)
21 25
     MD
22 26
 
23 27
     event_description <<-MD
24
-      Events are the raw JSON provided by the Twitter API. Should look something like:
28
+      Events are the raw JSON provided by the [Twitter API](https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline). Should look something like:
25 29
 
26 30
           {
27 31
              ... every Tweet field, including ...
@@ -46,38 +50,60 @@ module Agents
46 50
 
47 51
     default_schedule "every_1h"
48 52
 
49
-    def validate_options
50
-      unless options['username'].present? &&
51
-        options['expected_update_period_in_days'].present?
52
-        errors.add(:base, "username and expected_update_period_in_days are required")
53
-      end      
54
-    end
55
-
56 53
     def working?
57 54
       event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
58 55
     end
59 56
 
60 57
     def default_options
61 58
       {
62
-        'username' => "tectonic",
63
-        'expected_update_period_in_days' => "2"
59
+        'username' => 'tectonic',
60
+        'include_retweets' => 'true',
61
+        'expected_update_period_in_days' => '2'
64 62
       }
65 63
     end
66 64
 
65
+    def validate_options
66
+      errors.add(:base, "username is required") unless options['username'].present?
67
+      errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
68
+
69
+      if options[:include_retweets].present? && !%w[true false].include?(options[:include_retweets])
70
+        errors.add(:base, "include_retweets must be a boolean value string (true/false)")
71
+      end
72
+
73
+      if options[:starting_at].present?
74
+        Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
75
+      end
76
+    end
77
+
78
+    def starting_at
79
+      if options[:starting_at].present?
80
+        Time.parse(options[:starting_at]) rescue created_at
81
+      else
82
+        created_at
83
+      end
84
+    end
85
+
86
+    def include_retweets?
87
+      options[:include_retweets] != "false"
88
+    end
89
+
67 90
     def check
68 91
       since_id = memory['since_id'] || nil
69
-      opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
92
+      opts = {:count => 200, :include_rts => include_retweets?, :exclude_replies => false, :include_entities => true, :contributor_details => true}
70 93
       opts.merge! :since_id => since_id unless since_id.nil?
71 94
 
95
+      # http://rdoc.info/gems/twitter/Twitter/REST/Timelines#user_timeline-instance_method
72 96
       tweets = twitter.user_timeline(options['username'], opts)
73 97
 
74 98
       tweets.each do |tweet|
75
-        memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
99
+        if tweet.created_at >= starting_at
100
+          memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
76 101
 
77
-        create_event :payload => tweet.attrs
102
+          create_event :payload => tweet.attrs
103
+        end
78 104
       end
79 105
 
80 106
       save!
81 107
     end
82 108
   end
83
-end
109
+end

+ 15 - 2
spec/models/agents/twitter_user_agent_spec.rb

@@ -4,10 +4,11 @@ describe Agents::TwitterUserAgent do
4 4
   before do
5 5
     # intercept the twitter API request for @tectonic's user profile
6 6
     stub_request(:any, /tectonic/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
7
-  
7
+
8 8
     @opts = {
9 9
       :username => "tectonic",
10 10
       :expected_update_period_in_days => "2",
11
+      :starting_at => "Jan 01 00:00:01 +0000 2000",
11 12
       :consumer_key => "---",
12 13
       :consumer_secret => "---",
13 14
       :oauth_token => "---",
@@ -25,4 +26,16 @@ describe Agents::TwitterUserAgent do
25 26
     end
26 27
   end
27 28
 
28
-end
29
+  describe "#check with starting_at=future date" do
30
+    it "should check for changes starting_at a future date, thus not find any" do
31
+      opts = @opts.merge({ :starting_at => "Jan 01 00:00:01 +0000 2999", })
32
+
33
+      checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
34
+      checker.user = users(:bob)
35
+      checker.save!
36
+
37
+      lambda { checker.check }.should change { Event.count }.by(0)
38
+    end
39
+  end
40
+
41
+end