Update with Andrew's fix

Add tests

Irfan Charania 9 years ago
parent
commit
83b00fa225
2 changed files with 27 additions and 6 deletions
  1. 4 6
      app/models/agents/rss_agent.rb
  2. 23 0
      spec/models/agents/rss_agent_spec.rb

+ 4 - 6
app/models/agents/rss_agent.rb

@@ -27,7 +27,7 @@ module Agents
27 27
           * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
28 28
           * `disable_ssl_verification` - Set to `true` to disable ssl verification.
29 29
           * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
30
-          * `max_items_per_feed` - Limit number of items parsed (events created) per feed.
30
+          * `max_events_per_run` - Limit number of events created (items parsed) per run for feed.
31 31
       MD
32 32
     end
33 33
 
@@ -77,12 +77,10 @@ module Agents
77 77
         if response.success?
78 78
           feed = FeedNormalizer::FeedNormalizer.parse(response.body)
79 79
           feed.clean! if interpolated['clean'] == 'true'
80
-
81
-          max_events = Integer(interpolated['max_items_per_feed']) if options['max_items_per_feed'].present?
82
-
80
+          max_events = (interpolated['max_events_per_run'].presence || 0).to_i
83 81
           created_event_count = 0
84
-          feed.entries.sort_by { |entry| [entry.date_published, entry.last_updated] }.each do |entry|
85
-            break if (!max_events.nil?) && (max_events >= 0) && (created_event_count >= max_events)
82
+          feed.entries.sort_by { |entry| [entry.date_published, entry.last_updated] }.each.with_index do |entry, index|
83
+            break if max_events && max_events > 0 && index >= max_events
86 84
             entry_id = get_entry_id(entry)
87 85
             if check_and_track(entry_id)
88 86
               created_event_count += 1

+ 23 - 0
spec/models/agents/rss_agent_spec.rb

@@ -96,6 +96,29 @@ describe Agents::RssAgent do
96 96
         agent.check
97 97
       }.to change { agent.events.count }.by(20 + 79)
98 98
     end
99
+    
100
+    it "should fetch one event per run" do
101
+      agent.options['url'] = ["https://github.com/cantino/huginn/commits/master.atom"]
102
+      
103
+      agent.options['max_events_per_run'] = 1
104
+      agent.check
105
+      expect(agent.events.count).to eq(1)
106
+    end
107
+
108
+    it "should fetch all events per run" do
109
+      agent.options['url'] = ["https://github.com/cantino/huginn/commits/master.atom"]
110
+      
111
+      # <= 0 should ignore option and get all
112
+      agent.options['max_events_per_run'] = 0
113
+      agent.check
114
+      expect(agent.events.count).to eq(20)
115
+
116
+      agent.options['max_events_per_run'] = -1
117
+      expect {
118
+        agent.check
119
+      }.to_not change { agent.events.count }
120
+    end
121
+
99 122
   end
100 123
 
101 124
   context "when no ids are available" do