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