@@ -1,5 +1,7 @@ |
||
1 | 1 |
module Agents |
2 | 2 |
class DataOutputAgent < Agent |
3 |
+ include LiquidInterpolatable |
|
4 |
+ |
|
3 | 5 |
cannot_be_scheduled! |
4 | 6 |
|
5 | 7 |
description do |
@@ -19,7 +21,7 @@ module Agents |
||
19 | 21 |
|
20 | 22 |
* `secrets` - An array of tokens that the requestor must provide for light-weight authentication. |
21 | 23 |
* `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 JSONPath values. JSONPath values must start with `$`, or can be interpolated between `<` and `>` characters. The `item` key will be repeated for every Event. |
|
24 |
+ * `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. |
|
23 | 25 |
MD |
24 | 26 |
end |
25 | 27 |
|
@@ -31,9 +33,9 @@ module Agents |
||
31 | 33 |
"title" => "XKCD comics as a feed", |
32 | 34 |
"description" => "This is a feed of recent XKCD comics, generated by Huginn", |
33 | 35 |
"item" => { |
34 |
- "title" => "$.title", |
|
35 |
- "description" => "Secret hovertext: <$.hovertext>", |
|
36 |
- "link" => "$.url", |
|
36 |
+ "title" => "{{title}}", |
|
37 |
+ "description" => "Secret hovertext: {{hovertext}}", |
|
38 |
+ "link" => "{{url}}", |
|
37 | 39 |
} |
38 | 40 |
} |
39 | 41 |
} |
@@ -82,7 +84,7 @@ module Agents |
||
82 | 84 |
def receive_web_request(params, method, format) |
83 | 85 |
if options['secrets'].include?(params['secret']) |
84 | 86 |
items = received_events.order('id desc').limit(events_to_show).map do |event| |
85 |
- interpolated = Utils.recursively_interpolate_jsonpaths(options['template']['item'], event.payload, :leading_dollarsign_is_jsonpath => true) |
|
87 |
+ interpolated = interpolate_options(options['template']['item'], event.payload) |
|
86 | 88 |
interpolated['guid'] = event.id |
87 | 89 |
interpolated['pubDate'] = event.created_at.rfc2822.to_s |
88 | 90 |
interpolated |
@@ -0,0 +1,7 @@ |
||
1 |
+class MigrateDataOutputAgentToLiquid < ActiveRecord::Migration |
|
2 |
+ def change |
|
3 |
+ Agent.where(:type => 'Agents::DataOutputAgent').each do |agent| |
|
4 |
+ LiquidMigrator.convert_all_agent_options(agent) |
|
5 |
+ end |
|
6 |
+ end |
|
7 |
+end |
@@ -19,10 +19,10 @@ module LiquidMigrator |
||
19 | 19 |
keys_to_remove << path_key |
20 | 20 |
end |
21 | 21 |
hash[key] = LiquidMigrator.convert_string value, options[:leading_dollarsign_is_jsonpath] |
22 |
- when 'Hash' |
|
23 |
- raise "nested Hashes are not supported at the moment" |
|
22 |
+ when 'ActiveSupport::HashWithIndifferentAccess' |
|
23 |
+ hash[key] = convert_hash(hash[key], options) |
|
24 | 24 |
when 'Array' |
25 |
- raise "nested Arrays are not supported at the moment" |
|
25 |
+ hash[key] = hash[key].collect { |k| convert_string(k, options[:leading_dollarsign_is_jsonpath])} |
|
26 | 26 |
end |
27 | 27 |
end |
28 | 28 |
# remove the unneeded *_path attributes |
@@ -80,6 +80,18 @@ describe LiquidMigrator do |
||
80 | 80 |
@agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'} |
81 | 81 |
end |
82 | 82 |
|
83 |
+ it "should work with nested hashes" do |
|
84 |
+ @agent.options['very'] = {'nested' => '$.value'} |
|
85 |
+ LiquidMigrator.convert_all_agent_options(@agent) |
|
86 |
+ @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'very' => {'nested' => '{{value}}'}, 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'} |
|
87 |
+ end |
|
88 |
+ |
|
89 |
+ it "should work with nested arrays" do |
|
90 |
+ @agent.options['array'] = ["one", "$.two"] |
|
91 |
+ LiquidMigrator.convert_all_agent_options(@agent) |
|
92 |
+ @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'array' => ['one', '{{two}}'], 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'} |
|
93 |
+ end |
|
94 |
+ |
|
83 | 95 |
it "should raise an exception when encountering complex JSONPaths" do |
84 | 96 |
@agent.options['username_path'] = "$.very.complex[*]" |
85 | 97 |
expect { LiquidMigrator.convert_all_agent_options(@agent) }. |
@@ -1,8 +1,11 @@ |
||
1 | 1 |
# encoding: utf-8 |
2 | 2 |
|
3 | 3 |
require 'spec_helper' |
4 |
+require 'models/concerns/liquid_interpolatable' |
|
4 | 5 |
|
5 | 6 |
describe Agents::DataOutputAgent do |
7 |
+ it_behaves_like LiquidInterpolatable |
|
8 |
+ |
|
6 | 9 |
let(:agent) do |
7 | 10 |
_agent = Agents::DataOutputAgent.new(:name => 'My Data Output Agent') |
8 | 11 |
_agent.options = _agent.default_options.merge('secrets' => ['secret1', 'secret2'], 'events_to_show' => 2) |