Skip to content

Commit

Permalink
Remove the "skip_agent" option from EventFormattingAgent.
Browse files Browse the repository at this point in the history
It is now possible to include an agent name without the special flag
just by putting `"agent" => "{{agent.type}}"` in "instructions".

This commit includes automatic migration.
  • Loading branch information
knu committed Jul 22, 2014
1 parent e55bb4a commit f79e26d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 3 additions & 4 deletions app/models/agents/event_formatting_agent.rb
Expand Up @@ -68,7 +68,7 @@ class EventFormattingAgent < Agent
If you want to retain original contents of events and only add new keys, then set `mode` to `merge`, otherwise set it to `clean`.
By default, the output event will have `agent` and `created_at` fields added 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`.
By default, the output event will have `created_at` fields added as well, reflecting the original Event creation time. You can skip this output by setting `skip_created_at` to `true`.
To CGI escape output (for example when creating a link), use the Liquid `uri_escape` filter, like so:
Expand All @@ -82,7 +82,7 @@ class EventFormattingAgent < Agent
after_save :clear_matchers

def validate_options
errors.add(:base, "instructions, mode, skip_agent, and skip_created_at all need to be present.") unless options['instructions'].present? && options['mode'].present? && options['skip_agent'].present? && options['skip_created_at'].present?
errors.add(:base, "instructions, mode, and skip_created_at all need to be present.") unless options['instructions'].present? && options['mode'].present? && options['skip_created_at'].present?

validate_matchers
end
Expand All @@ -91,11 +91,11 @@ def default_options
{
'instructions' => {
'message' => "You received a text {{text}} from {{fields.from}}",
'agent' => "{{agent.type}}",
'some_other_field' => "Looks like the weather is going to be {{fields.weather}}"
},
'matchers' => [],
'mode' => "clean",
'skip_agent' => "false",
'skip_created_at' => "false"
}
end
Expand All @@ -111,7 +111,6 @@ def receive(incoming_events)
opts = interpolated(payload)
formatted_event = opts['mode'].to_s == "merge" ? event.payload.dup : {}
formatted_event.merge! opts['instructions']
formatted_event['agent'] = agent.short_type unless opts['skip_agent'].to_s == "true"
formatted_event['created_at'] = event.created_at unless opts['skip_created_at'].to_s == "true"
create_event :payload => formatted_event
end
Expand Down
21 changes: 21 additions & 0 deletions db/migrate/20140722131220_convert_efa_skip_agent.rb
@@ -0,0 +1,21 @@
class ConvertEfaSkipAgent < ActiveRecord::Migration
def up
Agent.where(type: 'Agents::EventFormattingAgent').each do |agent|
agent.options_will_change!
unless agent.options.delete('skip_agent').to_s == 'true'
agent.options['instructions'] = {
'agent' => '{{agent.type}}'
}.update(agent.options['instructions'] || {})
end
agent.save!
end
end

def down
Agent.where(type: 'Agents::EventFormattingAgent').each do |agent|
agent.options_will_change!
agent.options['skip_agent'] = (agent.options['instructions'] || {})['agent'] == '{{agent.type}}'
agent.save!
end
end
end

0 comments on commit f79e26d

Please sign in to comment.