From 39765f88754c9b604a487c200a97be37f61c61c4 Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Sat, 26 Dec 2015 11:25:47 -0800 Subject: [PATCH] Rename to `data_from_event` and use Liquid --- app/models/agents/website_agent.rb | 14 ++++++-------- spec/models/agents/website_agent_spec.rb | 10 +++++----- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/models/agents/website_agent.rb b/app/models/agents/website_agent.rb index d0ef15b2..02a7d6fa 100644 --- a/app/models/agents/website_agent.rb +++ b/app/models/agents/website_agent.rb @@ -24,7 +24,7 @@ module Agents * If the Event contains a `url` key, that URL will be fetched. * For more control, you can set the `url_from_event` option and it will be used as a Liquid template to generate the url to access based on the Event. - * If you set `event_data_path` to the [JSONPath](http://goessner.net/articles/JsonPath/) of content in the Event, that will be used directly without fetching any URL. + * If you set `data_from_event` to the [JSONPath](http://goessner.net/articles/JsonPath/) of content in the Event, that will be used directly without fetching any URL. * If you specify `merge` for the `mode` option, Huginn will retain the old payload and update it with the new values. # Supported Document Types @@ -145,7 +145,7 @@ module Agents def validate_options # Check for required fields - errors.add(:base, "either url, url_from_event, or event_data_path are required") unless options['url'].present? || options['url_from_event'].present? || options['event_data_path'].present? + errors.add(:base, "either url, url_from_event, or data_from_event are required") unless options['url'].present? || options['url_from_event'].present? || options['data_from_event'].present? errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present? validate_extract_options! @@ -331,18 +331,16 @@ module Agents interpolate_with(event) do existing_payload = interpolated['mode'].to_s == "merge" ? event.payload : {} - if event_data_path = options['event_data_path'].presence - data = Utils.value_at(event.payload, interpolate_options(event_data_path)) + if data_from_event = options['data_from_event'].presence + data = interpolate_options(data_from_event) if data.present? handle_event_data(data, event, existing_payload) else - error "No data was found in the Event payload at the JSONPath #{interpolate_options(event_data_path)}", inbound_event: event + error "No data was found in the Event payload using the template #{data_from_event}", inbound_event: event end else url_to_scrape = - if event_data_path = options['event_data_path'].presence - interpolate_options(event_data_path) - elsif url_template = options['url_from_event'].presence + if url_template = options['url_from_event'].presence interpolate_options(url_template) else event.payload['url'] diff --git a/spec/models/agents/website_agent_spec.rb b/spec/models/agents/website_agent_spec.rb index 7dcc766d..4e2782e9 100644 --- a/spec/models/agents/website_agent_spec.rb +++ b/spec/models/agents/website_agent_spec.rb @@ -853,7 +853,7 @@ fire: hot end end - describe "with a event_data_path" do + describe "with a data_from_event" do describe "with json data" do before do @event = Event.new @@ -868,7 +868,7 @@ fire: hot @checker.options = @valid_options.merge( 'type' => 'json', - 'event_data_path' => 'some_object.some_data', + 'data_from_event' => '{{ some_object.some_data }}', 'extract' => { 'value' => { 'path' => 'hello' } } @@ -893,14 +893,14 @@ fire: hot it "should output an error when nothing can be found at the path" do @checker.options = @checker.options.merge( - 'event_data_path' => 'some_object.mistake' + 'data_from_event' => '{{ some_object.mistake }}' ) expect { @checker.receive([@event]) }.to_not change { Event.count } - expect(@checker.logs.last.message).to match(/No data was found in the Event payload at the JSONPath some_object.mistake/) + expect(@checker.logs.last.message).to match(/No data was found in the Event payload using the template {{ some_object\.mistake }}/) end it "should output an error when the data cannot be parsed" do @@ -928,7 +928,7 @@ fire: hot @checker.options = @valid_options.merge( 'type' => 'html', - 'event_data_path' => 'some_object.some_data', + 'data_from_event' => '{{ some_object.some_data }}', 'extract' => { 'title' => { 'css' => ".title", 'value' => ".//text()" }, 'body' => { 'css' => "div span.body", 'value' => ".//text()" }