Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webhooks update method #305

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lib/mailgun/webhooks/webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Mailgun
# A Mailgun::Webhooks object is a simple CRUD interface to Mailgun Webhooks.
# Uses Mailgun
class Webhooks
ACTIONS = %w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).freeze

# Public creates a new Mailgun::Webhooks instance.
# Defaults to Mailgun::Client
Expand Down Expand Up @@ -31,7 +32,7 @@ def list(domain, options = {})
# empty String if one is not set
def info(domain, action)
res = @client.get("domains/#{domain}/webhooks/#{action}")
res.to_h['webhook']['url'] || ''
res.to_h['webhook']['urls'] || ''
rescue NoMethodError
''
end
Expand All @@ -46,7 +47,7 @@ def info(domain, action)
# Returns a Boolean of whether the webhook was created
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'
res.to_h['webhook']['urls'].include?(url) && res.to_h['message'] == 'Webhook has been created'
end
alias_method :add, :create
alias_method :add_webhook, :create
Expand All @@ -58,7 +59,7 @@ def create(domain, action, url = '')
#
# Returns true or false
def create_all(domain, url = '')
%w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).each do |action|
ACTIONS.each do |action|
add_webhook domain, action, url
end
true
Expand All @@ -67,6 +68,21 @@ def create_all(domain, url = '')
end
alias_method :add_all_webhooks, :create_all

# 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
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
alias_method :update_webhook, :update

# Public: Delete a specific webhook
#
# domain - The required String of domain name
Expand All @@ -90,7 +106,7 @@ def remove(domain, action)
# Returns a Boolean on the success
def remove_all(domain)
fail Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
%w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).each do |action|
ACTIONS.each do |action|
delete_webhook domain, action
end
end
Expand Down
20 changes: 10 additions & 10 deletions spec/integration/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@
before(:all) do
@mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
@domain = TESTDOMAIN
@testhook = 'bounce'
@testhookup = 'bounceup'
@testhook = 'accepted'
@testhookup = 'accepted'
end

it 'creates a webhook' do
result = @mg_obj.post("domains/#{@domain}/webhooks", { id: 'bounce',
result = @mg_obj.post("domains/#{@domain}/webhooks", { id: @testhook,
url: "http://example.com/mailgun/events/#{@testhook}" } )

result.to_h!
expect(result.body["message"]).to eq("Webhook has been created")
expect(result.body["webhook"]["url"]).to eq("http://example.com/mailgun/events/#{@testhook}")
expect(result.body["webhook"]["urls"]).to include("http://example.com/mailgun/events/#{@testhook}")
end

it 'gets a webhook.' do
result = @mg_obj.get("domains/#{@domain}/webhooks/#{@testhook}")

result.to_h!
expect(result.body["webhook"]["url"]).to eq("http://example.com/mailgun/events/#{@testhook}")
expect(result.body["webhook"]["urls"]).to include("http://example.com/mailgun/events/#{@testhook}")
end

it 'gets a list of all webhooks.' do
result = @mg_obj.get("domains/#{@domain}/webhooks")

result.to_h!
expect(result.body["webhooks"]["bounce"]["url"]).to eq("http://example.com/mailgun/events/#{@testhook}")
expect(result.body["webhooks"]["accepted"]["urls"]).to include("http://example.com/mailgun/events/#{@testhook}")
end

it 'updates a webhook.' do
result = @mg_obj.put("domains/#{@domain}/webhooks/bounce", {:id => 'bounce',
result = @mg_obj.put("domains/#{@domain}/webhooks/#{@testhook}", {:id => @testhook,
:url => "http://example.com/mailgun/events/#{@testhookup}"})

result.to_h!
expect(result.body["message"]).to eq("Webhook has been updated")
expect(result.body["webhook"]["url"]).to eq("http://example.com/mailgun/events/#{@testhookup}")
expect(result.body["webhook"]["urls"]).to include("http://example.com/mailgun/events/#{@testhookup}")
end

it 'removes a webhook' do
result = @mg_obj.delete("domains/#{@domain}/webhooks/bounce")
result = @mg_obj.delete("domains/#{@domain}/webhooks/#{@testhook}")

result.to_h!
expect(result.body['message']).to eq("Webhook has been deleted")
expect(result.body['webhook']['url']).to eq("http://example.com/mailgun/events/#{@testhookup}")
expect(result.body['webhook']['urls']).to include("http://example.com/mailgun/events/#{@testhookup}")
end

end
Loading