From e3aa18b3a0e48e21a0f91fd8547e69ee89f7d9cf Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Sat, 11 May 2013 18:07:00 +0530 Subject: [PATCH] Added Event Formatting Agent --- app/models/agents/event_formatting_agent.rb | 61 +++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 app/models/agents/event_formatting_agent.rb diff --git a/app/models/agents/event_formatting_agent.rb b/app/models/agents/event_formatting_agent.rb new file mode 100644 index 00000000..d2c6d354 --- /dev/null +++ b/app/models/agents/event_formatting_agent.rb @@ -0,0 +1,61 @@ +module Agents + class EventFormattingAgent < Agent + cannot_be_scheduled! + + description <<-MD + Event Formatting Agent allows you to format your events in any way you prefer. + For example if you have an event like + + { + :content => { + :text => "Lorem ipsum dolor sit amet" + }, + :fields => "consectetur adipisicing elit", + :data => "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." + } + + + And other agents which are receiving this event, say, Twilio Agent expects `message` key , and Digest Email Agent which expects a `subject` key, then you can reformat this event by filling `Options` in the following way. + + message => $.content.text + subject => $.fields + + Values must be provided in JSONPath. You can add as many keys as you like. + + Then, event generated by Event Formatting Agent will be like + + { + :message => "Lorem ipsum dolor sit amet" + :subject => "consectetur adipisicing elit" + } + + MD + + event_description <<-MD + + User defined + MD + + def default_options + { + :message => "$.data.fields.content" + } + end + + def working? + true + end + + def receive(incoming_events) + incoming_events.each do |event| + formatted_event = {} + options.each_pair do |key,value| + formatted_event[key] = Utils.values_at event.payload,value + end + formatted_event[:agent] = Agent.find(event.agent_id).type.slice! 8..-1 + formatted_event[:created_at] = event.created_at + create_event :payload => formatted_event + end + end + end +end \ No newline at end of file