Class: Mailgun::Webhooks

Inherits:
Object
  • Object
show all
Includes:
ApiVersionChecker
Defined in:
lib/mailgun/webhooks/webhooks.rb

Overview

A Mailgun::Webhooks object is a simple CRUD interface to Mailgun Webhooks. Uses Mailgun

Constant Summary collapse

ACTIONS =
%w[accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed].freeze

Instance Method Summary collapse

Methods included from ApiVersionChecker

included

Constructor Details

#initialize(client = Mailgun::Client.new) ⇒ Webhooks

Public creates a new Mailgun::Webhooks instance.

Defaults to Mailgun::Client


13
14
15
# File 'lib/mailgun/webhooks/webhooks.rb', line 13

def initialize(client = Mailgun::Client.new)
  @client = client
end

Instance Method Details

#create(domain, action, url = '') ⇒ Object

Public: Add webhook

domain - A String of the domain name (ex. domain.com) action - A String of the action to create a webhook for url - A String of the url of the webhook

Returns a Boolean of whether the webhook was created



64
65
66
67
# File 'lib/mailgun/webhooks/webhooks.rb', line 64

def create(domain, action, url = '')
  res = @client.post("domains/#{domain}/webhooks", id: action, url: url)
  res.to_h['webhook']['urls'].include?(url) && res.to_h['message'] == 'Webhook has been created'
end

#get(domain, action) ⇒ Object

Public: Get webook information for a specific action

domain - a String of Domain name to find a webhook url for action - a String identifying the webhook to get the URL for

Returns a String of the url for the identified webhook or an

empty String if one is not set


43
44
45
46
# File 'lib/mailgun/webhooks/webhooks.rb', line 43

def get(domain, action)
  res = @client.get("domains/#{domain}/webhooks/#{action}")
  res.to_h['webhook']['urls'] || ''
end

#get_webhooks(domain, options = {}) ⇒ Object

:nocov:



30
31
32
33
# File 'lib/mailgun/webhooks/webhooks.rb', line 30

def get_webhooks(domain, options = {})
  warn('`get_webhooks` method will be deprecated in future versions of Mailgun. Please use `list` instead.')
  list(domain, options)
end

#list(domain, options = {}) ⇒ Object

Public: Get Webhooks

domain - a string the domain name to retrieve webhooks for options - a Hash of options

Returns a Hash of the list of domains or nil



23
24
25
26
# File 'lib/mailgun/webhooks/webhooks.rb', line 23

def list(domain, options = {})
  res = @client.get("domains/#{domain}/webhooks", options)
  res.to_h['webhooks']
end

#remove(domain, action) ⇒ Object

Public: Delete a specific webhook

domain - The required String of domain name action - The required String of the webhook action to delete

Returns a Boolean of the success



125
126
127
128
129
130
# File 'lib/mailgun/webhooks/webhooks.rb', line 125

def remove(domain, action)
  raise Mailgun::ParameterError('Domain not provided to remove webhook from') unless domain
  raise Mailgun::ParameterError('Action not provided to identify webhook to remove') unless action

  @client.delete("domains/#{domain}/webhooks/#{action}").to_h['message'] == 'Webhook has been deleted'
end

#update(domain, action, url = '') ⇒ Object

Public: Update webhook

domain - A String of the domain name (ex. domain.com) action - A String of the action to create a webhook for url - A String of the url of the webhook

Returns a Boolean of whether the webhook was updated



104
105
106
107
108
109
110
# File 'lib/mailgun/webhooks/webhooks.rb', line 104

def update(domain, action, url = '')
  raise Mailgun::ParameterError('Domain not provided to update webhooks') unless domain
  raise Mailgun::ParameterError('Action not provided to identify webhook to update') unless action

  res = @client.put("domains/#{domain}/webhooks/#{action}", id: action, url: url)
  res.to_h['message'] == 'Webhook has been updated'
end

#update_webhook(domain, action, url = '') ⇒ Object

:nocov:



113
114
115
116
# File 'lib/mailgun/webhooks/webhooks.rb', line 113

def update_webhook(domain, action, url = '')
  warn('`update_webhook` method will be deprecated in future versions of Mailgun. Please use `update` instead.')
  update(domain, action, url)
end