mailgun-ruby 1.4.1 → 1.4.3

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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +30 -8
  3. data/.rubocop.yml +64 -4
  4. data/Gemfile +3 -1
  5. data/README.md +1 -1
  6. data/Rakefile +5 -8
  7. data/docs/AnalyticsTags.md +63 -0
  8. data/lib/mailgun/address.rb +5 -5
  9. data/lib/mailgun/chains.rb +2 -3
  10. data/lib/mailgun/client.rb +56 -56
  11. data/lib/mailgun/domains/domains.rb +11 -10
  12. data/lib/mailgun/events/events.rb +4 -3
  13. data/lib/mailgun/exceptions/exceptions.rb +12 -15
  14. data/lib/mailgun/helpers/api_version_checker.rb +6 -1
  15. data/lib/mailgun/lists/opt_in_handler.rb +6 -10
  16. data/lib/mailgun/logs/logs.rb +4 -2
  17. data/lib/mailgun/messages/batch_message.rb +10 -10
  18. data/lib/mailgun/messages/message_builder.rb +40 -56
  19. data/lib/mailgun/metrics/metrics.rb +12 -6
  20. data/lib/mailgun/response.rb +12 -10
  21. data/lib/mailgun/subaccounts/subaccounts.rb +13 -8
  22. data/lib/mailgun/suppressions.rb +36 -43
  23. data/lib/mailgun/tags/analytics_tags.rb +37 -2
  24. data/lib/mailgun/tags/tags.rb +29 -19
  25. data/lib/mailgun/templates/templates.rb +40 -29
  26. data/lib/mailgun/version.rb +3 -1
  27. data/lib/mailgun/webhooks/webhooks.rb +22 -19
  28. data/lib/mailgun-ruby.rb +2 -0
  29. data/lib/mailgun.rb +4 -4
  30. data/lib/railgun/attachment.rb +12 -19
  31. data/lib/railgun/errors.rb +2 -3
  32. data/lib/railgun/mailer.rb +37 -41
  33. data/lib/railgun/railtie.rb +2 -0
  34. data/lib/railgun.rb +2 -0
  35. data/mailgun.gemspec +15 -11
  36. data/spec/integration/analytics_tags_spec.rb +54 -0
  37. data/spec/integration/bounces_spec.rb +12 -11
  38. data/spec/integration/campaign_spec.rb +20 -18
  39. data/spec/integration/complaints_spec.rb +8 -6
  40. data/spec/integration/domains_spec.rb +12 -18
  41. data/spec/integration/email_validation_spec.rb +35 -34
  42. data/spec/integration/events_spec.rb +8 -8
  43. data/spec/integration/list_members_spec.rb +27 -26
  44. data/spec/integration/list_spec.rb +22 -21
  45. data/spec/integration/logs_spec.rb +49 -47
  46. data/spec/integration/mailer_spec.rb +7 -3
  47. data/spec/integration/mailgun_spec.rb +85 -92
  48. data/spec/integration/metrics_spec.rb +137 -131
  49. data/spec/integration/routes_spec.rb +41 -40
  50. data/spec/integration/stats_spec.rb +4 -2
  51. data/spec/integration/subaccounts_spec.rb +9 -10
  52. data/spec/integration/suppressions_spec.rb +222 -44
  53. data/spec/integration/templates_spec.rb +14 -12
  54. data/spec/integration/unsubscribes_spec.rb +8 -6
  55. data/spec/integration/webhook_spec.rb +18 -12
  56. data/spec/spec_helper.rb +15 -8
  57. data/spec/unit/client_spec.rb +424 -0
  58. data/spec/unit/connection/test_client.rb +108 -55
  59. data/spec/unit/events/events_spec.rb +48 -29
  60. data/spec/unit/exceptions/exceptions_spec.rb +8 -7
  61. data/spec/unit/helpers/api_version_checker_spec.rb +206 -0
  62. data/spec/unit/lists/opt_in_handler_spec.rb +11 -7
  63. data/spec/unit/mailgun_spec.rb +71 -68
  64. data/spec/unit/messages/batch_message_spec.rb +37 -36
  65. data/spec/unit/messages/message_builder_spec.rb +170 -169
  66. data/spec/unit/railgun/content_type_spec.rb +31 -30
  67. data/spec/unit/railgun/mailer_spec.rb +62 -59
  68. data/spec/unit/response_spec.rb +225 -0
  69. data/vcr_cassettes/For_the_suppressions_handling_class/creates_a_single_bounce.yml +55 -0
  70. data/vcr_cassettes/analytics_tags.yml +187 -0
  71. data/vcr_cassettes/suppressions.yml +1053 -170
  72. metadata +95 -29
  73. data/.rubocop_todo.yml +0 -22
@@ -1,9 +1,9 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # The Mailgun::Suppressions object makes it easy to manage "suppressions"
4
5
  # attached to an account. "Suppressions" means bounces, unsubscribes, and complaints.
5
6
  class Suppressions
6
-
7
7
  # @param [Mailgun::Client] client API client to use for requests
8
8
  # @param [String] domain Domain name to use for the suppression endpoints.
9
9
  def initialize(client, domain)
@@ -60,12 +60,12 @@ module Mailgun
60
60
  def create_bounces(data)
61
61
  # `data` should be a list of hashes, with each hash containing *at least* an `address` key.
62
62
  split_return = []
63
- if data.length >= 1000 then
64
- resp, resp_l = create_bounces data[999..-1]
63
+ if data.length >= 1000
64
+ resp, resp_l = create_bounces data[999..]
65
65
  split_return.push(resp)
66
66
  split_return.concat(resp_l)
67
67
  data = data[0..998]
68
- elsif data.length == 0 then
68
+ elsif data.empty?
69
69
  return nil, []
70
70
  end
71
71
 
@@ -74,25 +74,21 @@ module Mailgun
74
74
  # NOTE: `data` could potentially be very large (1000 elements) so it is
75
75
  # more efficient to pop from data and push into a different array as
76
76
  # opposed to possibly copying the entire array to another array.
77
- while not data.empty? do
77
+ until data.empty?
78
78
  bounce = data.pop
79
79
  # Bounces MUST contain a `address` key.
80
- if not bounce.include? :address then
81
- raise Mailgun::ParameterError.new "Bounce MUST include a :address key: #{bounce}"
82
- end
80
+ raise Mailgun::ParameterError, "Bounce MUST include a :address key: #{bounce}" unless bounce.include? :address
83
81
 
84
82
  bounce.each do |k, v|
85
83
  # Hash values MUST be strings.
86
- if not v.is_a? String then
87
- bounce[k] = v.to_s
88
- end
84
+ bounce[k] = v.to_s unless v.is_a? String
89
85
  end
90
86
 
91
87
  valid.push bounce
92
88
  end
93
89
 
94
- response = @client.post("#{@domain}/bounces", valid.to_json, { "Content-Type" => "application/json" })
95
- return response, split_return
90
+ response = @client.post("#{@domain}/bounces", valid.to_json, { 'Content-Type' => 'application/json' })
91
+ [response, split_return]
96
92
  end
97
93
 
98
94
  def delete_bounce(address)
@@ -133,22 +129,22 @@ module Mailgun
133
129
  def create_unsubscribes(data)
134
130
  # `data` should be a list of hashes, with each hash containing *at least* an `address` key.
135
131
  split_return = []
136
- if data.length >= 1000 then
137
- resp, resp_l = create_unsubscribes data[999..-1]
132
+ if data.length >= 1000
133
+ resp, resp_l = create_unsubscribes data[999..]
138
134
  split_return.push(resp)
139
135
  split_return.concat(resp_l)
140
136
  data = data[0..998]
141
- elsif data.length == 0 then
137
+ elsif data.empty?
142
138
  return nil, []
143
139
  end
144
140
 
145
141
  valid = []
146
142
  # Validate the unsubscribes given
147
- while not data.empty? do
143
+ until data.empty?
148
144
  unsubscribe = data.pop
149
145
  # unsubscribes MUST contain a `address` key.
150
- if not unsubscribe.include? :address then
151
- raise Mailgun::ParameterError.new "Unsubscribe MUST include a :address key: #{unsubscribe}"
146
+ unless unsubscribe.include? :address
147
+ raise Mailgun::ParameterError, "Unsubscribe MUST include a :address key: #{unsubscribe}"
152
148
  end
153
149
 
154
150
  unsubscribe.each do |k, v|
@@ -164,11 +160,11 @@ module Mailgun
164
160
  valid.push unsubscribe
165
161
  end
166
162
 
167
- response = @client.post("#{@domain}/unsubscribes", valid.to_json, { "Content-Type" => "application/json" })
168
- return response, split_return
163
+ response = @client.post("#{@domain}/unsubscribes", valid.to_json, { 'Content-Type' => 'application/json' })
164
+ [response, split_return]
169
165
  end
170
166
 
171
- def delete_unsubscribe(address, params = {})
167
+ def delete_unsubscribe(address, _params = {})
172
168
  @client.delete("#{@domain}/unsubscribes/#{escape_address(address)}")
173
169
  end
174
170
 
@@ -202,36 +198,34 @@ module Mailgun
202
198
  def create_complaints(data)
203
199
  # `data` should be a list of hashes, with each hash containing *at least* an `address` key.
204
200
  split_return = []
205
- if data.length >= 1000 then
206
- resp, resp_l = create_complaints data[999..-1]
201
+ if data.length >= 1000
202
+ resp, resp_l = create_complaints data[999..]
207
203
  split_return.push(resp)
208
204
  split_return.concat(resp_l)
209
205
  data = data[0..998]
210
- elsif data.length == 0 then
206
+ elsif data.empty?
211
207
  return nil, []
212
208
  end
213
209
 
214
210
  valid = []
215
211
  # Validate the complaints given
216
- while not data.empty? do
212
+ until data.empty?
217
213
  complaint = data.pop
218
214
  # complaints MUST contain a `address` key.
219
- if not complaint.include? :address then
220
- raise Mailgun::ParameterError.new "Complaint MUST include a :address key: #{complaint}"
215
+ unless complaint.include? :address
216
+ raise Mailgun::ParameterError, "Complaint MUST include a :address key: #{complaint}"
221
217
  end
222
218
 
223
219
  complaint.each do |k, v|
224
220
  # Hash values MUST be strings.
225
- if not v.is_a? String then
226
- complaint[k] = v.to_s
227
- end
221
+ complaint[k] = v.to_s unless v.is_a? String
228
222
  end
229
223
 
230
224
  valid.push complaint
231
225
  end
232
226
 
233
- response = @client.post("#{@domain}/complaints", valid.to_json, { "Content-Type" => "application/json" })
234
- return response, split_return
227
+ response = @client.post("#{@domain}/complaints", valid.to_json, { 'Content-Type' => 'application/json' })
228
+ [response, split_return]
235
229
  end
236
230
 
237
231
  def delete_complaint(address)
@@ -250,24 +244,23 @@ module Mailgun
250
244
 
251
245
  def extract_paging(response)
252
246
  rhash = response.to_h
253
- return nil unless rhash.include? "paging"
247
+ return nil unless rhash.include? 'paging'
254
248
 
255
- page_info = rhash["paging"]
249
+ page_info = rhash['paging']
256
250
 
257
251
  # Build the `next` endpoint
258
- page_next = URI.parse(page_info["next"])
252
+ page_next = URI.parse(page_info['next'])
259
253
  @paging_next = {
260
- :path => page_next.path[/\/v[\d]\/(.+)/, 1],
261
- :params => Hash[URI.decode_www_form page_next.query],
254
+ path: page_next.path[%r{/v\d/(.+)}, 1],
255
+ params: Hash[URI.decode_www_form page_next.query]
262
256
  }
263
257
 
264
258
  # Build the `prev` endpoint
265
- page_prev = URI.parse(page_info["previous"])
259
+ page_prev = URI.parse(page_info['previous'])
266
260
  @paging_prev = {
267
- :path => page_prev.path[/\/v[\d]\/(.+)/, 1],
268
- :params => Hash[URI.decode_www_form page_prev.query],
261
+ path: page_prev.path[%r{/v\d/(.+)}, 1],
262
+ params: Hash[URI.decode_www_form page_prev.query]
269
263
  }
270
264
  end
271
-
272
265
  end
273
266
  end
@@ -1,5 +1,6 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # A Mailgun::AnalyticsTags object is a simple CRUD interface to Mailgun Tags.
4
5
  # Uses Mailgun
5
6
  class AnalyticsTags
@@ -11,18 +12,52 @@ module Mailgun
11
12
  @client = client
12
13
  end
13
14
 
15
+ # Public: Update account tag
16
+ #
17
+ # tag - [String] The tag to update.
18
+ # description - [String] The updated tag description.
19
+ #
20
+ # Returns [Boolean] true or false
14
21
  def update(tag, description)
15
- @client.put('analytics/tags', { tag: tag, description: description }, body_params: true ).to_h['message'] == 'Tag updated'
22
+ @client.put('analytics/tags', { tag: tag, description: description }.to_json,
23
+ { 'Content-Type' => 'application/json' }).to_h['message'] == 'Tag updated'
16
24
  end
17
25
 
26
+ # Public: Post query to list account tags or search for single tag
27
+ #
28
+ # options - [Hash] of
29
+ # include_subaccounts - [Boolean] Boolean indicating whether or not to include data from all subaccounts.
30
+ # Default false.
31
+ # include_metrics - [Boolean] Boolean indicating whether or not to include metrics for tags.
32
+ # Default false. When true max limit is 20.
33
+ # tag - [String] The tag or tag prefix.
34
+ # pagination - [Object]
35
+ # sort - [String] Colon-separated value indicating column name and sort direction
36
+ # e.g. 'timestamp:desc'.
37
+ # skip - [Integer] The number of items to skip over when satisfying the request.
38
+ # limit - [Integer] The maximum number of items returned (100 max).
39
+ # total - [Integer] The total number of tags matching the search criteria.
40
+ # include_total - [Boolean] Boolean indicating whether or not to include total number of items.
41
+ # Default false.
42
+
43
+ #
44
+ # Returns [Hash] Information on the requested tags.
18
45
  def list(options = {})
19
46
  @client.post('analytics/tags', options).to_h['items']
20
47
  end
21
48
 
49
+ # Public: Delete account tag
50
+ #
51
+ # tag - [String] The tag to delete.
52
+ #
53
+ # Returns [Boolean] true or false
22
54
  def remove(tag)
23
55
  @client.delete('analytics/tags', { tag: tag }, body_params: true).to_h['message'] == 'Tag deleted'
24
56
  end
25
57
 
58
+ # Public: Get account tag limit information
59
+ #
60
+ # Returns [Hash] Information on the tag limits.
26
61
  def limits
27
62
  @client.get('analytics/tags/limits').to_h
28
63
  end
@@ -1,9 +1,9 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # A Mailgun::Tags object is a simple CRUD interface to Mailgun Tags.
4
5
  # Uses Mailgun
5
6
  class Tags
6
-
7
7
  # Public: creates a new Mailgun::Tags instance.
8
8
  # Defaults to Mailgun::Client
9
9
  def initialize(client = Mailgun::Client.new)
@@ -22,7 +22,8 @@ module Mailgun
22
22
  # Returns [Array] A list of tags (hash)
23
23
  def get_tags(domain, options = {})
24
24
  warn('This API is deprecated in favor of our new analytics Tags API')
25
- fail(ParameterError, 'No domain given to store template on', caller) unless domain
25
+ raise(ParameterError, 'No domain given to store template on', caller) unless domain
26
+
26
27
  @client.get("#{domain}/tags", options).to_h['items']
27
28
  end
28
29
 
@@ -34,8 +35,9 @@ module Mailgun
34
35
  # Returns [Hash] Information on the requested tag.
35
36
  def get_tag(domain, tag)
36
37
  warn('This API is deprecated in favor of our new analytics Tags API')
37
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
38
- fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
38
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
39
+ raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
40
+
39
41
  @client.get("#{domain}/tags/#{tag}").to_h!
40
42
  end
41
43
 
@@ -49,8 +51,9 @@ module Mailgun
49
51
  # Returns [Boolean] if successful or not
50
52
  def update(domain, tag, options = {})
51
53
  warn('This API is deprecated in favor of our new analytics Tags API')
52
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
53
- fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
54
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
55
+ raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
56
+
54
57
  @client.put("#{domain}/tags/#{tag}", options).to_h['message'] == 'Tag updated'
55
58
  end
56
59
 
@@ -60,16 +63,19 @@ module Mailgun
60
63
  # tag - [String] Tag name to lookup for
61
64
  # options - [Hash] of
62
65
  # event - [String] The type of the event. Required. (ex. accepted, delivered, failed, opened)
63
- # start - [String] The starting time. Should be in RFC 282 or unix epoch format. Default: 7 days from the current time.
66
+ # start - [String] The starting time. Should be in RFC 282 or unix epoch format.
67
+ # Default: 7 days from the current time.
64
68
  # end - [String] The ending date. Should be in RFC 2822 or unix epoch time in seconds. Default: current time.
65
69
  # resolution - [String] Can be either hour, day or month. Default: day
66
- # duration - [String] Period of time with resolution encoded. If provided, overwrites the start date and resolution.
70
+ # duration - [String] Period of time with resolution encoded.
71
+ # If provided, overwrites the start date and resolution.
67
72
  #
68
73
  # Returns [Hash] of tag stats info
69
74
  def get_tag_stats(domain, tag, options = {})
70
75
  warn('This API is deprecated in favor of our new analytics Tags API')
71
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
72
- fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
76
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
77
+ raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
78
+
73
79
  @client.get("#{domain}/tags/#{tag}/stats", options).to_h
74
80
  end
75
81
 
@@ -82,8 +88,9 @@ module Mailgun
82
88
  # Returns [Boolean] if successful or not
83
89
  def remove(domain, tag)
84
90
  warn('This API is deprecated in favor of our new analytics Tags API')
85
- fail(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
86
- fail(ParameterError, 'No template name given to find on provided domain', caller) unless tag
91
+ raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
92
+ raise(ParameterError, 'No template name given to find on provided domain', caller) unless tag
93
+
87
94
  @client.delete("#{domain}/tags/#{tag}").to_h['message'] == 'Tag deleted'
88
95
  end
89
96
 
@@ -95,8 +102,9 @@ module Mailgun
95
102
  # Returns [Hash] of countries of origin for a given domain
96
103
  def get_countries_aggregated_stats(domain, tag)
97
104
  warn('This API is deprecated in favor of our new analytics Tags API')
98
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
99
- fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
105
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
106
+ raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
107
+
100
108
  @client.get("#{domain}/tags/#{tag}/stats/aggregates/countries").to_h
101
109
  end
102
110
 
@@ -108,8 +116,9 @@ module Mailgun
108
116
  # Returns [Hash] of email providers for a given domain
109
117
  def get_providers_aggregated_stats(domain, tag)
110
118
  warn('This API is deprecated in favor of our new analytics Tags API')
111
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
112
- fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
119
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
120
+ raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
121
+
113
122
  @client.get("#{domain}/tags/#{tag}/stats/aggregates/providers").to_h
114
123
  end
115
124
 
@@ -121,8 +130,9 @@ module Mailgun
121
130
  # Returns [Hash] of devices for a given domain
122
131
  def get_devices_aggregated_stats(domain, tag)
123
132
  warn('This API is deprecated in favor of our new analytics Tags API')
124
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
125
- fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
133
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
134
+ raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
135
+
126
136
  @client.get("#{domain}/tags/#{tag}/stats/aggregates/devices").to_h
127
137
  end
128
138
  end
@@ -1,9 +1,9 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # A Mailgun::Templates object is a simple CRUD interface to Mailgun Templates.
4
5
  # Uses Mailgun
5
6
  class Templates
6
-
7
7
  # Public: creates a new Mailgun::Templates instance.
8
8
  # Defaults to Mailgun::Client
9
9
  def initialize(client = Mailgun::Client.new)
@@ -24,7 +24,8 @@ module Mailgun
24
24
  #
25
25
  # Returns [Hash] of created template
26
26
  def create(domain, options = {})
27
- fail(ParameterError, 'No domain given to store template on', caller) unless domain
27
+ raise(ParameterError, 'No domain given to store template on', caller) unless domain
28
+
28
29
  @client.post("#{domain}/templates", options).to_h
29
30
  end
30
31
 
@@ -38,8 +39,9 @@ module Mailgun
38
39
  #
39
40
  # Returns [Hash] Information on the requested template.
40
41
  def info(domain, template_name, options = {})
41
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
42
- fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
42
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
43
+ raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
44
+
43
45
  @client.get("#{domain}/templates/#{template_name}", options).to_h!
44
46
  end
45
47
 
@@ -52,8 +54,9 @@ module Mailgun
52
54
  #
53
55
  # Returns [Hash] of updated domain
54
56
  def update(domain, template_name, options = {})
55
- fail(ParameterError, 'No domain given to add on Mailgun', caller) unless domain
56
- fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
57
+ raise(ParameterError, 'No domain given to add on Mailgun', caller) unless domain
58
+ raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
59
+
57
60
  @client.put("#{domain}/templates/#{template_name}", options).to_h
58
61
  end
59
62
 
@@ -65,12 +68,13 @@ module Mailgun
65
68
  #
66
69
  # Returns [Boolean] if successful or not
67
70
  def remove(domain, template_name)
68
- fail(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
69
- fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
71
+ raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
72
+ raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
73
+
70
74
  @client.delete("#{domain}/templates/#{template_name}").to_h['message'] == 'template has been deleted'
71
75
  end
72
- alias_method :delete, :remove
73
- alias_method :delete_template, :remove
76
+ alias delete remove
77
+ alias delete_template remove
74
78
 
75
79
  # Public: Get Templates
76
80
  #
@@ -81,10 +85,11 @@ module Mailgun
81
85
  #
82
86
  # Returns [Array] A list of templates (hash)
83
87
  def list(domain, options = {})
84
- fail(ParameterError, 'No domain given.', caller) unless domain
88
+ raise(ParameterError, 'No domain given.', caller) unless domain
89
+
85
90
  @client.get("#{domain}/templates", options).to_h['items']
86
91
  end
87
- alias_method :get_templates, :list
92
+ alias get_templates list
88
93
 
89
94
  # Public: Delete Templates
90
95
  # NOTE: This method deletes all stored templates for the domain.
@@ -93,10 +98,11 @@ module Mailgun
93
98
  #
94
99
  # Returns [Boolean] if successful or not
95
100
  def remove_all(domain)
96
- fail(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
101
+ raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
102
+
97
103
  @client.delete("#{domain}/templates").to_h['message'] == 'templates have been deleted'
98
104
  end
99
- alias_method :delete_templates, :remove_all
105
+ alias delete_templates remove_all
100
106
 
101
107
  # Public: Create a new version of a template
102
108
  #
@@ -112,8 +118,9 @@ module Mailgun
112
118
  #
113
119
  # Returns [Hash] of updated template
114
120
  def create_version(domain, template_name, options = {})
115
- fail(ParameterError, 'No domain given.', caller) unless domain
116
- fail(ParameterError, 'No template name given.', caller) unless template_name
121
+ raise(ParameterError, 'No domain given.', caller) unless domain
122
+ raise(ParameterError, 'No template name given.', caller) unless template_name
123
+
117
124
  @client.post("#{domain}/templates/#{template_name}/versions", options).to_h
118
125
  end
119
126
 
@@ -125,9 +132,10 @@ module Mailgun
125
132
  #
126
133
  # Returns [Hash] Information on the requested template + version.
127
134
  def info_version(domain, template_name, tag)
128
- fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
129
- fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
130
- fail(ParameterError, 'No version tag given.', caller) unless tag
135
+ raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
136
+ raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
137
+ raise(ParameterError, 'No version tag given.', caller) unless tag
138
+
131
139
  @client.get("#{domain}/templates/#{template_name}/versions/#{tag}").to_h!
132
140
  end
133
141
 
@@ -145,9 +153,10 @@ module Mailgun
145
153
  #
146
154
  # Returns [Hash] of updated template's version
147
155
  def update_version(domain, template_name, tag, options = {})
148
- fail(ParameterError, 'No domain given.', caller) unless domain
149
- fail(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
150
- fail(ParameterError, 'No version tag given.', caller) unless tag
156
+ raise(ParameterError, 'No domain given.', caller) unless domain
157
+ raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
158
+ raise(ParameterError, 'No version tag given.', caller) unless tag
159
+
151
160
  @client.put("#{domain}/templates/#{template_name}/versions/#{tag}", options).to_h
152
161
  end
153
162
 
@@ -159,11 +168,12 @@ module Mailgun
159
168
  #
160
169
  # Returns [Boolean] if successful or not
161
170
  def delete_version(domain, template_name, tag)
162
- fail(ParameterError, 'No domain given.', caller) unless domain
163
- fail(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
164
- fail(ParameterError, 'No version tag given.', caller) unless tag
171
+ raise(ParameterError, 'No domain given.', caller) unless domain
172
+ raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
173
+ raise(ParameterError, 'No version tag given.', caller) unless tag
174
+
165
175
  @client.delete("#{domain}/templates/#{template_name}/versions/#{tag}")
166
- .to_h['message'] == 'version has been deleted'
176
+ .to_h['message'] == 'version has been deleted'
167
177
  end
168
178
 
169
179
  # Public: Get Template's Versions list
@@ -177,8 +187,9 @@ module Mailgun
177
187
  #
178
188
  # Returns [Array] A list of template's versions (hash)
179
189
  def template_versions_list(domain, template_name, options = {})
180
- fail(ParameterError, 'No domain given.', caller) unless domain
181
- fail(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
190
+ raise(ParameterError, 'No domain given.', caller) unless domain
191
+ raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
192
+
182
193
  @client.get("#{domain}/templates/#{template_name}/versions", options).to_h
183
194
  end
184
195
  end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # It's the version. Yeay!
2
4
  module Mailgun
3
- VERSION = '1.4.1'
5
+ VERSION = '1.4.3'
4
6
  end
@@ -1,9 +1,10 @@
1
- module Mailgun
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mailgun
3
4
  # A Mailgun::Webhooks object is a simple CRUD interface to Mailgun Webhooks.
4
5
  # Uses Mailgun
5
6
  class Webhooks
6
- ACTIONS = %w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).freeze
7
+ ACTIONS = %w[accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed].freeze
7
8
 
8
9
  # Public creates a new Mailgun::Webhooks instance.
9
10
  # Defaults to Mailgun::Client
@@ -21,7 +22,7 @@ module Mailgun
21
22
  res = @client.get("domains/#{domain}/webhooks", options)
22
23
  res.to_h['webhooks']
23
24
  end
24
- alias_method :get_webhooks, :list
25
+ alias get_webhooks list
25
26
 
26
27
  # Public: Get webook information for a specific action
27
28
  #
@@ -36,7 +37,7 @@ module Mailgun
36
37
  rescue NoMethodError
37
38
  ''
38
39
  end
39
- alias_method :get_webhook_url, :info
40
+ alias get_webhook_url info
40
41
 
41
42
  # Public: Add webhook
42
43
  #
@@ -49,8 +50,8 @@ module Mailgun
49
50
  res = @client.post("domains/#{domain}/webhooks", id: action, url: url)
50
51
  res.to_h['webhook']['urls'].include?(url) && res.to_h['message'] == 'Webhook has been created'
51
52
  end
52
- alias_method :add, :create
53
- alias_method :add_webhook, :create
53
+ alias add create
54
+ alias add_webhook create
54
55
 
55
56
  # Public: Sets all webhooks to the same URL
56
57
  #
@@ -63,10 +64,10 @@ module Mailgun
63
64
  add_webhook domain, action, url
64
65
  end
65
66
  true
66
- rescue
67
+ rescue StandardError
67
68
  false
68
69
  end
69
- alias_method :add_all_webhooks, :create_all
70
+ alias add_all_webhooks create_all
70
71
 
71
72
  # Public: Update webhook
72
73
  #
@@ -76,12 +77,13 @@ module Mailgun
76
77
  #
77
78
  # Returns a Boolean of whether the webhook was updated
78
79
  def update(domain, action, url = '')
79
- fail Mailgun::ParameterError('Domain not provided to update webhooks') unless domain
80
- fail Mailgun::ParameterError('Action not provided to identify webhook to update') unless action
80
+ raise Mailgun::ParameterError('Domain not provided to update webhooks') unless domain
81
+ raise Mailgun::ParameterError('Action not provided to identify webhook to update') unless action
82
+
81
83
  res = @client.put("domains/#{domain}/webhooks/#{action}", id: action, url: url)
82
84
  res.to_h['webhook']['urls'] == url && res.to_h['message'] == 'Webhook has been updated'
83
85
  end
84
- alias_method :update_webhook, :update
86
+ alias update_webhook update
85
87
 
86
88
  # Public: Delete a specific webhook
87
89
  #
@@ -90,14 +92,15 @@ module Mailgun
90
92
  #
91
93
  # Returns a Boolean of the success
92
94
  def remove(domain, action)
93
- fail Mailgun::ParameterError('Domain not provided to remove webhook from') unless domain
94
- fail Mailgun::ParameterError('Action not provided to identify webhook to remove') unless action
95
+ raise Mailgun::ParameterError('Domain not provided to remove webhook from') unless domain
96
+ raise Mailgun::ParameterError('Action not provided to identify webhook to remove') unless action
97
+
95
98
  @client.delete("domains/#{domain}/webhooks/#{action}").to_h['message'] == 'Webhook has been deleted'
96
99
  rescue Mailgun::CommunicationError
97
100
  false
98
101
  end
99
- alias_method :delete, :remove
100
- alias_method :delete_webhook, :remove
102
+ alias delete remove
103
+ alias delete_webhook remove
101
104
 
102
105
  # Public: Delete all webhooks for a domain
103
106
  #
@@ -105,13 +108,13 @@ module Mailgun
105
108
  #
106
109
  # Returns a Boolean on the success
107
110
  def remove_all(domain)
108
- fail Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
111
+ raise Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
112
+
109
113
  ACTIONS.each do |action|
110
114
  delete_webhook domain, action
111
115
  end
112
116
  end
113
- alias_method :delete_all, :remove_all
114
- alias_method :delete_all_webooks, :remove_all
115
-
117
+ alias delete_all remove_all
118
+ alias delete_all_webooks remove_all
116
119
  end
117
120
  end
data/lib/mailgun-ruby.rb CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'mailgun'
2
4
  require_relative 'railgun' if defined?(Rails) && defined?(ActionMailer)