Merge pull request #488 from knu/data_output_agent-pubDate

DataOutputAgent: Allow the `pubDate` value for each item to be given by user.

Akinori MUSHA 10 lat temu
rodzic
commit
b71ad2e99c

+ 12 - 2
app/models/agents/data_output_agent.rb

@@ -19,7 +19,9 @@ module Agents
19 19
 
20 20
           * `secrets` - An array of tokens that the requestor must provide for light-weight authentication.
21 21
           * `expected_receive_period_in_days` - How often you expect data to be received by this Agent from other Agents.
22
-          * `template` - A JSON object representing a mapping between item output keys and incoming event values. Use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) to format the values. The `item` key will be repeated for every Event.
22
+          * `template` - A JSON object representing a mapping between item output keys and incoming event values. Use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) to format the values. The `item` key will be repeated for every Event. The `pubDate` key for each item will have the creation time of the Event unless given.
23
+          * `events_to_show` - The number of events to output in RSS or JSON. (default: `40`)
24
+          * `ttl` - A value for the <ttl> element in RSS output. (default: `60`)
23 25
       MD
24 26
     end
25 27
 
@@ -85,7 +87,15 @@ module Agents
85 87
         items = received_events.order('id desc').limit(events_to_show).map do |event|
86 88
           interpolated = interpolate_options(options['template']['item'], event)
87 89
           interpolated['guid'] = event.id
88
-          interpolated['pubDate'] = event.created_at.rfc2822.to_s
90
+          date_string = interpolated['pubDate'].to_s
91
+          date =
92
+            begin
93
+              Time.zone.parse(date_string)  # may return nil
94
+            rescue => e
95
+              error "Error parsing a \"pubDate\" value \"#{date_string}\": #{e.message}"
96
+              nil
97
+            end || event.created_at
98
+          interpolated['pubDate'] = date.rfc2822.to_s
89 99
           interpolated
90 100
         end
91 101
 

+ 30 - 3
spec/models/agents/data_output_agent_spec.rb

@@ -5,7 +5,8 @@ require 'spec_helper'
5 5
 describe Agents::DataOutputAgent do
6 6
   let(:agent) do
7 7
     _agent = Agents::DataOutputAgent.new(:name => 'My Data Output Agent')
8
-    _agent.options = _agent.default_options.merge('secrets' => ['secret1', 'secret2'], 'events_to_show' => 2)
8
+    _agent.options = _agent.default_options.merge('secrets' => ['secret1', 'secret2'], 'events_to_show' => 3)
9
+    _agent.options['template']['item']['pubDate'] = "{{date}}"
9 10
     _agent.user = users(:bob)
10 11
     _agent.sources << agents(:bob_website_agent)
11 12
     _agent.save!
@@ -95,6 +96,16 @@ describe Agents::DataOutputAgent do
95 96
         agents(:bob_website_agent).create_event :payload => {
96 97
           "url" => "http://imgs.xkcd.com/comics/evolving2.png",
97 98
           "title" => "Evolving again",
99
+          "date" => '',
100
+          "hovertext" => "Something else"
101
+        }
102
+      end
103
+
104
+      let!(:event3) do
105
+        agents(:bob_website_agent).create_event :payload => {
106
+          "url" => "http://imgs.xkcd.com/comics/evolving0.png",
107
+          "title" => "Evolving yet again with a past date",
108
+          "date" => '2014/05/05',
98 109
           "hovertext" => "Something else"
99 110
         }
100 111
       end
@@ -116,19 +127,27 @@ describe Agents::DataOutputAgent do
116 127
            <ttl>60</ttl>
117 128
 
118 129
            <item>
130
+            <title>Evolving yet again with a past date</title>
131
+            <description>Secret hovertext: Something else</description>
132
+            <link>http://imgs.xkcd.com/comics/evolving0.png</link>
133
+            <pubDate>#{Time.zone.parse(event3.payload['date']).rfc2822}</pubDate>
134
+            <guid>#{event3.id}</guid>
135
+           </item>
136
+
137
+           <item>
119 138
             <title>Evolving again</title>
120 139
             <description>Secret hovertext: Something else</description>
121 140
             <link>http://imgs.xkcd.com/comics/evolving2.png</link>
122
-            <guid>#{event2.id}</guid>
123 141
             <pubDate>#{event2.created_at.rfc2822}</pubDate>
142
+            <guid>#{event2.id}</guid>
124 143
            </item>
125 144
 
126 145
            <item>
127 146
             <title>Evolving</title>
128 147
             <description>Secret hovertext: Biologists play reverse Pokemon, trying to avoid putting any one team member on the front lines long enough for the experience to cause evolution.</description>
129 148
             <link>http://imgs.xkcd.com/comics/evolving.png</link>
130
-            <guid>#{event1.id}</guid>
131 149
             <pubDate>#{event1.created_at.rfc2822}</pubDate>
150
+            <guid>#{event1.id}</guid>
132 151
            </item>
133 152
 
134 153
           </channel>
@@ -148,6 +167,14 @@ describe Agents::DataOutputAgent do
148 167
           'pubDate' => Time.now,
149 168
           'items' => [
150 169
             {
170
+              'title' => 'Evolving yet again with a past date',
171
+              'description' => 'Secret hovertext: Something else',
172
+              'link' => 'http://imgs.xkcd.com/comics/evolving0.png',
173
+              'guid' => event3.id,
174
+              'pubDate' => Time.zone.parse(event3.payload['date']).rfc2822,
175
+              'foo' => 'hi'
176
+            },
177
+            {
151 178
               'title' => 'Evolving again',
152 179
               'description' => 'Secret hovertext: Something else',
153 180
               'link' => 'http://imgs.xkcd.com/comics/evolving2.png',