mailgun-ruby 1.4.3 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,8 @@ module Mailgun
4
4
  # A Mailgun::Webhooks object is a simple CRUD interface to Mailgun Webhooks.
5
5
  # Uses Mailgun
6
6
  class Webhooks
7
+ include ApiVersionChecker
8
+
7
9
  ACTIONS = %w[accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed].freeze
8
10
 
9
11
  # Public creates a new Mailgun::Webhooks instance.
@@ -22,7 +24,14 @@ module Mailgun
22
24
  res = @client.get("domains/#{domain}/webhooks", options)
23
25
  res.to_h['webhooks']
24
26
  end
25
- alias get_webhooks list
27
+
28
+ # :nocov:
29
+
30
+ def get_webhooks(domain, options = {})
31
+ warn('`get_webhooks` method will be deprecated in future versions of Mailgun. Please use `list` instead.')
32
+ list(domain, options)
33
+ end
34
+ # :nocov:
26
35
 
27
36
  # Public: Get webook information for a specific action
28
37
  #
@@ -31,13 +40,19 @@ module Mailgun
31
40
  #
32
41
  # Returns a String of the url for the identified webhook or an
33
42
  # empty String if one is not set
34
- def info(domain, action)
43
+ def get(domain, action)
35
44
  res = @client.get("domains/#{domain}/webhooks/#{action}")
36
45
  res.to_h['webhook']['urls'] || ''
37
- rescue NoMethodError
38
- ''
39
46
  end
40
- alias get_webhook_url info
47
+
48
+ # :nocov:
49
+ %i[info get_webhook_url].each do |method|
50
+ define_method(method) do |domain, action|
51
+ warn("`#{method}` method will be deprecated in future versions of Mailgun. Please use `get` instead.")
52
+ get(domain, action)
53
+ end
54
+ end
55
+ # :nocov:
41
56
 
42
57
  # Public: Add webhook
43
58
  #
@@ -50,8 +65,16 @@ module Mailgun
50
65
  res = @client.post("domains/#{domain}/webhooks", id: action, url: url)
51
66
  res.to_h['webhook']['urls'].include?(url) && res.to_h['message'] == 'Webhook has been created'
52
67
  end
53
- alias add create
54
- alias add_webhook create
68
+
69
+ # :nocov:
70
+ %i[add add_webhook].each do |method|
71
+ define_method(method) do |domain, action, url|
72
+ url ||= ''
73
+ warn("`#{method}` method will be deprecated in future versions of Mailgun. Please use `create` instead.")
74
+ create(domain, action, url)
75
+ end
76
+ end
77
+ # :nocov:
55
78
 
56
79
  # Public: Sets all webhooks to the same URL
57
80
  #
@@ -59,15 +82,17 @@ module Mailgun
59
82
  # url - A String of the url to set all webhooks to
60
83
  #
61
84
  # Returns true or false
62
- def create_all(domain, url = '')
63
- ACTIONS.each do |action|
64
- add_webhook domain, action, url
85
+ # :nocov:
86
+ %i[create_all add_all_webhooks].each do |method|
87
+ define_method(method) do |domain|
88
+ warn("`#{method}` method will be deprecated in future versions of Mailgun. Please use `create` instead.")
89
+
90
+ ACTIONS.each do |action|
91
+ create domain, action, url
92
+ end
65
93
  end
66
- true
67
- rescue StandardError
68
- false
69
94
  end
70
- alias add_all_webhooks create_all
95
+ # :nocov:
71
96
 
72
97
  # Public: Update webhook
73
98
  #
@@ -81,9 +106,15 @@ module Mailgun
81
106
  raise Mailgun::ParameterError('Action not provided to identify webhook to update') unless action
82
107
 
83
108
  res = @client.put("domains/#{domain}/webhooks/#{action}", id: action, url: url)
84
- res.to_h['webhook']['urls'] == url && res.to_h['message'] == 'Webhook has been updated'
109
+ res.to_h['message'] == 'Webhook has been updated'
85
110
  end
86
- alias update_webhook update
111
+
112
+ # :nocov:
113
+ def update_webhook(domain, action, url = '')
114
+ warn('`update_webhook` method will be deprecated in future versions of Mailgun. Please use `update` instead.')
115
+ update(domain, action, url)
116
+ end
117
+ # :nocov:
87
118
 
88
119
  # Public: Delete a specific webhook
89
120
  #
@@ -96,25 +127,36 @@ module Mailgun
96
127
  raise Mailgun::ParameterError('Action not provided to identify webhook to remove') unless action
97
128
 
98
129
  @client.delete("domains/#{domain}/webhooks/#{action}").to_h['message'] == 'Webhook has been deleted'
99
- rescue Mailgun::CommunicationError
100
- false
101
130
  end
102
- alias delete remove
103
- alias delete_webhook remove
131
+
132
+ # :nocov:
133
+ %i[delete delete_webhook].each do |method|
134
+ define_method(method) do |domain, action|
135
+ warn("`#{method}` method will be deprecated in future versions of Mailgun. Please use `remove` instead.")
136
+ remove(domain, action)
137
+ end
138
+ end
139
+ # :nocov:
104
140
 
105
141
  # Public: Delete all webhooks for a domain
106
142
  #
107
143
  # domain - A required String of the domain to remove all webhooks for
108
144
  #
109
145
  # Returns a Boolean on the success
110
- def remove_all(domain)
111
- raise Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
146
+ # :nocov:
147
+ %i[remove_all delete_all delete_all_webooks].each do |method|
148
+ define_method(method) do |domain|
149
+ warn("`#{method}` method will be deprecated in future versions of Mailgun. Please use `remove` instead.")
112
150
 
113
- ACTIONS.each do |action|
114
- delete_webhook domain, action
151
+ raise Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
152
+
153
+ ACTIONS.each do |action|
154
+ remove domain, action
155
+ end
115
156
  end
116
157
  end
117
- alias delete_all remove_all
118
- alias delete_all_webooks remove_all
158
+ # :nocov:
159
+
160
+ enforces_api_version 'v3', :list, :get, :create, :update, :remove
119
161
  end
120
162
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'mailgun'
5
+
6
+ vcr_opts = { cassette_name: 'account_webhooks' }
7
+
8
+ describe 'For the webhooks endpoint', order: :defined, vcr: vcr_opts do
9
+ let(:api_version) { 'v1' }
10
+ let(:mg_client) { Mailgun::Client.new(APIKEY, APIHOST, api_version, SSL) }
11
+ let(:mg_obj) { Mailgun::AccountWebhooks.new(mg_client) }
12
+
13
+ it 'creates a webhook' do
14
+ result = mg_obj.create(
15
+ description: 'test',
16
+ event_types: 'accepted',
17
+ url: 'http://example.com/mailgun/events'
18
+ )
19
+
20
+ expect(result).to have_key('webhook_id')
21
+ end
22
+
23
+ it 'gets a webhook' do
24
+ result = mg_obj.get('test')
25
+
26
+ expect(result['url']).to eq('http://example.com/mailgun/events')
27
+ end
28
+
29
+ it 'gets a list of all account webhooks' do
30
+ result = mg_obj.list
31
+
32
+ expect(result[0]['url']).to eq('http://example.com/mailgun/events')
33
+ end
34
+
35
+ it 'updates a webhook' do
36
+ result = mg_obj.update(
37
+ 'test',
38
+ description: 'test2',
39
+ event_types: 'accepted',
40
+ url: 'http://example.com/mailgun/events'
41
+ )
42
+
43
+ expect(result).to be_truthy
44
+ end
45
+
46
+ it 'removes a webhook' do
47
+ result = mg_obj.remove_by_id('test')
48
+
49
+ expect(result).to be_truthy
50
+ end
51
+
52
+ it 'removes all webhooks' do
53
+ result = mg_obj.remove(all: true)
54
+
55
+ expect(result).to be_truthy
56
+ end
57
+ end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'mailgun'
5
+
6
+ vcr_opts = { cassette_name: 'domains' }
7
+
8
+ describe 'DomainKeys', vcr: vcr_opts do
9
+ let(:api_version) { APIVERSION }
10
+ let(:mg_client) { Mailgun::Client.new(APIKEY, APIHOST, api_version, SSL) }
11
+ let(:mg_obj) { Mailgun::DomainKeys.new(mg_client) }
12
+ let(:domain) { 'integration-test.domain.invalid' }
13
+
14
+ describe '#list_domain_keys' do
15
+ let(:api_version) { 'v1' }
16
+
17
+ it 'lists all domain keys' do
18
+ result = mg_obj.list_domain_keys(
19
+ {
20
+ signing_domain: domain
21
+ }
22
+ )
23
+
24
+ expect(result).to include('items')
25
+ expect(result['items'].first['selector']).to eq('k1')
26
+ end
27
+ end
28
+
29
+ describe '#create_domain_key' do
30
+ let(:api_version) { 'v1' }
31
+
32
+ it 'creates a domain key' do
33
+ result = mg_obj.create_domain_key(
34
+ {
35
+ signing_domain: domain,
36
+ selector: 'test'
37
+ }
38
+ )
39
+
40
+ expect(result['signing_domain']).to eq(domain)
41
+ expect(result['selector']).to eq('test')
42
+ end
43
+ end
44
+
45
+ describe '#delete_domain_key' do
46
+ let(:api_version) { 'v1' }
47
+
48
+ it 'deletes a domain key' do
49
+ result = mg_obj.delete_domain_key(
50
+ {
51
+ signing_domain: domain,
52
+ selector: 'test'
53
+ }
54
+ )
55
+
56
+ expect(result['message']).to eq('success')
57
+ end
58
+ end
59
+
60
+ describe '#activate_domain_key' do
61
+ let(:api_version) { 'v4' }
62
+
63
+ it 'activates a domain key' do
64
+ result = mg_obj.activate_domain_key(
65
+ domain,
66
+ 'mailo1'
67
+ )
68
+
69
+ expect(result['message']).to eq('domain key activated')
70
+ end
71
+ end
72
+
73
+ describe '#get_domain_keys' do
74
+ let(:api_version) { 'v4' }
75
+
76
+ it 'lists the domain keys for a specified signing domain' do
77
+ result = mg_obj.get_domain_keys(
78
+ domain
79
+ )
80
+
81
+ expect(result).to include('items')
82
+ expect(result['items'].first['selector']).to eq('mailo1')
83
+ end
84
+ end
85
+
86
+ describe '#deactivate_domain_key' do
87
+ let(:api_version) { 'v4' }
88
+
89
+ it 'deactivates a domain key' do
90
+ result = mg_obj.deactivate_domain_key(
91
+ domain,
92
+ 'tetetet'
93
+ )
94
+
95
+ expect(result['message']).to eq('domain key deactivated')
96
+ end
97
+ end
98
+
99
+ describe '#update_domain_dkim_authority' do
100
+ it 'updates the DKIM authority for a domain' do
101
+ result = mg_obj.update_domain_dkim_authority(
102
+ domain,
103
+ {
104
+ self: true
105
+ }
106
+ )
107
+
108
+ expect(result['message']).to eq('Domain DKIM authority has not been changed')
109
+ end
110
+ end
111
+
112
+ describe '#update_domain_dkim_selector' do
113
+ it 'updates the DKIM selector for a domain' do
114
+ result = mg_obj.update_domain_dkim_selector(
115
+ domain,
116
+ {
117
+ dkim_selector: 'mailo1'
118
+ }
119
+ )
120
+
121
+ expect(result['message']).to eq('DKIM selector changed')
122
+ end
123
+ end
124
+ end
@@ -73,119 +73,6 @@ describe 'For the domains endpoint', vcr: vcr_opts do
73
73
  end
74
74
  end
75
75
 
76
- context 'Domain::Keys methods' do
77
- describe '#list_domain_keys' do
78
- let(:api_version) { 'v1' }
79
-
80
- it 'lists all domain keys' do
81
- result = mg_obj.list_domain_keys(
82
- {
83
- signing_domain: domain
84
- }
85
- )
86
-
87
- expect(result).to include('items')
88
- expect(result['items'].first['selector']).to eq('k1')
89
- end
90
- end
91
-
92
- describe '#create_domain_key' do
93
- let(:api_version) { 'v1' }
94
-
95
- it 'creates a domain key' do
96
- result = mg_obj.create_domain_key(
97
- {
98
- signing_domain: domain,
99
- selector: 'test'
100
- }
101
- )
102
-
103
- expect(result['signing_domain']).to eq(domain)
104
- expect(result['selector']).to eq('test')
105
- end
106
- end
107
-
108
- describe '#delete_domain_key' do
109
- let(:api_version) { 'v1' }
110
-
111
- it 'deletes a domain key' do
112
- result = mg_obj.delete_domain_key(
113
- {
114
- signing_domain: domain,
115
- selector: 'test'
116
- }
117
- )
118
-
119
- expect(result['message']).to eq('success')
120
- end
121
- end
122
-
123
- describe '#activate_domain_key' do
124
- let(:api_version) { 'v4' }
125
-
126
- it 'activates a domain key' do
127
- result = mg_obj.activate_domain_key(
128
- domain,
129
- 'mailo1'
130
- )
131
-
132
- expect(result['message']).to eq('domain key activated')
133
- end
134
- end
135
-
136
- describe '#get_domain_keys' do
137
- let(:api_version) { 'v4' }
138
-
139
- it 'lists the domain keys for a specified signing domain' do
140
- result = mg_obj.get_domain_keys(
141
- domain
142
- )
143
-
144
- expect(result).to include('items')
145
- expect(result['items'].first['selector']).to eq('mailo1')
146
- end
147
- end
148
-
149
- describe '#deactivate_domain_key' do
150
- let(:api_version) { 'v4' }
151
-
152
- it 'deactivates a domain key' do
153
- result = mg_obj.deactivate_domain_key(
154
- domain,
155
- 'tetetet'
156
- )
157
-
158
- expect(result['message']).to eq('domain key deactivated')
159
- end
160
- end
161
-
162
- describe '#update_domain_dkim_authority' do
163
- it 'updates the DKIM authority for a domain' do
164
- result = mg_obj.update_domain_dkim_authority(
165
- domain,
166
- {
167
- self: true
168
- }
169
- )
170
-
171
- expect(result['message']).to eq('Domain DKIM authority has not been changed')
172
- end
173
- end
174
-
175
- describe '#update_domain_dkim_selector' do
176
- it 'updates the DKIM selector for a domain' do
177
- result = mg_obj.update_domain_dkim_selector(
178
- domain,
179
- {
180
- dkim_selector: 'mailo1'
181
- }
182
- )
183
-
184
- expect(result['message']).to eq('DKIM selector changed')
185
- end
186
- end
187
- end
188
-
189
76
  context 'Domain::Tracking methods' do
190
77
  # TODO: add missing:
191
78
  # get_domain_tracking_certificate
@@ -6,55 +6,40 @@ require 'mailgun'
6
6
  vcr_opts = { cassette_name: 'webhooks' }
7
7
 
8
8
  describe 'For the webhooks endpoint', order: :defined, vcr: vcr_opts do
9
- before(:all) do
10
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
11
- @domain = 'DOMAIN.TEST'
12
- @testhook = 'accepted'
13
- @testhookup = 'accepted'
14
- end
9
+ let(:api_version) { 'v3' }
10
+ let(:mg_client) { Mailgun::Client.new(APIKEY, APIHOST, api_version, SSL) }
11
+ let(:mg_obj) { Mailgun::Webhooks.new(mg_client) }
12
+ let(:domain) { 'DOMAIN.TEST' }
13
+ let(:testhook) { 'accepted' }
14
+ let(:testhookup) { 'accepted' }
15
15
 
16
16
  it 'creates a webhook' do
17
- result = @mg_obj.post("domains/#{@domain}/webhooks", { id: @testhook,
18
- url: "http://example.com/mailgun/events/#{@testhook}" })
17
+ result = mg_obj.create(domain, testhook, "http://example.com/mailgun/events/#{testhook}")
19
18
 
20
- result.to_h!
21
- expect(result.body['message']).to eq('Webhook has been created')
22
- expect(result.body['webhook']['urls']).to include("http://example.com/mailgun/events/#{@testhook}")
19
+ expect(result).to be_truthy
23
20
  end
24
21
 
25
- it 'gets a webhook.' do
26
- result = @mg_obj.get("domains/#{@domain}/webhooks/#{@testhook}")
22
+ it 'gets a webhook' do
23
+ result = mg_obj.get(domain, testhook)
27
24
 
28
- result.to_h!
29
- expect(result.body['webhook']['urls']).to include("http://example.com/mailgun/events/#{@testhook}")
25
+ expect(result).to include("http://example.com/mailgun/events/#{testhook}")
30
26
  end
31
27
 
32
- it 'gets a list of all webhooks.' do
33
- result = @mg_obj.get("domains/#{@domain}/webhooks")
28
+ it 'gets a list of all webhooks' do
29
+ result = mg_obj.list(domain)
34
30
 
35
- result.to_h!
36
- expect(result.body['webhooks']['accepted']['urls']).to include("http://example.com/mailgun/events/#{@testhook}")
31
+ expect(result['accepted']['urls'][0]).to include("http://example.com/mailgun/events/#{testhook}")
37
32
  end
38
33
 
39
- it 'updates a webhook.' do
40
- result = @mg_obj.put(
41
- "domains/#{@domain}/webhooks/#{@testhook}",
42
- {
43
- id: @testhook,
44
- url: "http://example.com/mailgun/events/#{@testhookup}"
45
- }
46
- )
47
-
48
- result.to_h!
49
- expect(result.body['message']).to eq('Webhook has been updated')
50
- expect(result.body['webhook']['urls']).to include("http://example.com/mailgun/events/#{@testhookup}")
34
+ it 'updates a webhook' do
35
+ result = mg_obj.update(domain, testhook, "http://example.com/mailgun/events/#{testhookup}")
36
+
37
+ expect(result).to be_truthy
51
38
  end
52
39
 
53
40
  it 'removes a webhook' do
54
- result = @mg_obj.delete("domains/#{@domain}/webhooks/#{@testhook}")
41
+ result = mg_obj.remove(domain, testhook)
55
42
 
56
- result.to_h!
57
- expect(result.body['message']).to eq('Webhook has been deleted')
58
- expect(result.body['webhook']['urls']).to include("http://example.com/mailgun/events/#{@testhookup}")
43
+ expect(result).to be_truthy
59
44
  end
60
45
  end