diff --git a/lib/mailgun/helpers/api_version_checker.rb b/lib/mailgun/helpers/api_version_checker.rb index 333d42d..463bf3d 100644 --- a/lib/mailgun/helpers/api_version_checker.rb +++ b/lib/mailgun/helpers/api_version_checker.rb @@ -11,9 +11,9 @@ def requires_api_version(version, *method_names) method_names.each do |method_name| original_method = instance_method(method_name) - define_method(method_name) do |*args, &block| + define_method(method_name) do |*args, **kwargs, &block| warn_unless_api_version(version) - original_method.bind(self).call(*args, &block) + original_method.bind(self).call(*args, **kwargs, &block) end end end @@ -22,9 +22,9 @@ def enforces_api_version(version, *method_names) method_names.each do |method_name| original_method = instance_method(method_name) - define_method(method_name) do |*args, &block| + define_method(method_name) do |*args, **kwargs, &block| require_api_version(version) - original_method.bind(self).call(*args, &block) + original_method.bind(self).call(*args, **kwargs, &block) end end end diff --git a/lib/mailgun/webhooks/account_webhooks.rb b/lib/mailgun/webhooks/account_webhooks.rb index 017b88b..faab41e 100644 --- a/lib/mailgun/webhooks/account_webhooks.rb +++ b/lib/mailgun/webhooks/account_webhooks.rb @@ -25,15 +25,15 @@ def list(webhook_ids = '') end # Public: Create an account-level webhook - # options - [Hash] of - # description - [String] Description for the webhook - # event_types - [String] Event types to subscribe to. Use multiple times to specify multiple event types. - # Maximum of 3 unique URLs per event type. - # url - [String] URL for webhook to be sent to + # + # description - [String] Description for the webhook + # event_types - [String] Event types to subscribe to. Use multiple times to specify multiple event types. + # Maximum of 3 unique URLs per event type. + # url - [String] URL for webhook to be sent to # # Returns the Unique identifier for the webhook - def create(_options = {}) - res = @client.post('webhooks', description: description, event_types: event_types, url: url) + def create(description:, event_types:, url:) + res = @client.post('webhooks', { description: description, event_types: event_types, url: url }) res.to_h end @@ -44,7 +44,7 @@ def create(_options = {}) # all - [Boolean] The required String of the webhook action to delete # # Returns a Boolean of the success - def remove_all(webhook_ids = nil, all: false) + def remove(webhook_ids = nil, all: false) @client.delete('webhooks', { webhook_ids: webhook_ids, all: all }.compact).status == 204 end @@ -60,15 +60,15 @@ def get(webhook_id) # Public: Update an account-level webhook # - # options - [Hash] of - # description - [String] Description for the webhook - # event_types - [String] Event types to subscribe to. Use multiple times to specify multiple event types. - # Maximum of 3 unique URLs per event type. - # url - [String] URL for webhook to be sent to + # description - [String] Description for the webhook + # event_types - [String] Event types to subscribe to. Use multiple times to specify multiple event types. + # Maximum of 3 unique URLs per event type. + # url - [String] URL for webhook to be sent to # # Returns a Boolean of the success - def update(webhook_id, options = {}) - @client.put("webhooks/#{webhook_id}", options).status == 204 + def update(webhook_id, description:, event_types:, url:) + @client.put("webhooks/#{webhook_id}", + { description: description, event_types: event_types, url: url }).status == 204 end # Public: Delete account-level webhook by ID @@ -76,10 +76,10 @@ def update(webhook_id, options = {}) # webhook_id - [String] The webhook ID to delete # # Returns a Boolean of the success - def remove(webhook_id) + def remove_by_id(webhook_id) @client.delete("webhooks/#{webhook_id}").status == 204 end - enforces_api_version 'v1', :list, :create, :remove_all, :get, :update, :remove + enforces_api_version 'v1', :list, :create, :remove, :get, :update, :remove_by_id end end diff --git a/lib/mailgun/webhooks/webhooks.rb b/lib/mailgun/webhooks/webhooks.rb index add5380..8c56166 100644 --- a/lib/mailgun/webhooks/webhooks.rb +++ b/lib/mailgun/webhooks/webhooks.rb @@ -27,9 +27,9 @@ def list(domain, options = {}) # :nocov: - def get_webhooks(domain, _options = {}) + def get_webhooks(domain, options = {}) warn('`get_webhooks` method will be deprecated in future versions of Mailgun. Please use `list` instead.') - list(domain, {}) + list(domain, options) end # :nocov: diff --git a/spec/integration/account_webhooks_spec.rb b/spec/integration/account_webhooks_spec.rb new file mode 100644 index 0000000..9f31576 --- /dev/null +++ b/spec/integration/account_webhooks_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'mailgun' + +vcr_opts = { cassette_name: 'account_webhooks' } + +describe 'For the webhooks endpoint', order: :defined, vcr: vcr_opts do + let(:api_version) { 'v1' } + let(:mg_client) { Mailgun::Client.new(APIKEY, APIHOST, api_version, SSL) } + let(:mg_obj) { Mailgun::AccountWebhooks.new(mg_client) } + + it 'creates a webhook' do + result = mg_obj.create( + description: 'test', + event_types: 'accepted', + url: 'http://example.com/mailgun/events' + ) + + expect(result).to have_key('webhook_id') + end + + it 'gets a webhook' do + result = mg_obj.get('test') + + expect(result['url']).to eq('http://example.com/mailgun/events') + end + + it 'gets a list of all account webhooks' do + result = mg_obj.list + + expect(result[0]['url']).to eq('http://example.com/mailgun/events') + end + + it 'updates a webhook' do + result = mg_obj.update( + 'test', + description: 'test2', + event_types: 'accepted', + url: 'http://example.com/mailgun/events' + ) + + expect(result).to be_truthy + end + + it 'removes a webhook' do + result = mg_obj.remove_by_id('test') + + expect(result).to be_truthy + end + + it 'removes all webhooks' do + result = mg_obj.remove(all: true) + + expect(result).to be_truthy + end +end diff --git a/vcr_cassettes/account_webhooks.yml b/vcr_cassettes/account_webhooks.yml new file mode 100644 index 0000000..4b8a66a --- /dev/null +++ b/vcr_cassettes/account_webhooks.yml @@ -0,0 +1,269 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.mailgun.net/v1/webhooks + body: + encoding: UTF-8 + string: description=test&event_types=accepted&url=http%3A%2F%2Fexample.com%2Fmailgun%2Fevents + headers: + User-Agent: + - mailgun-sdk-ruby/1.4.4 + Accept: + - "*/*" + Authorization: + - Basic xxx + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Origin: + - "*" + Cache-Control: + - no-store + Content-Length: + - '42' + Content-Type: + - application/json; charset=utf-8 + Date: + - Mon, 25 May 2026 09:11:02 GMT + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Mailgun-Key-Id: + - xxx + X-Xss-Protection: + - 1; mode=block + Server-Timing: + - cfReqDur;dur=382.358 + Cf-Team: + - xxx + body: + encoding: UTF-8 + string: '{"webhook_id":"test"} + + ' + recorded_at: Mon, 25 May 2026 09:11:02 GMT +- request: + method: get + uri: https://api.mailgun.net/v1/webhooks?webhook_ids= + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - mailgun-sdk-ruby/1.4.4 + Accept: + - "*/*" + Authorization: + - Basic xxx + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Origin: + - "*" + Cache-Control: + - no-store + Content-Length: + - '183' + Content-Type: + - application/json; charset=utf-8 + Date: + - Mon, 25 May 2026 09:16:27 GMT + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Mailgun-Key-Id: + - xxx + X-Xss-Protection: + - 1; mode=block + Server-Timing: + - cfReqDur;dur=201.477 + Cf-Team: + - xxx + body: + encoding: UTF-8 + string: '{"webhooks":[{"webhook_id":"test","description":"test","url":"http://example.com/mailgun/events","event_types":["accepted"],"created_at":"2026-05-25T09:11:02Z"}]} + + ' + recorded_at: Mon, 25 May 2026 09:16:27 GMT +- request: + method: get + uri: https://api.mailgun.net/v1/webhooks/test + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - mailgun-sdk-ruby/1.4.4 + Accept: + - "*/*" + Authorization: + - Basic xxx + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Origin: + - "*" + Cache-Control: + - no-store + Content-Length: + - '168' + Content-Type: + - application/json; charset=utf-8 + Date: + - Mon, 25 May 2026 09:22:12 GMT + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Mailgun-Key-Id: + - xxx + X-Xss-Protection: + - 1; mode=block + Server-Timing: + - cfReqDur;dur=234.628 + Cf-Team: + - xxx + body: + encoding: UTF-8 + string: '{"webhook_id":"test","description":"test","url":"http://example.com/mailgun/events","event_types":["accepted"],"created_at":"2026-05-25T09:11:02Z"} + + ' + recorded_at: Mon, 25 May 2026 09:22:12 GMT +- request: + method: put + uri: https://api.mailgun.net/v1/webhooks/test + body: + encoding: UTF-8 + string: description=test2&event_types=accepted&url=http%3A%2F%2Fexample.com%2Fmailgun%2Fevents + headers: + User-Agent: + - mailgun-sdk-ruby/1.4.4 + Accept: + - "*/*" + Authorization: + - Basic xxx + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 204 + message: No Content + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Origin: + - "*" + Cache-Control: + - no-store + Date: + - Mon, 25 May 2026 12:03:17 GMT + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Mailgun-Key-Id: + - xxx + X-Xss-Protection: + - 1; mode=block + body: + encoding: UTF-8 + string: '' + recorded_at: Mon, 25 May 2026 12:03:17 GMT +- request: + method: delete + uri: https://api.mailgun.net/v1/webhooks/test + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - mailgun-sdk-ruby/1.4.4 + Accept: + - "*/*" + Authorization: + - Basic xxx + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 204 + message: No Content + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Origin: + - "*" + Cache-Control: + - no-store + Date: + - Mon, 25 May 2026 12:08:15 GMT + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Mailgun-Key-Id: + - xxx + X-Xss-Protection: + - 1; mode=block + body: + encoding: UTF-8 + string: '' + recorded_at: Mon, 25 May 2026 12:08:15 GMT +- request: + method: delete + uri: https://api.mailgun.net/v1/webhooks?all=true + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - mailgun-sdk-ruby/1.4.4 + Accept: + - "*/*" + Authorization: + - Basic xxx + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 204 + message: No Content + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Origin: + - "*" + Cache-Control: + - no-store + Date: + - Mon, 25 May 2026 20:28:16 GMT + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Mailgun-Key-Id: + - xxx + X-Xss-Protection: + - 1; mode=block + Server-Timing: + - cfReqDur;dur=246.503 + Cf-Team: + - xxx + body: + encoding: UTF-8 + string: '' + recorded_at: Mon, 25 May 2026 20:28:16 GMT +recorded_with: VCR 6.4.0