Module: MailgunWebhooks

Extended by:
MailgunWebhooks
Included in:
MailgunWebhooks
Defined in:
lib/mailgun_webhooks.rb,
lib/mailgun_webhooks/rack.rb,
lib/mailgun_webhooks/version.rb,
lib/mailgun_webhooks/signature.rb,
lib/mailgun_webhooks/rails/railtie.rb,
lib/mailgun_webhooks/webhook_registry.rb

Defined Under Namespace

Modules: Rails Classes: Rack, Signature, WebhookRegistry

Constant Summary collapse

Error =

Public: Base class for any errors raised from within MailgunWebhooks.

Class.new(StandardError)
InvalidSignature =

Public: Error raised when signature is invalid.

Class.new(Error)
VERSION =
"0.0.2"

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.api_hostObject

Public: The Mailgun API host.

Returns a String.



25
26
27
# File 'lib/mailgun_webhooks.rb', line 25

def api_host
  @api_host
end

.api_keyObject

Public: The Mailgun API key.

Returns a String.



20
21
22
# File 'lib/mailgun_webhooks.rb', line 20

def api_key
  @api_key
end

.endpointObject

Public: The endpoint for webhooks.

Returns a String.



30
31
32
# File 'lib/mailgun_webhooks.rb', line 30

def endpoint
  @endpoint
end

Instance Method Details

#on(webhook, &block) ⇒ Object

Public: Add a webhook to the registry.

Returns nothing.



42
43
44
# File 'lib/mailgun_webhooks.rb', line 42

def on(webhook, &block)
  webhooks.on(webhook, &block)
end

#trigger(webhook, data) ⇒ Object

Public: Verify the signature of the data and trigger the webhook if it is valid. Raise InvalidSignature if the data signature is not valid.

Returns nothing.

Raises:



50
51
52
53
54
# File 'lib/mailgun_webhooks.rb', line 50

def trigger(webhook, data)
  raise InvalidSignature unless verify_signature(data)

  webhooks.trigger(webhook, data)
end

#verify_signature(data) ⇒ Object

Public: Helper method to check the validity of the data.

Returns a boolean.



59
60
61
# File 'lib/mailgun_webhooks.rb', line 59

def verify_signature(data)
  Signature.valid?(data.merge('api_key' => api_key))
end

#webhooksObject

Internal: A WebhookRegistry instance.



64
65
66
# File 'lib/mailgun_webhooks.rb', line 64

def webhooks
  @webhooks ||= WebhookRegistry.new
end