mailgunner 2.5.0 → 3.2.0

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