Nenhuma Descrição http://j1x-huginn.herokuapp.com

event_formatting_agent.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. module Agents
  2. class EventFormattingAgent < Agent
  3. cannot_be_scheduled!
  4. description <<-MD
  5. Event Formatting Agent allows you to format your events in any way you prefer.
  6. For example if you have an event like
  7. {
  8. :high => {
  9. :celsius => "18",
  10. :fahreinheit => "64"
  11. },
  12. :conditions => "Rain showers",
  13. :data => "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
  14. }
  15. 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 `Instructions` in the following way.
  16. message => "Today's conditions look like <$.conditions> and high temperaure is going to be <$.high.celsius> degree Celsius.""
  17. subject => "$.data"
  18. JSONPaths must be between < and > . Make sure you dont use these symbols anywhere else.
  19. Event generated by Event Formatting Agent will be like
  20. {
  21. :message => "Today's conditions look like Rain showers and high temperaure is going to be 18 degree Celsius"
  22. :subject => "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
  23. }
  24. `Mode` can only be `merge` or `clean`. If you want to retain original contents of events and only add add new keys, then use `merge` mode else use `clean` mode.
  25. MD
  26. event_description <<-MD
  27. User defined
  28. MD
  29. def validate_options
  30. 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?
  31. end
  32. def default_options
  33. {
  34. :instructions => {
  35. :message => "You received a text <$.text> from <$.fields.from>",
  36. :content => "Looks like the weather is going to be <$.fields.weather>"},
  37. :mode => "clean",
  38. :skip_agent => "false",
  39. :skip_created_at => "false"
  40. }
  41. end
  42. def working?
  43. true
  44. end
  45. def value_constructor(value,payload)
  46. value.gsub(/<[^>]+>/).each {|jsonpath| Utils.values_at(payload,jsonpath[1..-2]).first.to_s}
  47. end
  48. def receive(incoming_events)
  49. incoming_events.each do |event|
  50. formatted_event = options[:mode].to_s == "merge" ? event.payload : {}
  51. options[:instructions].each_pair {|key,value| formatted_event[key] = value_constructor value, event.payload}
  52. formatted_event[:agent] = Agent.find(event.agent_id).type.slice!(8..-1) unless options[:skip_agent].to_s == "true"
  53. formatted_event[:created_at] = event.created_at unless options[:skip_created_at].to_s == "true"
  54. create_event :payload => formatted_event
  55. end
  56. end
  57. end
  58. end