Class: Mandrill::WebHook::Processor
- Inherits:
-
Object
- Object
- Mandrill::WebHook::Processor
- Defined in:
- lib/mandrill/web_hook/processor.rb
Instance Attribute Summary collapse
-
#callback_host ⇒ Object
Returns the value of attribute callback_host.
-
#mandrill_events ⇒ Object
Returns the value of attribute mandrill_events.
-
#on_unhandled_mandrill_events ⇒ Object
Returns the value of attribute on_unhandled_mandrill_events.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
-
.authentic?(expected_signature, mandrill_webhook_keys, original_url, params) ⇒ Boolean
Returns true if
params
sent tooriginal_url
are authentic givenexpected_signature
andmandrill_webhook_keys
. -
.generate_signature(webhook_key, original_url, params) ⇒ Object
Method described in docs: help.mandrill.com/entries/23704122-Authenticating-webhook-requests.
Instance Method Summary collapse
-
#initialize(params = {}, callback_host = nil) ⇒ Processor
constructor
Command initialise the processor with
params
Hash. -
#run! ⇒ Object
Command: processes all
mandrill_events
. -
#wrap_payload(raw_event_payload) ⇒ Object
Returns a suitably ecapsulated
raw_event_payload
.
Constructor Details
#initialize(params = {}, callback_host = nil) ⇒ Processor
Command initialise the processor with params
Hash. params
is expected to contain an array of mandrill_events. callback_host
is a handle to the controller making the request.
9 10 11 12 |
# File 'lib/mandrill/web_hook/processor.rb', line 9 def initialize(params={},callback_host=nil) self.params = params self.callback_host = callback_host end |
Instance Attribute Details
#callback_host ⇒ Object
Returns the value of attribute callback_host.
3 4 5 |
# File 'lib/mandrill/web_hook/processor.rb', line 3 def callback_host @callback_host end |
#mandrill_events ⇒ Object
Returns the value of attribute mandrill_events.
3 4 5 |
# File 'lib/mandrill/web_hook/processor.rb', line 3 def mandrill_events @mandrill_events end |
#on_unhandled_mandrill_events ⇒ Object
Returns the value of attribute on_unhandled_mandrill_events.
4 5 6 |
# File 'lib/mandrill/web_hook/processor.rb', line 4 def on_unhandled_mandrill_events @on_unhandled_mandrill_events end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/mandrill/web_hook/processor.rb', line 3 def params @params end |
Class Method Details
.authentic?(expected_signature, mandrill_webhook_keys, original_url, params) ⇒ Boolean
Returns true if params
sent to original_url
are authentic given expected_signature
and mandrill_webhook_keys
.
51 52 53 54 55 56 57 58 59 |
# File 'lib/mandrill/web_hook/processor.rb', line 51 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) result = (signature == expected_signature) break if result end result end |
.generate_signature(webhook_key, original_url, params) ⇒ Object
Method described in docs: help.mandrill.com/entries/23704122-Authenticating-webhook-requests
62 63 64 65 66 67 68 69 |
# File 'lib/mandrill/web_hook/processor.rb', line 62 def generate_signature(webhook_key, original_url, params) signed_data = original_url.dup params.keys.sort.each do |key| signed_data << key signed_data << params[key] end Base64.encode64("#{OpenSSL::HMAC.digest('sha1', webhook_key, signed_data)}").strip end |
Instance Method Details
#run! ⇒ Object
Command: processes all mandrill_events
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mandrill/web_hook/processor.rb', line 21 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 = "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, else Rails.logger.error rescue nil end end end end |
#wrap_payload(raw_event_payload) ⇒ Object
Returns a suitably ecapsulated raw_event_payload
44 45 46 |
# File 'lib/mandrill/web_hook/processor.rb', line 44 def wrap_payload(raw_event_payload) Mandrill::WebHook::EventDecorator[raw_event_payload] end |