Class: Mailgun::Webhooks

Inherits:
Object
  • Object
show all
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

Constructor Details

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

Public creates a new Mailgun::Webhooks instance.

Defaults to Mailgun::Client


10
11
12
# File 'lib/mailgun/webhooks/webhooks.rb', line 10

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

Instance Method Details

#create(domain, action, url = '') ⇒ Object Also known as: add, add_webhook

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



48
49
50
51
# File 'lib/mailgun/webhooks/webhooks.rb', line 48

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

#create_all(domain, url = '') ⇒ Object Also known as: add_all_webhooks

Public: Sets all webhooks to the same URL

domain - A String of the domain name url - A String of the url to set all webhooks to

Returns true or false



61
62
63
64
65
66
67
68
# File 'lib/mailgun/webhooks/webhooks.rb', line 61

def create_all(domain, url = '')
  ACTIONS.each do |action|
    add_webhook domain, action, url
  end
  true
rescue
  false
end

#info(domain, action) ⇒ Object Also known as: get_webhook_url

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


33
34
35
36
37
38
# File 'lib/mailgun/webhooks/webhooks.rb', line 33

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

#list(domain, options = {}) ⇒ Object Also known as: get_webhooks

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



20
21
22
23
# File 'lib/mailgun/webhooks/webhooks.rb', line 20

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

#remove(domain, action) ⇒ Object Also known as: delete, delete_webhook

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



92
93
94
95
96
97
98
# File 'lib/mailgun/webhooks/webhooks.rb', line 92

def remove(domain, action)
  fail Mailgun::ParameterError('Domain not provided to remove webhook from') unless domain
  fail 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'
rescue Mailgun::CommunicationError
  false
end

#remove_all(domain) ⇒ Object Also known as: delete_all, delete_all_webooks

Public: Delete all webhooks for a domain

domain - A required String of the domain to remove all webhooks for

Returns a Boolean on the success



107
108
109
110
111
112
# File 'lib/mailgun/webhooks/webhooks.rb', line 107

def remove_all(domain)
  fail Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
  ACTIONS.each do |action|
    delete_webhook domain, action
  end
end

#update(domain, action, url = '') ⇒ Object Also known as: update_webhook

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



78
79
80
81
82
83
# File 'lib/mailgun/webhooks/webhooks.rb', line 78

def update(domain, action, url = '')
  fail Mailgun::ParameterError('Domain not provided to update webhooks') unless domain
  fail 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['webhook']['urls'] == url && res.to_h['message'] == 'Webhook has been updated'
end