Migrated DataOutputAgent to use liquid

This commit is contained in:
Dominik Sander 2014-05-01 21:25:56 +02:00
parent 0980458449
commit 28931f2126
5 changed files with 32 additions and 8 deletions

View file

@ -1,5 +1,7 @@
module Agents
class DataOutputAgent < Agent
include LiquidInterpolatable
cannot_be_scheduled!
description do
@ -19,7 +21,7 @@ module Agents
* `secrets` - An array of tokens that the requestor must provide for light-weight authentication.
* `expected_receive_period_in_days` - How often you expect data to be received by this Agent from other Agents.
* `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.
* `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.
MD
end
@ -31,9 +33,9 @@ module Agents
"title" => "XKCD comics as a feed",
"description" => "This is a feed of recent XKCD comics, generated by Huginn",
"item" => {
"title" => "$.title",
"description" => "Secret hovertext: <$.hovertext>",
"link" => "$.url",
"title" => "{{title}}",
"description" => "Secret hovertext: {{hovertext}}",
"link" => "{{url}}",
}
}
}
@ -82,7 +84,7 @@ module Agents
def receive_web_request(params, method, format)
if options['secrets'].include?(params['secret'])
items = received_events.order('id desc').limit(events_to_show).map do |event|
interpolated = Utils.recursively_interpolate_jsonpaths(options['template']['item'], event.payload, :leading_dollarsign_is_jsonpath => true)
interpolated = interpolate_options(options['template']['item'], event.payload)
interpolated['guid'] = event.id
interpolated['pubDate'] = event.created_at.rfc2822.to_s
interpolated

View file

@ -0,0 +1,7 @@
class MigrateDataOutputAgentToLiquid < ActiveRecord::Migration
def change
Agent.where(:type => 'Agents::DataOutputAgent').each do |agent|
LiquidMigrator.convert_all_agent_options(agent)
end
end
end

View file

@ -19,10 +19,10 @@ module LiquidMigrator
keys_to_remove << path_key
end
hash[key] = LiquidMigrator.convert_string value, options[:leading_dollarsign_is_jsonpath]
when 'Hash'
raise "nested Hashes are not supported at the moment"
when 'ActiveSupport::HashWithIndifferentAccess'
hash[key] = convert_hash(hash[key], options)
when 'Array'
raise "nested Arrays are not supported at the moment"
hash[key] = hash[key].collect { |k| convert_string(k, options[:leading_dollarsign_is_jsonpath])}
end
end
# remove the unneeded *_path attributes

View file

@ -80,6 +80,18 @@ describe LiquidMigrator do
@agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
end
it "should work with nested hashes" do
@agent.options['very'] = {'nested' => '$.value'}
LiquidMigrator.convert_all_agent_options(@agent)
@agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'very' => {'nested' => '{{value}}'}, 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
end
it "should work with nested arrays" do
@agent.options['array'] = ["one", "$.two"]
LiquidMigrator.convert_all_agent_options(@agent)
@agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'array' => ['one', '{{two}}'], 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
end
it "should raise an exception when encountering complex JSONPaths" do
@agent.options['username_path'] = "$.very.complex[*]"
expect { LiquidMigrator.convert_all_agent_options(@agent) }.

View file

@ -1,8 +1,11 @@
# encoding: utf-8
require 'spec_helper'
require 'models/concerns/liquid_interpolatable'
describe Agents::DataOutputAgent do
it_behaves_like LiquidInterpolatable
let(:agent) do
_agent = Agents::DataOutputAgent.new(:name => 'My Data Output Agent')
_agent.options = _agent.default_options.merge('secrets' => ['secret1', 'secret2'], 'events_to_show' => 2)