-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1364 from kreuzwerker/feature/json-parse
Add JsonParseAgent
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Agents | ||
class JsonParseAgent < Agent | ||
include FormConfigurable | ||
|
||
cannot_be_scheduled! | ||
|
||
description <<-MD | ||
The JSON Parse Agent parses a JSON string and emits the data in a new event | ||
`data` is the JSON to parse. Use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) templating to specify the JSON string. | ||
`data_key` sets the key which contains the parsed JSON data in emitted events | ||
MD | ||
|
||
def default_options | ||
{ | ||
'data' => '{{ data }}', | ||
'data_key' => 'data', | ||
} | ||
end | ||
|
||
event_description do | ||
"Events will looks like this:\n\n %s" % Utils.pretty_print(interpolated['data_key'] => {parsed: 'object'}) | ||
end | ||
|
||
form_configurable :data | ||
form_configurable :data_key | ||
|
||
def validate_options | ||
errors.add(:base, "data needs to be present") if options['data'].blank? | ||
errors.add(:base, "data_key needs to be present") if options['data_key'].blank? | ||
end | ||
|
||
def working? | ||
received_event_without_error? | ||
end | ||
|
||
def receive(incoming_events) | ||
incoming_events.each do |event| | ||
begin | ||
mo = interpolated(event) | ||
create_event payload: { mo['data_key'] => JSON.parse(mo['data']) } | ||
rescue JSON::JSONError => e | ||
error("Could not parse JSON: #{e.class} '#{e.message}'") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'rails_helper' | ||
|
||
describe Agents::JsonParseAgent do | ||
before(:each) do | ||
@checker = Agents::JsonParseAgent.new(:name => "somename", :options => Agents::JsonParseAgent.new.default_options) | ||
@checker.user = users(:jane) | ||
@checker.save! | ||
end | ||
|
||
it "event description does not throw an exception" do | ||
expect(@checker.event_description).to include('parsed') | ||
end | ||
|
||
describe "validating" do | ||
before do | ||
expect(@checker).to be_valid | ||
end | ||
|
||
it "requires data to be present" do | ||
@checker.options['data'] = '' | ||
expect(@checker).not_to be_valid | ||
end | ||
|
||
it "requires data_key to be set" do | ||
@checker.options['data_key'] = '' | ||
expect(@checker).not_to be_valid | ||
end | ||
end | ||
|
||
context '#working' do | ||
it 'is not working without having received an event' do | ||
expect(@checker).not_to be_working | ||
end | ||
|
||
it 'is working after receiving an event without error' do | ||
@checker.last_receive_at = Time.now | ||
expect(@checker).to be_working | ||
end | ||
end | ||
|
||
describe "#receive" do | ||
it "parses valid JSON" do | ||
event = Event.new(payload: { data: '{"test": "data"}' } ) | ||
expect { @checker.receive([event]) }.to change(Event, :count).by(1) | ||
end | ||
|
||
it "writes to the error log when the JSON could not be parsed" do | ||
event = Event.new(payload: { data: '{"test": "data}' } ) | ||
expect { @checker.receive([event]) }.to change(AgentLog, :count).by(1) | ||
end | ||
end | ||
end |