lib/mandrill/web_hook/processor.rb in mandrill-rails-1.2.0 vs lib/mandrill/web_hook/processor.rb in mandrill-rails-1.3.0
- old
+ new
@@ -10,45 +10,21 @@
self.params = params
self.callback_host = callback_host
end
def mandrill_events
- @mandrill_events ||= JSON.parse(params['mandrill_events'] || '[]')
- rescue
- @mandrill_events = []
+ @mandrill_events ||= JSON.parse(params['mandrill_events']) rescue []
end
# Command: processes all +mandrill_events+
def run!
mandrill_events.each do |raw_payload|
- event_payload = wrap_payload(raw_payload)
- handler = "handle_#{event_payload.event_type}".to_sym
- if callback_host && callback_host.respond_to?(handler, true)
- callback_host.send(handler,event_payload)
- elsif self.respond_to?(handler)
- self.send(handler,event_payload)
- else
- error_message = "Expected handler method `#{handler}` for event type `#{event_payload.event_type}`"
- case on_unhandled_mandrill_events
- when :ignore
- # NOP
- when :raise_exception
- raise Mandrill::Rails::Errors::MissingEventHandler, error_message
- else
- Rails.logger.error error_message rescue nil
- end
- end
+ process_event(Mandrill::WebHook::EventDecorator[raw_payload])
end
end
- # Returns a suitably ecapsulated +raw_event_payload+
- def wrap_payload(raw_event_payload)
- Mandrill::WebHook::EventDecorator[raw_event_payload]
- end
-
class << self
-
# Returns true if +params+ sent to +original_url+ are authentic given +expected_signature+ and +mandrill_webhook_keys+.
def authentic?(expected_signature, mandrill_webhook_keys, original_url, params)
result = true
Array(mandrill_webhook_keys).each do |key|
signature = generate_signature(key, original_url, params)
@@ -65,9 +41,31 @@
signed_data << key
signed_data << params[key]
end
Base64.encode64("#{OpenSSL::HMAC.digest('sha1', webhook_key, signed_data)}").strip
end
-
end
+ private
+
+
+ # Command: attempts to process +event_payload+
+ def process_event(event_payload)
+ handler = "handle_#{event_payload.event_type}".to_sym
+
+ if callback_host && callback_host.respond_to?(handler, true)
+ callback_host.send(handler,event_payload)
+ elsif self.respond_to?(handler)
+ self.send(handler,event_payload)
+ else
+ error_message = "Expected handler method `#{handler}` for event type `#{event_payload.event_type}`"
+ case on_unhandled_mandrill_events
+ when :ignore
+ # NOP
+ when :raise_exception
+ raise Mandrill::Rails::Errors::MissingEventHandler, error_message
+ else
+ Rails.logger.error error_message rescue nil
+ end
+ end
+ end
end