Class: Mandrill::WebHook::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill/web_hook/processor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_hostObject

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_eventsObject

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_eventsObject

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

#paramsObject

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.

Returns:

  • (Boolean)


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



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
      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

#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