From 28931f2126a4eb772774f888dcb292916d26ef51 Mon Sep 17 00:00:00 2001 From: Dominik Sander Date: Thu, 1 May 2014 21:25:56 +0200 Subject: [PATCH] Migrated DataOutputAgent to use liquid --- app/models/agents/data_output_agent.rb | 12 +++++++----- ...0501191849_migrate_data_output_agent_to_liquid.rb | 7 +++++++ lib/liquid_migrator.rb | 6 +++--- spec/lib/liquid_migrator_spec.rb | 12 ++++++++++++ spec/models/agents/data_output_agent_spec.rb | 3 +++ 5 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20140501191849_migrate_data_output_agent_to_liquid.rb diff --git a/app/models/agents/data_output_agent.rb b/app/models/agents/data_output_agent.rb index 82a4dfad..29f75a54 100644 --- a/app/models/agents/data_output_agent.rb +++ b/app/models/agents/data_output_agent.rb @@ -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 diff --git a/db/migrate/20140501191849_migrate_data_output_agent_to_liquid.rb b/db/migrate/20140501191849_migrate_data_output_agent_to_liquid.rb new file mode 100644 index 00000000..5b6159ba --- /dev/null +++ b/db/migrate/20140501191849_migrate_data_output_agent_to_liquid.rb @@ -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 diff --git a/lib/liquid_migrator.rb b/lib/liquid_migrator.rb index 9d52c401..0acfcc56 100644 --- a/lib/liquid_migrator.rb +++ b/lib/liquid_migrator.rb @@ -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 diff --git a/spec/lib/liquid_migrator_spec.rb b/spec/lib/liquid_migrator_spec.rb index c2858e3c..ef6195e0 100644 --- a/spec/lib/liquid_migrator_spec.rb +++ b/spec/lib/liquid_migrator_spec.rb @@ -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) }. diff --git a/spec/models/agents/data_output_agent_spec.rb b/spec/models/agents/data_output_agent_spec.rb index fa465d3f..6999a156 100644 --- a/spec/models/agents/data_output_agent_spec.rb +++ b/spec/models/agents/data_output_agent_spec.rb @@ -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)