event_formatting_agent.rb 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. module Agents
  2. class EventFormattingAgent < Agent
  3. cannot_be_scheduled!
  4. description <<-MD
  5. An Event Formatting Agent allows you to format incoming Events, adding new fields as needed.
  6. For example, here is a possible Event:
  7. {
  8. :high => {
  9. :celsius => "18",
  10. :fahreinheit => "64"
  11. },
  12. :conditions => "Rain showers",
  13. :data => "This is some data"
  14. }
  15. You may want to send this event to another Agent, for example a Twilio Agent, which expects a `message` key.
  16. You can use an Event Formatting Agent's `instructions` setting to do this in the following way:
  17. instructions: {
  18. message: "Today's conditions look like <$.conditions> with a high temperature of <$.high.celsius> degrees Celsius.",
  19. subject: "$.data"
  20. }
  21. JSONPaths must be between < and > . Make sure that you dont use these symbols anywhere else.
  22. Events generated by this possible Event Formatting Agent will look like:
  23. {
  24. :message => "Today's conditions look like Rain showers with a high temperature of 18 degrees Celsius.",
  25. :subject => "This is some data"
  26. }
  27. If you want to retain original contents of events and only add new keys, then set `mode` to `merge`, otherwise set it to `clean`.
  28. By default, the output event will have `agent` and `created_at` fields as well, reflecting the original Agent type and Event creation time. You can skip these outputs by setting `skip_agent` and `skip_created_at` to `true`.
  29. MD
  30. event_description <<-MD
  31. User defined
  32. MD
  33. def validate_options
  34. errors.add(:base, "instructions, mode, skip_agent, and skip_created_at all need to be present.") unless options[:instructions].present? and options[:mode].present? and options[:skip_agent].present? and options[:skip_created_at].present?
  35. end
  36. def default_options
  37. {
  38. :instructions => {
  39. :message => "You received a text <$.text> from <$.fields.from>",
  40. :content => "Looks like the weather is going to be <$.fields.weather>"},
  41. :mode => "clean",
  42. :skip_agent => "false",
  43. :skip_created_at => "false"
  44. }
  45. end
  46. def working?
  47. true
  48. end
  49. def value_constructor(value, payload)
  50. value.gsub(/<[^>]+>/).each {|jsonpath| Utils.values_at(payload,jsonpath[1..-2]).first.to_s }
  51. end
  52. def receive(incoming_events)
  53. incoming_events.each do |event|
  54. formatted_event = options[:mode].to_s == "merge" ? event.payload : {}
  55. options[:instructions].each_pair {|key, value| formatted_event[key] = value_constructor value, event.payload }
  56. formatted_event[:agent] = Agent.find(event.agent_id).type.slice!(8..-1) unless options[:skip_agent].to_s == "true"
  57. formatted_event[:created_at] = event.created_at unless options[:skip_created_at].to_s == "true"
  58. create_event :payload => formatted_event
  59. end
  60. end
  61. end
  62. end