class Mailgun::Webhooks

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

Public Class Methods

new(client = Mailgun::Client.new) click to toggle source

Public creates a new Mailgun::Webhooks instance.

Defaults to Mailgun::Client
# File lib/mailgun/webhooks/webhooks.rb, line 9
def initialize(client = Mailgun::Client.new)
  @client = client
end

Public Instance Methods

add(domain, action, url = '')
Alias for: create
add_all_webhooks(domain, url = '')
Alias for: create_all
add_webhook(domain, action, url = '')
Alias for: create
create(domain, action, url = '') click to toggle source

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

# File lib/mailgun/webhooks/webhooks.rb, line 47
def create(domain, action, url = '')
  res = @client.post("domains/#{domain}/webhooks", id: action, url: url)
  res.to_h['webhook']['url'] == url && res.to_h[message] == 'Webhook has been created'
end
Also aliased as: add, add_webhook
create_all(domain, url = '') click to toggle source

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

# File lib/mailgun/webhooks/webhooks.rb, line 60
def create_all(domain, url = '')
  %w(bounce click deliver drop open spam unsubscribe).each do |action|
    add_webhook domain, action, url
  end
  true
rescue
  false
end
Also aliased as: add_all_webhooks
delete(domain, action)
Alias for: remove
delete_all(domain)
Alias for: remove_all
delete_all_webooks(domain)
Alias for: remove_all
delete_webhook(domain, action)
Alias for: remove
get_webhook_url(domain, action)
Alias for: info
get_webhooks(domain, options = {})
Alias for: list
info(domain, action) click to toggle source

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
# File lib/mailgun/webhooks/webhooks.rb, line 32
def info(domain, action)
  res = @client.get("domains/#{domain}/webhooks/#{action}")
  res.to_h['webhook']['url'] || ''
rescue NoMethodError
  ''
end
Also aliased as: get_webhook_url
list(domain, options = {}) click to toggle source

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

# File lib/mailgun/webhooks/webhooks.rb, line 19
def list(domain, options = {})
  res = @client.get("domains/#{domain}/webhooks", options)
  res.to_h['webhooks']
end
Also aliased as: get_webhooks
remove(domain, action) click to toggle source

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

# File lib/mailgun/webhooks/webhooks.rb, line 76
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
Also aliased as: delete, delete_webhook
remove_all(domain) click to toggle source

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

# File lib/mailgun/webhooks/webhooks.rb, line 91
def remove_all(domain)
  fail Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
  %w(bounce click deliver drop open spam unsubscribe).each do |action|
    delete_webhook domain, action
  end
end
Also aliased as: delete_all, delete_all_webooks