mailgun-ruby 1.4.1 → 1.4.2

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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -1
  3. data/README.md +1 -1
  4. data/Rakefile +5 -3
  5. data/docs/AnalyticsTags.md +63 -0
  6. data/lib/mailgun/address.rb +5 -5
  7. data/lib/mailgun/chains.rb +2 -3
  8. data/lib/mailgun/client.rb +47 -51
  9. data/lib/mailgun/domains/domains.rb +7 -8
  10. data/lib/mailgun/events/events.rb +4 -3
  11. data/lib/mailgun/exceptions/exceptions.rb +12 -15
  12. data/lib/mailgun/helpers/api_version_checker.rb +6 -1
  13. data/lib/mailgun/lists/opt_in_handler.rb +4 -6
  14. data/lib/mailgun/logs/logs.rb +4 -2
  15. data/lib/mailgun/messages/batch_message.rb +8 -9
  16. data/lib/mailgun/messages/message_builder.rb +36 -24
  17. data/lib/mailgun/metrics/metrics.rb +6 -4
  18. data/lib/mailgun/response.rb +11 -9
  19. data/lib/mailgun/subaccounts/subaccounts.rb +13 -8
  20. data/lib/mailgun/suppressions.rb +36 -43
  21. data/lib/mailgun/tags/analytics_tags.rb +33 -2
  22. data/lib/mailgun/tags/tags.rb +25 -17
  23. data/lib/mailgun/templates/templates.rb +40 -29
  24. data/lib/mailgun/version.rb +3 -1
  25. data/lib/mailgun/webhooks/webhooks.rb +22 -19
  26. data/lib/mailgun-ruby.rb +2 -0
  27. data/lib/mailgun.rb +4 -4
  28. data/lib/railgun/attachment.rb +9 -14
  29. data/lib/railgun/errors.rb +2 -3
  30. data/lib/railgun/mailer.rb +35 -39
  31. data/lib/railgun/railtie.rb +2 -0
  32. data/lib/railgun.rb +2 -0
  33. data/mailgun.gemspec +12 -11
  34. data/spec/integration/analytics_tags_spec.rb +54 -0
  35. data/spec/integration/bounces_spec.rb +12 -11
  36. data/spec/integration/campaign_spec.rb +20 -18
  37. data/spec/integration/complaints_spec.rb +8 -6
  38. data/spec/integration/domains_spec.rb +6 -6
  39. data/spec/integration/email_validation_spec.rb +35 -34
  40. data/spec/integration/events_spec.rb +7 -5
  41. data/spec/integration/list_members_spec.rb +27 -26
  42. data/spec/integration/list_spec.rb +22 -21
  43. data/spec/integration/logs_spec.rb +48 -46
  44. data/spec/integration/mailer_spec.rb +7 -3
  45. data/spec/integration/mailgun_spec.rb +82 -90
  46. data/spec/integration/metrics_spec.rb +130 -130
  47. data/spec/integration/routes_spec.rb +41 -40
  48. data/spec/integration/stats_spec.rb +4 -2
  49. data/spec/integration/subaccounts_spec.rb +9 -10
  50. data/spec/integration/suppressions_spec.rb +21 -20
  51. data/spec/integration/templates_spec.rb +14 -12
  52. data/spec/integration/unsubscribes_spec.rb +8 -6
  53. data/spec/integration/webhook_spec.rb +13 -12
  54. data/spec/spec_helper.rb +8 -8
  55. data/spec/unit/connection/test_client.rb +61 -55
  56. data/spec/unit/events/events_spec.rb +25 -22
  57. data/spec/unit/exceptions/exceptions_spec.rb +8 -7
  58. data/spec/unit/lists/opt_in_handler_spec.rb +8 -6
  59. data/spec/unit/mailgun_spec.rb +64 -63
  60. data/spec/unit/messages/batch_message_spec.rb +15 -15
  61. data/spec/unit/messages/message_builder_spec.rb +98 -94
  62. data/spec/unit/railgun/content_type_spec.rb +24 -23
  63. data/spec/unit/railgun/mailer_spec.rb +58 -58
  64. data/vcr_cassettes/analytics_tags.yml +187 -0
  65. metadata +49 -33
  66. data/.rubocop.yml +0 -8
  67. data/.rubocop_todo.yml +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f7b6ece85b0a809a90ea79e6836b55bc5a97cfe8cd036c1567acff92b8053c5
4
- data.tar.gz: 277f0def30a104f86beef2c8ef3012b877fa94e35e415a9beff328cc76247280
3
+ metadata.gz: 45aed85ba39b57f41a049e86f7e4aa86e04092c2fc04c86f4a948857cea182c1
4
+ data.tar.gz: 4588e70de8bf2836028858adc294e1c1f1b5a8d07de6623a14a1c9797049eecd
5
5
  SHA512:
6
- metadata.gz: 3a419c6c2c8fbb7efa2ed08ad51da87441befe54e00e3a70db6626b82f353932827c5ebdd933f32edfe06ba043ea2dcba99cacd93a445f078ff089ad58af96ea
7
- data.tar.gz: c910105de443193207c67c5afaa24b2d423dfb7b7e995466a95b8192d967f244f36e45142d78f97a46fb893809a633e793d75366ea206f83338b8f138a4ed3af
6
+ metadata.gz: 346f281a71259601f0215f33a2e7e65d2c76f5f90601522ecf06ff5f9c8c25daff473b1d8a8fc6a71474fdf6ac37c8c89c815831c74b9d1e09238392be139db7
7
+ data.tar.gz: 3e00ab15f716a2381735b12a792c6ddc333d32f1574c3d7b50640e293ca4c1f5a60b796c241d3e7c1447cb7b455a162a179c4167d8020bec11f21b6fa03920f1
data/Gemfile CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in mailgun.gemspec
4
6
  gemspec
5
7
 
6
- gem 'mini_mime'
7
8
  gem 'json', '~> 2.1', platform: :mri_19
9
+ gem 'mini_mime'
data/README.md CHANGED
@@ -19,7 +19,7 @@ gem install mailgun-ruby
19
19
  Gemfile:
20
20
 
21
21
  ```ruby
22
- gem 'mailgun-ruby', '~>1.4.1'
22
+ gem 'mailgun-ruby', '~>1.4.2'
23
23
  ```
24
24
 
25
25
  Usage
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rake'
3
5
  require 'rspec/core/rake_task'
@@ -9,7 +11,7 @@ end
9
11
 
10
12
  desc 'Run unit specs'
11
13
  RSpec::Core::RakeTask.new('spec:unit') do |t|
12
- t.rspec_opts = %w(--colour --format documentation)
14
+ t.rspec_opts = %w[--colour --format documentation]
13
15
  t.pattern = 'spec/unit/*_spec.rb', 'spec/unit/*/*_spec.rb'
14
16
  end
15
17
 
@@ -17,13 +19,13 @@ desc 'Run integration specs'
17
19
  # Before running integration tests, you need to specify
18
20
  # a valid API KEY in the spec/spec_helper.rb file.
19
21
  RSpec::Core::RakeTask.new('spec:integration') do |t|
20
- t.rspec_opts = %w(--colour --format documentation)
22
+ t.rspec_opts = %w[--colour --format documentation]
21
23
  t.pattern = 'spec/integration/*_spec.rb'
22
24
  end
23
25
 
24
26
  desc 'Run all tests'
25
27
  RSpec::Core::RakeTask.new('spec:all') do |t|
26
- t.rspec_opts = %w(--colour --format documentation)
28
+ t.rspec_opts = %w[--colour --format documentation]
27
29
  t.pattern = 'spec/**/*_spec.rb'
28
30
  end
29
31
 
@@ -0,0 +1,63 @@
1
+ Mailgun - Analytics Tags
2
+ ====================
3
+
4
+ This is the Mailgun Ruby *Analytics Tags* utilities.
5
+
6
+ The below assumes you've already installed the Mailgun Ruby SDK in to your
7
+ project. If not, go back to the master README for instructions. It currently supports
8
+ all calls except credentials.
9
+
10
+ ---
11
+
12
+ Mailgun allows you to tag your email with unique identifiers. Tags are visible via our analytics tags API endpoint.
13
+
14
+ You can view additional samples in the [analytics_tags_spec.rb](/spec/integration/analytics_tags_spec.rb)
15
+ or the Analytics Tags client API in [analytics_tags.rb](/lib/mailgun/tags/analytics_tags.rb).
16
+
17
+ Usage
18
+ -----
19
+
20
+ To get an instance of the Analytics Tags client:
21
+
22
+ ```ruby
23
+ require 'mailgun'
24
+
25
+ mg_client = Mailgun::Client.new('your-api-key', 'mailgun-api-host', 'v1')
26
+ tags = Mailgun::AnalyticsTags.new(mg_client)
27
+ ````
28
+ ---
29
+ Update account tag:
30
+ ```ruby
31
+ tags.update('name-of-tag-to-update', 'updated tag description')
32
+ ```
33
+ ---
34
+
35
+ Post query to list account tags or search for single tag:
36
+ ```ruby
37
+ options = {
38
+ pagination: {
39
+ sort: 'lastseen:desc',
40
+ limit: 10
41
+ },
42
+ include_subaccounts: true
43
+ }
44
+
45
+ tags.list(options)
46
+ ```
47
+
48
+ Delete account tag:
49
+ ```ruby
50
+ tags.remove('name-of-tag-to-remove')
51
+ ```
52
+
53
+ Get account tag limit information:
54
+ ```ruby
55
+ tags.limits
56
+ ```
57
+
58
+ ---
59
+
60
+ More Documentation
61
+ ------------------
62
+ See the official [Mailgun Tags New Docs](https://documentation.mailgun.com/docs/mailgun/api-reference/send/mailgun/tags-new)
63
+ for more information
@@ -1,5 +1,6 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # Mailgun::Address is a simple interface to the Email Validation API.
4
5
  class Address
5
6
  def initialize(api_key = Mailgun.api_key, api_host = Mailgun.api_host)
@@ -10,12 +11,11 @@ module Mailgun
10
11
  #
11
12
  # @param [String] address Email address to validate (max 512 chars.)
12
13
  def validate(address, mailbox_verification = false)
13
- params = {address: address}
14
+ params = { address: address }
14
15
  params[:mailbox_verification] = true if mailbox_verification
15
16
 
16
- res = @client.get "address/validate", params
17
- return res.to_h!
17
+ res = @client.get 'address/validate', params
18
+ res.to_h!
18
19
  end
19
20
  end
20
-
21
21
  end
@@ -1,8 +1,8 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # Public constants used throughout
4
5
  class Chains
5
-
6
6
  # maximum campaign ids per message
7
7
  MAX_CAMPAIGN_IDS = 3
8
8
 
@@ -11,6 +11,5 @@ module Mailgun
11
11
 
12
12
  # maximum recipients per message or batch
13
13
  MAX_RECIPIENTS = 1000
14
-
15
14
  end
16
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mailgun
2
4
  # A Mailgun::Client object is used to communicate with the Mailgun API. It is a
3
5
  # wrapper around Faraday so you don't have to worry about the HTTP aspect
@@ -5,28 +7,27 @@ module Mailgun
5
7
  #
6
8
  # See the Github documentation for full examples.
7
9
  class Client
8
- SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'.freeze
10
+ SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'
9
11
 
10
12
  def initialize(api_key = Mailgun.api_key,
11
13
  api_host = Mailgun.api_host || 'api.mailgun.net',
12
- api_version = Mailgun.api_version || 'v3',
14
+ api_version = Mailgun.api_version || 'v3',
13
15
  ssl = true,
14
- test_mode = !!Mailgun.test_mode,
16
+ test_mode = !Mailgun.test_mode.nil?,
15
17
  timeout = nil,
16
18
  proxy_url = Mailgun.proxy_url)
17
-
18
19
  endpoint = endpoint_generator(api_host, api_version, ssl)
19
20
 
20
21
  request_options = {
21
22
  url: endpoint,
22
23
  proxy: proxy_url,
23
- ssl: {verify: ssl},
24
+ ssl: { verify: ssl },
24
25
  headers: {
25
- 'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
26
- 'Accept' =>'*/*'
27
- }
26
+ 'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
27
+ 'Accept' => '*/*'
28
+ }
28
29
  }
29
- request_options.merge!(request: {timeout: timeout}) if timeout
30
+ request_options.merge!(request: { timeout: timeout }) if timeout
30
31
 
31
32
  @http_client = build_http_client(api_key, request_options)
32
33
 
@@ -55,7 +56,7 @@ module Mailgun
55
56
 
56
57
  # Add subaccount id to headers
57
58
  def set_subaccount(subaccount_id)
58
- @http_client.headers = @http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
59
+ @http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
59
60
  end
60
61
 
61
62
  # Reset subaccount for primary usage
@@ -71,9 +72,7 @@ module Mailgun
71
72
  end
72
73
 
73
74
  # @return [String] client api version
74
- def api_version
75
- @api_version
76
- end
75
+ attr_reader :api_version
77
76
 
78
77
  # Provides a store of all the emails sent in test mode so you can check them.
79
78
  #
@@ -91,12 +90,12 @@ module Mailgun
91
90
  def send_message(working_domain, data)
92
91
  perform_data_validation(working_domain, data)
93
92
 
94
- if test_mode? then
93
+ if test_mode?
95
94
  Mailgun::Client.deliveries << data.dup
96
95
  return Response.from_hash(
97
96
  {
98
- :body => "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
99
- :status => 200,
97
+ body: "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
98
+ status: 200
100
99
  }
101
100
  )
102
101
  end
@@ -106,7 +105,7 @@ module Mailgun
106
105
  # Remove nil values from the data hash
107
106
  # Submitting nils to the API will likely cause an error.
108
107
  # See also: https://github.com/mailgun/mailgun-ruby/issues/32
109
- data = data.select { |k, v| v != nil }
108
+ data = data.reject { |_k, v| v.nil? }
110
109
 
111
110
  if data.key?(:message)
112
111
  if data[:message].is_a?(String)
@@ -119,7 +118,7 @@ module Mailgun
119
118
  when MessageBuilder
120
119
  post("#{working_domain}/messages", data.message)
121
120
  else
122
- fail ParameterError.new('Unknown data type for data parameter.', data)
121
+ raise ParameterError.new('Unknown data type for data parameter.', data)
123
122
  end
124
123
  end
125
124
 
@@ -134,8 +133,8 @@ module Mailgun
134
133
  def post(resource_path, data, headers = {})
135
134
  response = @http_client.post(resource_path, data, headers)
136
135
  Response.new(response)
137
- rescue => err
138
- raise communication_error err
136
+ rescue StandardError => e
137
+ raise communication_error e
139
138
  end
140
139
 
141
140
  # Generic Mailgun GET Handler
@@ -151,8 +150,8 @@ module Mailgun
151
150
  response = @http_client.get(resource_path, params, headers)
152
151
 
153
152
  Response.new(response)
154
- rescue => err
155
- raise communication_error(err)
153
+ rescue StandardError => e
154
+ raise communication_error(e)
156
155
  end
157
156
 
158
157
  # Generic Mailgun PUT Handler
@@ -162,19 +161,11 @@ module Mailgun
162
161
  # @param [Hash] data This should be a standard Hash
163
162
  # containing required parameters for the requested resource.
164
163
  # @return [Mailgun::Response] A Mailgun::Response object.
165
- def put(resource_path, params, body_params = false)
166
- response =
167
- if body_params
168
- @http_client.put(resource_path) do |request|
169
- request['Content-Type'] = 'application/json'
170
- request.params = params.to_json
171
- end
172
- else
173
- @http_client.put(resource_path, params)
174
- end
164
+ def put(resource_path, data, headers = {})
165
+ response = @http_client.put(resource_path, data, headers)
175
166
  Response.new(response)
176
- rescue => err
177
- raise communication_error err
167
+ rescue StandardError => e
168
+ raise communication_error e
178
169
  end
179
170
 
180
171
  # Generic Mailgun DELETE Handler
@@ -196,8 +187,8 @@ module Mailgun
196
187
  @http_client.delete(resource_path)
197
188
  end
198
189
  Response.new(response)
199
- rescue => err
200
- raise communication_error err
190
+ rescue StandardError => e
191
+ raise communication_error e
201
192
  end
202
193
 
203
194
  # Constructs a Suppressions client for the given domain.
@@ -229,7 +220,7 @@ module Mailgun
229
220
  # @param [Boolean] ssl True, SSL. False, No SSL.
230
221
  # @return [string] concatenated URL string
231
222
  def endpoint_generator(api_host, api_version, ssl)
232
- ssl ? scheme = 'https' : scheme = 'http'
223
+ scheme = ssl ? 'https' : 'http'
233
224
  if api_version
234
225
  "#{scheme}://#{api_host}/#{api_version}"
235
226
  else
@@ -243,28 +234,33 @@ module Mailgun
243
234
  def communication_error(e)
244
235
  if e.respond_to?(:response) && e.response
245
236
  return case e.response_status
246
- when Unauthorized::CODE
247
- Unauthorized.new(e.message, e.response)
248
- when BadRequest::CODE
249
- BadRequest.new(e.message, e.response)
250
- else
251
- CommunicationError.new(e.message, e.response)
252
- end
237
+ when Unauthorized::CODE
238
+ Unauthorized.new(e.message, e.response)
239
+ when BadRequest::CODE
240
+ BadRequest.new(e.message, e.response)
241
+ else
242
+ CommunicationError.new(e.message, e.response)
243
+ end
253
244
  end
254
245
  CommunicationError.new(e.message)
255
246
  end
256
247
 
257
248
  def perform_data_validation(working_domain, data)
258
249
  message = data.respond_to?(:message) ? data.message : data
259
- fail ParameterError.new('Missing working domain', working_domain) unless working_domain
260
- fail ParameterError.new(
261
- 'Missing `to` recipient, message should contain at least 1 recipient',
262
- working_domain
263
- ) if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
264
- fail ParameterError.new(
250
+ raise ParameterError.new('Missing working domain', working_domain) unless working_domain
251
+
252
+ if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
253
+ raise ParameterError.new(
254
+ 'Missing `to` recipient, message should contain at least 1 recipient',
255
+ working_domain
256
+ )
257
+ end
258
+ return unless message.fetch('from', []).empty? && message.fetch(:from, []).empty?
259
+
260
+ raise ParameterError.new(
265
261
  'Missing a `from` sender, message should contain at least 1 `from` sender',
266
262
  working_domain
267
- ) if message.fetch('from', []).empty? && message.fetch(:from, []).empty?
263
+ )
268
264
  end
269
265
 
270
266
  def build_http_client(api_key, request_options)
@@ -1,5 +1,6 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # A Mailgun::Domains object is a simple CRUD interface to Mailgun Domains.
4
5
  # Uses Mailgun
5
6
  class Domains
@@ -67,7 +68,6 @@ module Mailgun
67
68
  @client.put("domains/#{domain}", options).to_h
68
69
  end
69
70
 
70
-
71
71
  # Public: Verify domain
72
72
  #
73
73
  # domain - [String] Domain name
@@ -101,7 +101,7 @@ module Mailgun
101
101
  #
102
102
  # Returns [Hash] with message key
103
103
  def list_domain_keys(options = {})
104
- @client.get("dkim/keys", options).to_h
104
+ @client.get('dkim/keys', options).to_h
105
105
  end
106
106
 
107
107
  # Public: Create a domain key
@@ -114,7 +114,7 @@ module Mailgun
114
114
  #
115
115
  # Returns [Hash] with message key
116
116
  def create_domain_key(options = {})
117
- @client.post("dkim/keys", options).to_h
117
+ @client.post('dkim/keys', options).to_h
118
118
  end
119
119
 
120
120
  # Public: Delete a domain key.
@@ -125,7 +125,7 @@ module Mailgun
125
125
  #
126
126
  # Returns [Hash] with message key
127
127
  def delete_domain_key(options = {})
128
- @client.delete("dkim/keys", options).to_h
128
+ @client.delete('dkim/keys', options).to_h
129
129
  end
130
130
 
131
131
  # Public: Activate a domain key for a specified authority and selector.
@@ -258,7 +258,6 @@ module Mailgun
258
258
 
259
259
  # ==== End of Domain::Tracking methods ====
260
260
 
261
-
262
261
  # ==== Domain::DKIM_Security methods ====
263
262
 
264
263
  # Public: Tracking Certificate: Generate
@@ -338,13 +337,13 @@ module Mailgun
338
337
  #
339
338
  # Returns [Array] A list of domains (hash)
340
339
  def get_domain_stats(domain, options = {})
341
- fail(ParameterError, 'No domain given to list stats on Mailgun', caller) unless domain
340
+ raise(ParameterError, 'No domain given to list stats on Mailgun', caller) unless domain
341
+
342
342
  @client.get("#{domain}/stats/total", options).to_h
343
343
  end
344
344
 
345
345
  # ==== End of Reporting::Stats methods ====
346
346
 
347
-
348
347
  enforces_api_version 'v1', :list_domain_keys, :create_domain_key, :delete_domain_key, :dkim_rotation,
349
348
  :dkim_rotate
350
349
  enforces_api_version 'v2', :get_domain_tracking_certificate, :regenerate_domain_tracking_certificate,
@@ -1,5 +1,6 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # A Mailgun::Events object makes it really simple to consume
4
5
  # Mailgun's events from the Events endpoint.
5
6
  #
@@ -100,7 +101,7 @@ module Mailgun
100
101
  # Returns a String of the partial URI if the given url follows the regular API format
101
102
  # Returns nil in other cases (e.g. when given nil, or an irrelevant url)
102
103
  def extract_endpoint_from(url = nil)
103
- URI.parse(url).path[/\/v[\d]\/#{@domain}\/events\/(.+)/,1]
104
+ URI.parse(url).path[%r{/v\d/#{@domain}/events/(.+)}, 1]
104
105
  rescue URI::InvalidURIError
105
106
  nil
106
107
  end
@@ -112,8 +113,8 @@ module Mailgun
112
113
  # Returns a String of the partial URI
113
114
  def construct_url(paging = nil)
114
115
  return "#{@domain}/events/#{paging}" if paging
116
+
115
117
  "#{@domain}/events"
116
118
  end
117
-
118
119
  end
119
120
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mailgun
2
4
  module Exceptions
3
5
  end
@@ -6,7 +8,6 @@ module Mailgun
6
8
  # Inherits from StandardError (previously RuntimeError) as not all errors are
7
9
  # runtime errors.
8
10
  class Error < StandardError
9
-
10
11
  # Public: get an object an error is instantiated with
11
12
  attr_reader :object
12
13
 
@@ -35,7 +36,7 @@ module Mailgun
35
36
  attr_reader :status
36
37
 
37
38
  # Public: fallback if there is no response status on the object
38
- NOCODE = 000
39
+ NOCODE = 0o00
39
40
  FORBIDDEN = 'Forbidden'
40
41
 
41
42
  # Public: initialization of new error given a message and/or object
@@ -46,10 +47,10 @@ module Mailgun
46
47
  def initialize(message = nil, response = nil)
47
48
  @response = response
48
49
  @status = if response.nil?
49
- NOCODE
50
- else
51
- response[:status]
52
- end
50
+ NOCODE
51
+ else
52
+ response[:status]
53
+ end
53
54
 
54
55
  begin
55
56
  json = JSON.parse(response[:body])
@@ -57,13 +58,13 @@ module Mailgun
57
58
  rescue JSON::ParserError
58
59
  api_message = response.response_body
59
60
  rescue NoMethodError
60
- api_message = "Unknown API error"
61
- rescue
61
+ api_message = 'Unknown API error'
62
+ rescue StandardError
62
63
  api_message = 'Unknown API error'
63
64
  end
64
65
 
65
- message = message || ''
66
- message = message + ': ' + (api_message || "")
66
+ message ||= ''
67
+ message = "#{message}: #{api_message || ''}"
67
68
 
68
69
  super(message, response)
69
70
  rescue NoMethodError, JSON::ParserError
@@ -78,7 +79,7 @@ module Mailgun
78
79
  CODE = 401
79
80
 
80
81
  def initialize(error_message, response)
81
- error_message = error_message + ' - Invalid Domain or API key'
82
+ error_message += ' - Invalid Domain or API key'
82
83
  super(error_message, response)
83
84
  end
84
85
  end
@@ -87,9 +88,5 @@ module Mailgun
87
88
  # Inherits from Mailgun::CommunicationError
88
89
  class BadRequest < CommunicationError
89
90
  CODE = 400
90
-
91
- def initialize(error_message, response)
92
- super(error_message, response)
93
- end
94
91
  end
95
92
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mailgun
2
4
  module ApiVersionChecker
3
5
  def self.included(base)
@@ -35,7 +37,10 @@ module Mailgun
35
37
  end
36
38
 
37
39
  def require_api_version(expected_version)
38
- fail(ParameterError, "Client api version must be #{expected_version}", caller) unless @client.api_version == expected_version
40
+ return if @client.api_version == expected_version
41
+
42
+ raise(ParameterError, "Client api version must be #{expected_version}",
43
+ caller)
39
44
  end
40
45
  end
41
46
  end
@@ -1,11 +1,11 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # Public: Provides methods for creating and handling opt-in URLs,
4
5
  # particularlly for mailing lists.
5
6
  #
6
7
  # See: https://github.com/mailgun/mailgun-ruby/blob/master/OptInHandler.md
7
8
  class OptInHandler
8
-
9
9
  # Generates a hash that can be used to validate opt-in recipients. Encodes
10
10
  # all the necessary data in the URL.
11
11
  #
@@ -46,6 +46,7 @@ module Mailgun
46
46
  if generated_hash == hash_provided
47
47
  return { 'recipient_address' => inner_payload['r'], 'mailing_list' => inner_payload['l'] }
48
48
  end
49
+
49
50
  false
50
51
  end
51
52
 
@@ -58,13 +59,10 @@ module Mailgun
58
59
  def self.base64_decode(input)
59
60
  # TODO: Condition can be droped if Ruby >= 2.4.0
60
61
  if input.respond_to?(:unpack1)
61
- input.unpack1('m')
62
- else
63
- input.unpack('m').first
64
62
  end
63
+ input.unpack1('m')
65
64
  end
66
65
 
67
66
  private_class_method :base64_encode, :base64_decode
68
67
  end
69
-
70
68
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mailgun
2
4
  # A Mailgun::Logs object is a simple interface to Mailgun Logs.
3
5
  # Uses Mailgun
@@ -30,8 +32,8 @@ module Mailgun
30
32
  # limit - [Integer] The maximum number of items returned (100 max).
31
33
  #
32
34
  # Returns [Hash] Logs
33
- def account_logs(options={})
34
- @client.post('analytics/logs', options.to_json, { "Content-Type" => "application/json" }).to_h!
35
+ def account_logs(options = {})
36
+ @client.post('analytics/logs', options.to_json, { 'Content-Type' => 'application/json' }).to_h!
35
37
  end
36
38
  end
37
39
  end
@@ -1,5 +1,6 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # A Mailgun::BatchMessage object is used to create a valid payload
4
5
  # for Batch Sending. Batch Sending can be difficult to implement, therefore
5
6
  # this code makes it dead simple to send millions of messages in batches of
@@ -18,7 +19,6 @@ module Mailgun
18
19
  #
19
20
  # See the Github documentation for full examples.
20
21
  class BatchMessage < MessageBuilder
21
-
22
22
  attr_reader :message_ids, :domain, :recipient_variables
23
23
 
24
24
  # Public: Creates a new BatchMessage object.
@@ -68,9 +68,10 @@ module Mailgun
68
68
  #
69
69
  # @return [Boolean]
70
70
  def any_recipients_left?
71
- return true if @counters[:recipients][:to] > 0
72
- return true if @counters[:recipients][:cc] > 0
73
- return true if @counters[:recipients][:bcc] > 0
71
+ return true if @counters[:recipients][:to].positive?
72
+ return true if @counters[:recipients][:cc].positive?
73
+ return true if @counters[:recipients][:bcc].positive?
74
+
74
75
  false
75
76
  end
76
77
 
@@ -86,7 +87,7 @@ module Mailgun
86
87
  @message[rkey] = @message[rkey].first if @message.key?(rkey)
87
88
 
88
89
  response = @client.send_message(@domain, @message).to_h!
89
- message_id = response['id'].gsub(/\>|\</, '')
90
+ message_id = response['id'].gsub(/>|</, '')
90
91
  @message_ids[message_id] = count_recipients
91
92
  reset_message
92
93
  end
@@ -94,7 +95,7 @@ module Mailgun
94
95
  # This method stores recipient variables for each recipient added, if
95
96
  # variables exist.
96
97
  def store_recipient_variables(recipient_type, address, variables)
97
- variables = { id: @counters[:recipients][recipient_type] } unless variables
98
+ variables ||= { id: @counters[:recipients][recipient_type] }
98
99
  @recipient_variables[address] = variables
99
100
  end
100
101
 
@@ -118,7 +119,5 @@ module Mailgun
118
119
  @counters[:recipients][:cc] = 0
119
120
  @counters[:recipients][:bcc] = 0
120
121
  end
121
-
122
122
  end
123
-
124
123
  end