Skip to content

Commit

Permalink
Merge pull request #1364 from kreuzwerker/feature/json-parse
Browse files Browse the repository at this point in the history
Add JsonParseAgent
  • Loading branch information
dsander committed Mar 22, 2016
2 parents c834314 + e2981b0 commit 0755451
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
49 changes: 49 additions & 0 deletions app/models/agents/json_parse_agent.rb
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
52 changes: 52 additions & 0 deletions spec/models/agents/json_parse_agent_spec.rb
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

0 comments on commit 0755451

Please sign in to comment.