mailgunner 2.6.0 → 3.0.0

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.
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_tags(params = {})
6
- get("/v3/#{escape @domain}/tags", params)
5
+ def get_tags(params = PARAMS)
6
+ get("/v3/#{escape @domain}/tags", query: params)
7
7
  end
8
8
 
9
9
  def get_tag(id)
@@ -15,7 +15,7 @@ module Mailgunner
15
15
  end
16
16
 
17
17
  def get_tag_stats(id, params)
18
- get("/v3/#{escape @domain}/tags/#{escape id}/stats", params)
18
+ get("/v3/#{escape @domain}/tags/#{escape id}/stats", query: params)
19
19
  end
20
20
 
21
21
  def delete_tag(id)
@@ -10,11 +10,11 @@ module Mailgunner
10
10
  get("/v3/domains/#{escape @domain}/webhooks/#{escape id}")
11
11
  end
12
12
 
13
- def add_webhook(attributes = {})
13
+ def add_webhook(attributes = ATTRIBUTES)
14
14
  post("/v3/domains/#{escape @domain}/webhooks", attributes)
15
15
  end
16
16
 
17
- def update_webhook(id, attributes = {})
17
+ def update_webhook(id, attributes = ATTRIBUTES)
18
18
  put("/v3/domains/#{escape @domain}/webhooks/#{escape id}", attributes)
19
19
  end
20
20
 
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Config
5
+ def domain
6
+ @domain ||= default_domain
7
+ end
8
+
9
+ attr_writer :domain
10
+
11
+ def api_key
12
+ @api_key ||= ENV.fetch('MAILGUN_API_KEY')
13
+ end
14
+
15
+ attr_writer :api_key
16
+
17
+ def api_host
18
+ @api_host ||= 'api.mailgun.net'
19
+ end
20
+
21
+ attr_writer :api_host
22
+
23
+ def user_agent
24
+ @user_agent ||= "Ruby/#{RUBY_VERSION} Mailgunner/#{VERSION}"
25
+ end
26
+
27
+ attr_writer :user_agent
28
+
29
+ module NoDomainProvided
30
+ def self.to_s
31
+ raise Error, 'No domain provided'
32
+ end
33
+ end
34
+
35
+ private_constant :NoDomainProvided
36
+
37
+ private
38
+
39
+ def default_domain
40
+ return NoDomainProvided unless ENV.key?('MAILGUN_SMTP_LOGIN')
41
+
42
+ ENV['MAILGUN_SMTP_LOGIN'].to_s.split('@').last
43
+ end
44
+ end
45
+ end
@@ -1,6 +1,7 @@
1
1
  require 'mail/check_delivery_params'
2
2
 
3
3
  module Mailgunner
4
+ # @private
4
5
  class DeliveryMethod
5
6
  attr_accessor :settings
6
7
 
@@ -1,15 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mailgunner
2
- class Error < StandardError; end
4
+ class Error < StandardError
5
+ # @private
6
+ def self.parse(response)
7
+ exception_class = case response
8
+ when Net::HTTPUnauthorized
9
+ AuthenticationError
10
+ when Net::HTTPClientError
11
+ ClientError
12
+ when Net::HTTPServerError
13
+ ServerError
14
+ else
15
+ Error
16
+ end
17
+
18
+ message = if response['Content-Type']&.start_with?('application/json')
19
+ JSON.parse(response.body)['message']
20
+ end
21
+
22
+ message ||= "HTTP #{response.code} response from Mailgun API"
23
+
24
+ exception_class.new(message)
25
+ end
26
+ end
3
27
 
4
28
  class ClientError < Error; end
5
29
 
6
30
  class AuthenticationError < ClientError; end
7
31
 
8
32
  class ServerError < Error; end
9
-
10
- module NoDomainProvided
11
- def self.to_s
12
- raise Error, 'No domain provided'
13
- end
14
- end
15
33
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require 'cgi'
3
+
4
+ module Mailgunner
5
+ module Params
6
+ def self.encode(params)
7
+ params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
8
+ end
9
+
10
+ def self.escape(component)
11
+ CGI.escape(component.to_s)
12
+ end
13
+ end
14
+
15
+ private_constant :Params
16
+ end
@@ -1,4 +1,5 @@
1
1
  module Mailgunner
2
+ # @private
2
3
  class Railtie < Rails::Railtie
3
4
  config.before_initialize do
4
5
  require 'mailgunner/delivery_method'
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Struct
5
+ def initialize(hash = nil)
6
+ @hash = hash || {}
7
+ end
8
+
9
+ def [](key)
10
+ @hash[key.to_s]
11
+ end
12
+
13
+ def []=(key, value)
14
+ @hash[key] = value
15
+ end
16
+
17
+ def to_h
18
+ @hash
19
+ end
20
+
21
+ def ==(other)
22
+ other.is_a?(self.class) && other.to_h == @hash
23
+ end
24
+
25
+ def respond_to_missing?(name, include_all)
26
+ @hash.key?(name.to_s)
27
+ end
28
+
29
+ def method_missing(name, *args)
30
+ return @hash[name.to_s] if @hash.key?(name.to_s)
31
+
32
+ super
33
+ end
34
+
35
+ def pretty_print(q)
36
+ q.object_address_group(self) do
37
+ q.seplist(@hash, lambda { q.text ',' }) do |key, value|
38
+ q.breakable
39
+ q.text key.to_s
40
+ q.text '='
41
+ q.group(1) {
42
+ q.breakable ''
43
+ q.pp value
44
+ }
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module Mailgunner
2
- VERSION = '2.6.0'
2
+ VERSION = '3.0.0'
3
3
  end
@@ -7,12 +7,14 @@ Gem::Specification.new do |s|
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ['Tim Craft']
9
9
  s.email = ['mail@timcraft.com']
10
- s.homepage = 'http://github.com/readysteady/mailgunner'
10
+ s.homepage = 'https://github.com/readysteady/mailgunner'
11
11
  s.description = 'Ruby client for the Mailgun API'
12
12
  s.summary = 'Ruby client for the Mailgun API'
13
- s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE.txt README.md mailgunner.gemspec)
14
- s.required_ruby_version = '>= 1.9.3'
15
- s.add_development_dependency('rake', '>= 12')
13
+ s.files = Dir.glob('lib/**/*.rb') + %w(LICENSE.txt README.md mailgunner.gemspec)
14
+ s.required_ruby_version = '>= 2.5.0'
15
+ s.add_development_dependency('rake', '~> 12')
16
+ s.add_development_dependency('yard', '~> 0.9')
17
+ s.add_development_dependency('redcarpet', '~> 3') unless RUBY_PLATFORM == 'java'
16
18
  s.add_development_dependency('webmock', '~> 3')
17
19
  s.add_development_dependency('mail', '~> 2')
18
20
  s.add_development_dependency('actionmailer', '~> 5')
metadata CHANGED
@@ -1,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailgunner
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Craft
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-16 00:00:00.000000000 Z
11
+ date: 2019-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '12'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redcarpet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: webmock
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -102,14 +130,15 @@ files:
102
130
  - lib/mailgunner/client/suppressions.rb
103
131
  - lib/mailgunner/client/tags.rb
104
132
  - lib/mailgunner/client/webhooks.rb
133
+ - lib/mailgunner/config.rb
105
134
  - lib/mailgunner/delivery_method.rb
106
135
  - lib/mailgunner/errors.rb
136
+ - lib/mailgunner/params.rb
107
137
  - lib/mailgunner/railtie.rb
138
+ - lib/mailgunner/struct.rb
108
139
  - lib/mailgunner/version.rb
109
140
  - mailgunner.gemspec
110
- - spec/mailgunner_delivery_method_spec.rb
111
- - spec/mailgunner_spec.rb
112
- homepage: http://github.com/readysteady/mailgunner
141
+ homepage: https://github.com/readysteady/mailgunner
113
142
  licenses:
114
143
  - LGPL-3.0
115
144
  metadata:
@@ -125,15 +154,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
154
  requirements:
126
155
  - - ">="
127
156
  - !ruby/object:Gem::Version
128
- version: 1.9.3
157
+ version: 2.5.0
129
158
  required_rubygems_version: !ruby/object:Gem::Requirement
130
159
  requirements:
131
160
  - - ">="
132
161
  - !ruby/object:Gem::Version
133
162
  version: '0'
134
163
  requirements: []
135
- rubyforge_project:
136
- rubygems_version: 2.7.6
164
+ rubygems_version: 3.0.3
137
165
  signing_key:
138
166
  specification_version: 4
139
167
  summary: Ruby client for the Mailgun API
@@ -1,37 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'webmock/minitest'
3
- require 'action_mailer'
4
- require 'mailgunner'
5
- require 'mailgunner/delivery_method'
6
-
7
- class ExampleMailer < ActionMailer::Base
8
- default from: 'testing@localhost'
9
-
10
- def registration_confirmation(user)
11
- mail to: user[:email], subject: 'Welcome!', body: 'Hello!'
12
- end
13
- end
14
-
15
- describe 'Mailgunner::DeliveryMethod' do
16
- before do
17
- @api_key = 'xxx'
18
-
19
- @domain = 'samples.mailgun.org'
20
-
21
- @base_url = 'https://api.mailgun.net/v3'
22
-
23
- @auth = ['api', @api_key]
24
-
25
- @address = 'user@example.com'
26
-
27
- ActionMailer::Base.delivery_method = :mailgun
28
-
29
- ActionMailer::Base.mailgun_settings = {api_key: @api_key, domain: @domain}
30
- end
31
-
32
- it 'delivers the mail to mailgun in mime format' do
33
- stub_request(:post, "#@base_url/#@domain/messages.mime").with(basic_auth: @auth)
34
-
35
- ExampleMailer.registration_confirmation(email: @address).deliver_now
36
- end
37
- end
@@ -1,747 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'webmock/minitest'
3
- require 'mocha/setup'
4
- require 'mailgunner'
5
- require 'json'
6
- require 'mail'
7
-
8
- describe 'Mailgunner::Client' do
9
- before do
10
- @domain = 'samples.mailgun.org'
11
-
12
- @api_key = 'xxx'
13
-
14
- @base_url = 'https://@api.mailgun.net/v3'
15
-
16
- @json_response_object = {'key' => 'value'}
17
-
18
- @client = Mailgunner::Client.new(domain: @domain, api_key: @api_key)
19
-
20
- @address = 'user@example.com'
21
-
22
- @encoded_address = 'user%40example.com'
23
-
24
- @login = 'bob.bar'
25
-
26
- @id = 'idxxx'
27
- end
28
-
29
- def stub(http_method, url, body: nil, headers: nil)
30
- headers ||= {}
31
- headers['User-Agent'] = /\ARuby\/\d+\.\d+\.\d+ Mailgunner\/\d+\.\d+\.\d+\z/
32
-
33
- params = {basic_auth: ['api', @api_key]}
34
- params[:headers] = headers
35
- params[:body] = body if body
36
-
37
- response_headers = {'Content-Type' => 'application/json;charset=utf-8'}
38
- response_body = '{"key":"value"}'
39
-
40
- stub_request(http_method, url).with(params).to_return(headers: response_headers, body: response_body)
41
- end
42
-
43
- describe 'http method' do
44
- it 'returns a net http object' do
45
- @client.http.must_be_instance_of(Net::HTTP)
46
- end
47
- end
48
-
49
- describe 'http object' do
50
- it 'uses ssl' do
51
- @client.http.use_ssl?.must_equal(true)
52
- end
53
-
54
- it 'uses the address from the api_host option' do
55
- api_host = 'api.eu.mailgun.net'
56
-
57
- @client = Mailgunner::Client.new(api_key: @api_key, api_host: api_host)
58
-
59
- @client.http.address.must_equal(api_host)
60
- end
61
- end
62
-
63
- describe 'domain method' do
64
- it 'returns the value passed to the constructor' do
65
- @client.domain.must_equal(@domain)
66
- end
67
-
68
- it 'defaults to the domain in the MAILGUN_SMTP_LOGIN environment variable' do
69
- ENV['MAILGUN_SMTP_LOGIN'] = 'postmaster@samples.mailgun.org'
70
-
71
- Mailgunner::Client.new(api_key: @api_key).domain.must_equal(@domain)
72
-
73
- ENV.delete('MAILGUN_SMTP_LOGIN')
74
- end
75
- end
76
-
77
- describe 'api_key method' do
78
- it 'returns the value passed to the constructor' do
79
- @client.api_key.must_equal(@api_key)
80
- end
81
-
82
- it 'defaults to the value of MAILGUN_API_KEY environment variable' do
83
- ENV['MAILGUN_API_KEY'] = @api_key
84
-
85
- Mailgunner::Client.new(domain: @domain).api_key.must_equal(@api_key)
86
-
87
- ENV.delete('MAILGUN_API_KEY')
88
- end
89
- end
90
-
91
- describe 'validate_address method' do
92
- it 'calls the address validate resource with the given email address and returns the response object' do
93
- stub(:get, "#@base_url/address/validate?address=#@encoded_address")
94
-
95
- @client.validate_address(@address).must_equal(@json_response_object)
96
- end
97
- end
98
-
99
- describe 'parse_addresses method' do
100
- it 'calls the address parse resource with the given email addresses and returns the response object' do
101
- stub(:get, "#@base_url/address/parse?addresses=bob%40example.com%2Ceve%40example.com")
102
-
103
- @client.parse_addresses(['bob@example.com', 'eve@example.com']).must_equal(@json_response_object)
104
- end
105
- end
106
-
107
- describe 'get_message method' do
108
- it 'fetches the domain message resource with the given id and returns the response object' do
109
- stub(:get, "#@base_url/domains/#@domain/messages/#@id")
110
-
111
- @client.get_message(@id).must_equal(@json_response_object)
112
- end
113
- end
114
-
115
- describe 'get_mime_message method' do
116
- it 'fetches the domain message resource with the given key and returns the response object' do
117
- stub(:get, "#@base_url/domains/#@domain/messages/#@id", headers: {'Accept' => 'message/rfc2822'})
118
-
119
- @client.get_mime_message(@id).must_equal(@json_response_object)
120
- end
121
- end
122
-
123
- describe 'send_message method' do
124
- it 'posts to the domain messages resource and returns the response object' do
125
- stub(:post, "#@base_url/#@domain/messages", body: "to=#@encoded_address")
126
-
127
- @client.send_message({to: @address}).must_equal(@json_response_object)
128
- end
129
-
130
- it 'raises an exception if the domain is not provided' do
131
- @client = Mailgunner::Client.new(api_key: @api_key)
132
-
133
- exception = proc { @client.send_message({}) }.must_raise(Mailgunner::Error)
134
- exception.message.must_include('No domain provided')
135
- end
136
-
137
- it 'encodes the message attributes as multipart form data when sending attachments' do
138
- # TODO
139
- end
140
- end
141
-
142
- describe 'send_mime method' do
143
- before do
144
- @mail = Mail.new({
145
- to: 'alice@example.com',
146
- from: 'bob@example.com',
147
- subject: 'Test email',
148
- body: 'This is a test email'
149
- })
150
- end
151
-
152
- it 'posts to the domain messages resource and returns the response object' do
153
- stub(:post, "#@base_url/#@domain/messages.mime")
154
-
155
- @client.send_mime(@mail).must_equal(@json_response_object)
156
- end
157
-
158
- it 'includes all recipients of the message' do
159
- @mail.cc = 'carol@example.com'
160
- @mail.bcc = 'dave@example.com'
161
-
162
- stub(:post, "#@base_url/#@domain/messages.mime")
163
-
164
- recipients = 'alice@example.com,carol@example.com,dave@example.com'
165
-
166
- Net::HTTP::Post.any_instance.expects(:set_form).with(includes(['to', recipients]), 'multipart/form-data')
167
-
168
- @client.send_mime(@mail)
169
- end
170
- end
171
-
172
- describe 'delete_message method' do
173
- it 'deletes the domain message resource with the given key and returns the response object' do
174
- stub(:delete, "#@base_url/domains/#@domain/messages/#@id")
175
-
176
- @client.delete_message(@id).must_equal(@json_response_object)
177
- end
178
- end
179
-
180
- describe 'get_domains method' do
181
- it 'fetches the domains resource and returns the response object' do
182
- stub(:get, "#@base_url/domains")
183
-
184
- @client.get_domains.must_equal(@json_response_object)
185
- end
186
- end
187
-
188
- describe 'get_domain method' do
189
- it 'fetches the domain resource and returns the response object' do
190
- stub(:get, "#@base_url/domains/#@domain")
191
-
192
- @client.get_domain(@domain).must_equal(@json_response_object)
193
- end
194
- end
195
-
196
- describe 'add_domain method' do
197
- it 'posts to the domains resource and returns the response object' do
198
- stub(:post, "#@base_url/domains", body: "name=#@domain")
199
-
200
- @client.add_domain({name: @domain}).must_equal(@json_response_object)
201
- end
202
- end
203
-
204
- describe 'delete_domain method' do
205
- it 'deletes the domain resource with the given name and returns the response object' do
206
- stub(:delete, "#@base_url/domains/#@domain")
207
-
208
- @client.delete_domain(@domain).must_equal(@json_response_object)
209
- end
210
- end
211
-
212
- describe 'get_credentials method' do
213
- it 'fetches the domain credentials resource and returns the response object' do
214
- stub(:get, "#@base_url/domains/#@domain/credentials")
215
-
216
- @client.get_credentials.must_equal(@json_response_object)
217
- end
218
- end
219
-
220
- describe 'add_credentials method' do
221
- it 'posts to the domain credentials resource and returns the response object' do
222
- stub(:post, "#@base_url/domains/#@domain/credentials", body: "login=#@login")
223
-
224
- @client.add_credentials(login: @login).must_equal(@json_response_object)
225
- end
226
- end
227
-
228
- describe 'update_credentials method' do
229
- it 'updates the domain credentials resource with the given login and returns the response object' do
230
- stub(:put, "#@base_url/domains/#@domain/credentials/#@login", body: 'password=secret')
231
-
232
- @client.update_credentials(@login, {password: 'secret'}).must_equal(@json_response_object)
233
- end
234
- end
235
-
236
- describe 'delete_credentials method' do
237
- it 'deletes the domain credentials resource with the given login and returns the response object' do
238
- stub(:delete, "#@base_url/domains/#@domain/credentials/#@login")
239
-
240
- @client.delete_credentials(@login).must_equal(@json_response_object)
241
- end
242
- end
243
-
244
- describe 'get_connection_settings method' do
245
- it 'fetches the domain connection settings resource and returns the response object' do
246
- stub(:get, "#@base_url/domains/#@domain/connection")
247
-
248
- @client.get_connection_settings.must_equal(@json_response_object)
249
- end
250
- end
251
-
252
- describe 'update_connection_settings method' do
253
- it 'updates the domain connection settings resource and returns the response object' do
254
- stub(:put, "#@base_url/domains/#@domain/connection", body: 'require_tls=true')
255
-
256
- @client.update_connection_settings({require_tls: true}).must_equal(@json_response_object)
257
- end
258
- end
259
-
260
- describe 'get_unsubscribes method' do
261
- it 'fetches the domain unsubscribes resource and returns the response object' do
262
- stub(:get, "#@base_url/#@domain/unsubscribes")
263
-
264
- @client.get_unsubscribes.must_equal(@json_response_object)
265
- end
266
-
267
- it 'encodes skip and limit parameters' do
268
- stub(:get, "#@base_url/#@domain/unsubscribes?skip=1&limit=2")
269
-
270
- @client.get_unsubscribes(skip: 1, limit: 2)
271
- end
272
- end
273
-
274
- describe 'get_unsubscribe method' do
275
- it 'fetches the unsubscribe resource with the given address and returns the response object' do
276
- stub(:get, "#@base_url/#@domain/unsubscribes/#@encoded_address")
277
-
278
- @client.get_unsubscribe(@address).must_equal(@json_response_object)
279
- end
280
- end
281
-
282
- describe 'delete_unsubscribe method' do
283
- it 'deletes the domain unsubscribe resource with the given address and returns the response object' do
284
- stub(:delete, "#@base_url/#@domain/unsubscribes/#@encoded_address")
285
-
286
- @client.delete_unsubscribe(@address).must_equal(@json_response_object)
287
- end
288
- end
289
-
290
- describe 'add_unsubscribe method' do
291
- it 'posts to the domain unsubscribes resource and returns the response object' do
292
- stub(:post, "#@base_url/#@domain/unsubscribes", body: "address=#@encoded_address")
293
-
294
- @client.add_unsubscribe({address: @address}).must_equal(@json_response_object)
295
- end
296
- end
297
-
298
- describe 'get_complaints method' do
299
- it 'fetches the domain complaints resource and returns the response object' do
300
- stub(:get, "#@base_url/#@domain/complaints")
301
-
302
- @client.get_complaints.must_equal(@json_response_object)
303
- end
304
-
305
- it 'encodes skip and limit parameters' do
306
- stub(:get, "#@base_url/#@domain/complaints?skip=1&limit=2")
307
-
308
- @client.get_complaints(skip: 1, limit: 2)
309
- end
310
- end
311
-
312
- describe 'get_complaint method' do
313
- it 'fetches the complaint resource with the given address and returns the response object' do
314
- stub(:get, "#@base_url/#@domain/complaints/#@encoded_address")
315
-
316
- @client.get_complaint(@address).must_equal(@json_response_object)
317
- end
318
- end
319
-
320
- describe 'add_complaint method' do
321
- it 'posts to the domain complaints resource and returns the response object' do
322
- stub(:post, "#@base_url/#@domain/complaints", body: "address=#@encoded_address")
323
-
324
- @client.add_complaint({address: @address}).must_equal(@json_response_object)
325
- end
326
- end
327
-
328
- describe 'delete_complaint method' do
329
- it 'deletes the domain complaint resource with the given address and returns the response object' do
330
- stub(:delete, "#@base_url/#@domain/complaints/#@encoded_address")
331
-
332
- @client.delete_complaint(@address).must_equal(@json_response_object)
333
- end
334
- end
335
-
336
- describe 'get_bounces method' do
337
- it 'fetches the domain bounces resource and returns the response object' do
338
- stub(:get, "#@base_url/#@domain/bounces")
339
-
340
- @client.get_bounces.must_equal(@json_response_object)
341
- end
342
-
343
- it 'encodes skip and limit parameters' do
344
- stub(:get, "#@base_url/#@domain/bounces?skip=1&limit=2")
345
-
346
- @client.get_bounces(skip: 1, limit: 2)
347
- end
348
- end
349
-
350
- describe 'get_bounce method' do
351
- it 'fetches the bounce resource with the given address and returns the response object' do
352
- stub(:get, "#@base_url/#@domain/bounces/#@encoded_address")
353
-
354
- @client.get_bounce(@address).must_equal(@json_response_object)
355
- end
356
- end
357
-
358
- describe 'add_bounce method' do
359
- it 'posts to the domain bounces resource and returns the response object' do
360
- stub(:post, "#@base_url/#@domain/bounces", body: "address=#@encoded_address")
361
-
362
- @client.add_bounce({address: @address}).must_equal(@json_response_object)
363
- end
364
- end
365
-
366
- describe 'delete_bounce method' do
367
- it 'deletes the domain bounce resource with the given address and returns the response object' do
368
- stub(:delete, "#@base_url/#@domain/bounces/#@encoded_address")
369
-
370
- @client.delete_bounce(@address).must_equal(@json_response_object)
371
- end
372
- end
373
-
374
- describe 'delete_bounces method' do
375
- it 'deletes the domain bounces resource and returns the response object' do
376
- stub(:delete, "#@base_url/#@domain/bounces")
377
-
378
- @client.delete_bounces.must_equal(@json_response_object)
379
- end
380
- end
381
-
382
- describe 'get_stats method' do
383
- before do
384
- Kernel.stubs(:warn)
385
- end
386
-
387
- it 'fetches the domain stats resource and returns the response object' do
388
- stub(:get, "#@base_url/#@domain/stats")
389
-
390
- @client.get_stats.must_equal(@json_response_object)
391
- end
392
-
393
- it 'encodes skip and limit parameters' do
394
- stub(:get, "#@base_url/#@domain/stats?skip=1&limit=2")
395
-
396
- @client.get_stats(skip: 1, limit: 2)
397
- end
398
-
399
- it 'encodes an event parameter with multiple values' do
400
- WebMock::Config.instance.query_values_notation = :flat_array
401
-
402
- stub(:get, "#@base_url/#@domain/stats?event=opened&event=sent")
403
-
404
- @client.get_stats(event: %w(sent opened))
405
-
406
- WebMock::Config.instance.query_values_notation = nil
407
- end
408
-
409
- it 'emits a deprecation warning' do
410
- stub(:get, "#@base_url/#@domain/stats")
411
-
412
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#get_stats is deprecated/))
413
-
414
- @client.get_stats
415
- end
416
- end
417
-
418
- describe 'get_total_stats method' do
419
- it 'fetches the domain total stats resource and returns the response object' do
420
- stub(:get, "#@base_url/#@domain/stats/total?event=delivered")
421
-
422
- @client.get_total_stats(event: 'delivered').must_equal(@json_response_object)
423
- end
424
-
425
- it 'encodes optional parameters' do
426
- stub(:get, "#@base_url/#@domain/stats/total?event=delivered&resolution=hour")
427
-
428
- @client.get_total_stats(event: 'delivered', resolution: 'hour')
429
- end
430
-
431
- it 'encodes an event parameter with multiple values' do
432
- WebMock::Config.instance.query_values_notation = :flat_array
433
-
434
- stub(:get, "#@base_url/#@domain/stats/total?event=delivered&event=accepted")
435
-
436
- @client.get_total_stats(event: %w(accepted delivered))
437
-
438
- WebMock::Config.instance.query_values_notation = nil
439
- end
440
- end
441
-
442
- describe 'get_events method' do
443
- it 'fetches the domain events resource and returns the response object' do
444
- stub(:get, "#@base_url/#@domain/events")
445
-
446
- @client.get_events.must_equal(@json_response_object)
447
- end
448
-
449
- it 'encodes optional parameters' do
450
- stub(:get, "#@base_url/#@domain/events?event=accepted&limit=10")
451
-
452
- @client.get_events(event: 'accepted', limit: 10)
453
- end
454
- end
455
-
456
- describe 'get_tags method' do
457
- it 'fetches the domain tags resource and returns the response object' do
458
- stub(:get, "#@base_url/#@domain/tags")
459
-
460
- @client.get_tags.must_equal(@json_response_object)
461
- end
462
-
463
- it 'encodes optional limit parameter' do
464
- stub(:get, "#@base_url/#@domain/tags?limit=2")
465
-
466
- @client.get_tags(limit: 2)
467
- end
468
- end
469
-
470
- describe 'get_tag method' do
471
- it 'fetches the domain tag resource with the given id and returns the response object' do
472
- stub(:get, "#@base_url/#@domain/tags/#@id")
473
-
474
- @client.get_tag(@id).must_equal(@json_response_object)
475
- end
476
- end
477
-
478
- describe 'update_tag method' do
479
- it 'updates the domain tag resource with the given id and returns the response object' do
480
- stub(:put, "#@base_url/#@domain/tags/#@id", body: 'description=Tag+description')
481
-
482
- @client.update_tag(@id, {description: 'Tag description'}).must_equal(@json_response_object)
483
- end
484
- end
485
-
486
- describe 'get_tag_stats method' do
487
- it 'fetches the domain tag stats resource with the given id and returns the response object' do
488
- stub(:get, "#@base_url/#@domain/tags/#@id/stats?event=accepted")
489
-
490
- @client.get_tag_stats(@id, event: 'accepted').must_equal(@json_response_object)
491
- end
492
- end
493
-
494
- describe 'delete_tag method' do
495
- it 'deletes the domain tag resource with the given id and returns the response object' do
496
- stub(:delete, "#@base_url/#@domain/tags/#@id")
497
-
498
- @client.delete_tag(@id).must_equal(@json_response_object)
499
- end
500
- end
501
-
502
- describe 'get_routes method' do
503
- it 'fetches the routes resource and returns the response object' do
504
- stub(:get, "#@base_url/routes")
505
-
506
- @client.get_routes.must_equal(@json_response_object)
507
- end
508
-
509
- it 'encodes skip and limit parameters' do
510
- stub(:get, "#@base_url/routes?skip=1&limit=2")
511
-
512
- @client.get_routes(skip: 1, limit: 2)
513
- end
514
- end
515
-
516
- describe 'get_route method' do
517
- it 'fetches the route resource with the given id and returns the response object' do
518
- stub(:get, "#@base_url/routes/#@id")
519
-
520
- @client.get_route(@id).must_equal(@json_response_object)
521
- end
522
- end
523
-
524
- describe 'add_route method' do
525
- it 'posts to the routes resource and returns the response object' do
526
- stub(:post, "#@base_url/routes", body: 'description=Example+route&priority=1')
527
-
528
- @client.add_route({description: 'Example route', priority: 1}).must_equal(@json_response_object)
529
- end
530
- end
531
-
532
- describe 'update_route method' do
533
- it 'updates the route resource with the given id and returns the response object' do
534
- stub(:put, "#@base_url/routes/#@id", body: 'priority=10')
535
-
536
- @client.update_route(@id, {priority: 10}).must_equal(@json_response_object)
537
- end
538
- end
539
-
540
- describe 'delete_route method' do
541
- it 'deletes the route resource with the given id and returns the response object' do
542
- stub(:delete, "#@base_url/routes/#@id")
543
-
544
- @client.delete_route(@id).must_equal(@json_response_object)
545
- end
546
- end
547
-
548
- describe 'get_webhooks method' do
549
- it 'fetches the domain webhooks resource and returns the response object' do
550
- stub(:get, "#@base_url/domains/#@domain/webhooks")
551
-
552
- @client.get_webhooks.must_equal(@json_response_object)
553
- end
554
- end
555
-
556
- describe 'get_webhook method' do
557
- it 'fetches the domain webhook resource with the given id and returns the response object' do
558
- stub(:get, "#@base_url/domains/#@domain/webhooks/#@id")
559
-
560
- @client.get_webhook(@id).must_equal(@json_response_object)
561
- end
562
- end
563
-
564
- describe 'add_webhook method' do
565
- it 'posts to the domain webhooks resource and returns the response object' do
566
- attributes = {id: @id, url: 'http://example.com/webhook'}
567
-
568
- stub(:post, "#@base_url/domains/#@domain/webhooks", body: attributes)
569
-
570
- @client.add_webhook(attributes).must_equal(@json_response_object)
571
- end
572
- end
573
-
574
- describe 'update_webhook method' do
575
- it 'updates the domain webhook resource with the given id and returns the response object' do
576
- attributes = {url: 'http://example.com/webhook'}
577
-
578
- stub(:put, "#@base_url/domains/#@domain/webhooks/#@id", body: attributes)
579
-
580
- @client.update_webhook(@id, attributes).must_equal(@json_response_object)
581
- end
582
- end
583
-
584
- describe 'delete_webhook method' do
585
- it 'deletes the domain webhook resource with the given address and returns the response object' do
586
- stub(:delete, "#@base_url/domains/#@domain/webhooks/#@id")
587
-
588
- @client.delete_webhook(@id).must_equal(@json_response_object)
589
- end
590
- end
591
-
592
- describe 'get_all_ips method' do
593
- it 'fetches the ips resource and returns the response object' do
594
- stub(:get, "#@base_url/ips")
595
-
596
- @client.get_all_ips.must_equal(@json_response_object)
597
- end
598
- end
599
-
600
- describe 'get_ip method' do
601
- it 'fetches the ip resource with the given address and returns the response object' do
602
- address = '127.0.0.1'
603
-
604
- stub(:get, "#@base_url/ips/#{address}")
605
-
606
- @client.get_ip(address).must_equal(@json_response_object)
607
- end
608
- end
609
-
610
- describe 'get_ips method' do
611
- it 'fetches the domain ips resource and returns the response object' do
612
- stub(:get, "#@base_url/domains/#@domain/ips")
613
-
614
- @client.get_ips.must_equal(@json_response_object)
615
- end
616
- end
617
-
618
- describe 'add_ip method' do
619
- it 'posts to the domain ips resource and returns the response object' do
620
- address = '127.0.0.1'
621
-
622
- stub(:post, "#@base_url/domains/#@domain/ips", body: {ip: address})
623
-
624
- @client.add_ip(address).must_equal(@json_response_object)
625
- end
626
- end
627
-
628
- describe 'delete_ip method' do
629
- it 'deletes the domain ip resource with the given address and returns the response object' do
630
- address = '127.0.0.1'
631
-
632
- stub(:delete, "#@base_url/domains/#@domain/ips/#{address}")
633
-
634
- @client.delete_ip(address).must_equal(@json_response_object)
635
- end
636
- end
637
-
638
- describe 'get_lists method' do
639
- it 'fetches the lists resource and returns the response object' do
640
- stub(:get, "#@base_url/lists")
641
-
642
- @client.get_lists.must_equal(@json_response_object)
643
- end
644
-
645
- it 'encodes skip and limit parameters' do
646
- stub(:get, "#@base_url/lists?skip=1&limit=2")
647
-
648
- @client.get_lists(skip: 1, limit: 2)
649
- end
650
- end
651
-
652
- describe 'get_list method' do
653
- it 'fetches the list resource with the given address and returns the response object' do
654
- stub(:get, "#@base_url/lists/developers%40mailgun.net")
655
-
656
- @client.get_list('developers@mailgun.net').must_equal(@json_response_object)
657
- end
658
- end
659
-
660
- describe 'add_list method' do
661
- it 'posts to the lists resource and returns the response object' do
662
- stub(:post, "#@base_url/lists", body: 'address=developers%40mailgun.net')
663
-
664
- @client.add_list({address: 'developers@mailgun.net'}).must_equal(@json_response_object)
665
- end
666
- end
667
-
668
- describe 'update_list method' do
669
- it 'updates the list resource and returns the response object' do
670
- stub(:put, "#@base_url/lists/developers%40mailgun.net", body: 'name=Example+list')
671
-
672
- @client.update_list('developers@mailgun.net', {name: 'Example list'}).must_equal(@json_response_object)
673
- end
674
- end
675
-
676
- describe 'delete_list method' do
677
- it 'deletes the list resource with the given address and returns the response object' do
678
- stub(:delete, "#@base_url/lists/developers%40mailgun.net")
679
-
680
- @client.delete_list('developers@mailgun.net').must_equal(@json_response_object)
681
- end
682
- end
683
-
684
- describe 'get_list_members method' do
685
- it 'fetches the list members resource and returns the response object' do
686
- stub(:get, "#@base_url/lists/developers%40mailgun.net/members")
687
-
688
- @client.get_list_members('developers@mailgun.net').must_equal(@json_response_object)
689
- end
690
-
691
- it 'encodes skip and limit parameters' do
692
- stub(:get, "#@base_url/lists/developers%40mailgun.net/members?skip=1&limit=2")
693
-
694
- @client.get_list_members('developers@mailgun.net', skip: 1, limit: 2)
695
- end
696
- end
697
-
698
- describe 'get_list_member method' do
699
- it 'fetches the list member resource with the given address and returns the response object' do
700
- stub(:get, "#@base_url/lists/developers%40mailgun.net/members/#@encoded_address")
701
-
702
- @client.get_list_member('developers@mailgun.net', @address).must_equal(@json_response_object)
703
- end
704
- end
705
-
706
- describe 'add_list_member method' do
707
- it 'posts to the list members resource and returns the response object' do
708
- stub(:post, "#@base_url/lists/developers%40mailgun.net/members", body: "address=#@encoded_address")
709
-
710
- @client.add_list_member('developers@mailgun.net', {address: @address}).must_equal(@json_response_object)
711
- end
712
- end
713
-
714
- describe 'update_list_member method' do
715
- it 'updates the list member resource with the given address and returns the response object' do
716
- stub(:put, "#@base_url/lists/developers%40mailgun.net/members/#@encoded_address", body: 'subscribed=no')
717
-
718
- @client.update_list_member('developers@mailgun.net', @address, {subscribed: 'no'}).must_equal(@json_response_object)
719
- end
720
- end
721
-
722
- describe 'delete_list_member method' do
723
- it 'deletes the list member resource with the given address and returns the response object' do
724
- stub(:delete, "#@base_url/lists/developers%40mailgun.net/members/#@encoded_address")
725
-
726
- @client.delete_list_member('developers@mailgun.net', @address).must_equal(@json_response_object)
727
- end
728
- end
729
-
730
- it 'raises an exception for authentication errors' do
731
- stub_request(:any, /api\.mailgun\.net/).to_return(status: 401)
732
-
733
- proc { @client.get_message(@id) }.must_raise(Mailgunner::AuthenticationError)
734
- end
735
-
736
- it 'raises an exception for client errors' do
737
- stub_request(:any, /api\.mailgun\.net/).to_return(status: 400)
738
-
739
- proc { @client.get_message(@id) }.must_raise(Mailgunner::ClientError)
740
- end
741
-
742
- it 'raises an exception for server errors' do
743
- stub_request(:any, /api\.mailgun\.net/).to_return(status: 500)
744
-
745
- proc { @client.get_message(@id) }.must_raise(Mailgunner::ServerError)
746
- end
747
- end