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)


27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



38
39
40
41
42
43
44
45
# 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

Instance Method Details

#run!Object

Command: processes all mandrill_events



19
20
21
22
23
# 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