class Mandrill::WebHook::Processor

Attributes

callback_host[RW]
mandrill_events[RW]
on_unhandled_mandrill_events[RW]
params[RW]

Public Class Methods

authentic?(expected_signature, mandrill_webhook_keys, original_url, params) click to toggle source

Returns true if params sent to original_url are authentic given expected_signature and mandrill_webhook_keys.

# File lib/mandrill/web_hook/processor.rb, line 27
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) click to toggle source

Method described in docs: help.mandrill.com/entries/23704122-Authenticating-webhook-requests

# File lib/mandrill/web_hook/processor.rb, line 38
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
new(params={},callback_host=nil) click to toggle source

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.

# File lib/mandrill/web_hook/processor.rb, line 9
def initialize(params={},callback_host=nil)
  self.params = params
  self.callback_host = callback_host
end

Public Instance Methods

run!() click to toggle source

Command: processes all mandrill_events

# File lib/mandrill/web_hook/processor.rb, line 19
def run!
  mandrill_events.each do |raw_payload|
    process_event(Mandrill::WebHook::EventDecorator[raw_payload])
  end
end

Private Instance Methods

process_event(event_payload) click to toggle source

Command: attempts to process event_payload

# File lib/mandrill/web_hook/processor.rb, line 52
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