mailgun-ruby 1.1.9 → 1.2.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +0 -0
  3. data/.rubocop_todo.yml +0 -0
  4. data/.ruby-env.yml.example +1 -0
  5. data/.travis.yml +6 -5
  6. data/CHANGELOG.md +16 -0
  7. data/Gemfile +1 -1
  8. data/README.md +30 -3
  9. data/docs/Domains.md +3 -0
  10. data/docs/EmailValidation.md +34 -0
  11. data/docs/OptInHandler.md +1 -1
  12. data/docs/Snippets.md +54 -61
  13. data/docs/Subaccounts.md +68 -0
  14. data/docs/Suppressions.md +10 -0
  15. data/docs/Webhooks.md +0 -0
  16. data/docs/railgun/EmailValidation.md +34 -0
  17. data/docs/railgun/Overview.md +11 -0
  18. data/docs/railgun/Parameters.md +83 -0
  19. data/docs/railgun/Templates.md +92 -0
  20. data/lib/mailgun/address.rb +3 -28
  21. data/lib/mailgun/chains.rb +0 -0
  22. data/lib/mailgun/client.rb +55 -9
  23. data/lib/mailgun/domains/domains.rb +20 -2
  24. data/lib/mailgun/events/events.rb +2 -2
  25. data/lib/mailgun/exceptions/exceptions.rb +28 -1
  26. data/lib/mailgun/messages/batch_message.rb +1 -0
  27. data/lib/mailgun/messages/message_builder.rb +56 -8
  28. data/lib/mailgun/response.rb +7 -0
  29. data/lib/mailgun/subaccounts/subaccounts.rb +84 -0
  30. data/lib/mailgun/suppressions.rb +15 -8
  31. data/lib/mailgun/templates/templates.rb +187 -0
  32. data/lib/mailgun/version.rb +1 -1
  33. data/lib/mailgun/webhooks/webhooks.rb +2 -2
  34. data/lib/mailgun-ruby.rb +1 -1
  35. data/lib/mailgun.rb +5 -1
  36. data/lib/railgun/mailer.rb +85 -12
  37. data/lib/railgun/message.rb +2 -1
  38. data/lib/railgun/railtie.rb +3 -2
  39. data/mailgun.gemspec +15 -12
  40. data/spec/integration/bounces_spec.rb +3 -3
  41. data/spec/integration/campaign_spec.rb +0 -0
  42. data/spec/integration/complaints_spec.rb +0 -0
  43. data/spec/integration/domains_spec.rb +8 -0
  44. data/spec/integration/email_validation_spec.rb +10 -2
  45. data/spec/integration/events_spec.rb +1 -1
  46. data/spec/integration/list_members_spec.rb +0 -0
  47. data/spec/integration/list_spec.rb +0 -0
  48. data/spec/integration/mailer_spec.rb +67 -0
  49. data/spec/integration/mailgun_spec.rb +92 -1
  50. data/spec/integration/routes_spec.rb +0 -0
  51. data/spec/integration/stats_spec.rb +0 -0
  52. data/spec/integration/subaccounts_spec.rb +58 -0
  53. data/spec/integration/suppressions_spec.rb +18 -2
  54. data/spec/integration/templates_spec.rb +135 -0
  55. data/spec/integration/unsubscribes_spec.rb +0 -0
  56. data/spec/integration/webhook_spec.rb +0 -0
  57. data/spec/spec_helper.rb +3 -1
  58. data/spec/unit/connection/test_client.rb +18 -1
  59. data/spec/unit/events/events_spec.rb +19 -0
  60. data/spec/unit/mailgun_spec.rb +43 -2
  61. data/spec/unit/messages/batch_message_spec.rb +56 -40
  62. data/spec/unit/messages/message_builder_spec.rb +149 -16
  63. data/spec/unit/messages/sample_data/unknown.type +0 -0
  64. data/spec/unit/railgun/mailer_spec.rb +388 -0
  65. data/vcr_cassettes/bounces.yml +12 -12
  66. data/vcr_cassettes/complaints.yml +0 -0
  67. data/vcr_cassettes/domains.todo.yml +0 -0
  68. data/vcr_cassettes/domains.yml +51 -1
  69. data/vcr_cassettes/email_validation.yml +5 -5
  70. data/vcr_cassettes/events.yml +0 -0
  71. data/vcr_cassettes/exceptions-invalid-api-key.yml +52 -0
  72. data/vcr_cassettes/exceptions-invalid-data.yml +52 -0
  73. data/vcr_cassettes/exceptions-not-allowed.yml +54 -0
  74. data/vcr_cassettes/list_members.yml +0 -0
  75. data/vcr_cassettes/mailer_invalid_domain.yml +109 -0
  76. data/vcr_cassettes/mailing_list.todo.yml +0 -0
  77. data/vcr_cassettes/mailing_list.yml +0 -0
  78. data/vcr_cassettes/message_deliver.yml +149 -0
  79. data/vcr_cassettes/routes.yml +0 -0
  80. data/vcr_cassettes/send_message.yml +0 -0
  81. data/vcr_cassettes/stats.yml +0 -0
  82. data/vcr_cassettes/subaccounts.yml +270 -0
  83. data/vcr_cassettes/suppressions.yml +66 -15
  84. data/vcr_cassettes/templates.yml +1065 -0
  85. data/vcr_cassettes/unsubscribes.yml +0 -0
  86. data/vcr_cassettes/webhooks.yml +0 -0
  87. metadata +56 -29
  88. data/.ruby-version +0 -1
  89. /data/spec/unit/{railgun_spec.rb → railgun/content_type_spec.rb} +0 -0
@@ -1,3 +1,4 @@
1
+ require 'mime/types'
1
2
  require 'time'
2
3
 
3
4
  module Mailgun
@@ -124,7 +125,7 @@ module Mailgun
124
125
  add_file(:attachment, attachment, filename)
125
126
  end
126
127
 
127
- # Adds an inline image to the mesage object.
128
+ # Adds an inline image to the message object.
128
129
  #
129
130
  # @param [String|File] inline_image A file object for attaching an inline image.
130
131
  # @param [String] filename The filename you wish the inline image to be.
@@ -197,7 +198,9 @@ module Mailgun
197
198
  # @param [Boolean] tracking Boolean true or false.
198
199
  # @return [void]
199
200
  def track_opens(mode)
200
- set_multi_simple('o:tracking-opens', bool_lookup(mode))
201
+ value = bool_lookup(mode)
202
+ set_single('o:tracking-opens', value)
203
+ set_multi_simple('o:tracking', value)
201
204
  end
202
205
 
203
206
  # Deprecated: 'set_open_tracking' is deprecated. Please use 'track_opens' instead.
@@ -211,7 +214,9 @@ module Mailgun
211
214
  # @param [String] mode True, False, or HTML (for HTML only tracking)
212
215
  # @return [void]
213
216
  def track_clicks(mode)
214
- set_multi_simple('o:tracking-clicks', bool_lookup(mode))
217
+ value = bool_lookup(mode)
218
+ set_single('o:tracking-clicks', value)
219
+ set_multi_simple('o:tracking', value)
215
220
  end
216
221
 
217
222
  # Depreciated: 'set_click_tracking. is deprecated. Please use 'track_clicks' instead.
@@ -269,8 +274,12 @@ module Mailgun
269
274
  # @return [void]
270
275
  def variable(name, data)
271
276
  fail(Mailgun::ParameterError, 'Variable name must be specified') if name.to_s.empty?
272
- jsondata = make_json data
273
- set_single("v:#{name}", jsondata)
277
+ begin
278
+ jsondata = make_json data
279
+ set_single("v:#{name}", jsondata)
280
+ rescue Mailgun::ParameterError
281
+ set_single("v:#{name}", data)
282
+ end
274
283
  end
275
284
 
276
285
  # Add custom parameter to the message. A custom parameter is any parameter that
@@ -303,6 +312,38 @@ module Mailgun
303
312
  message_id data
304
313
  end
305
314
 
315
+ # Set name of a template stored via template API. See Templates for more information
316
+ # https://documentation.mailgun.com/en/latest/api-templates.html
317
+ #
318
+ # @param [String] tag A defined template name to use. Passing nil or
319
+ # empty string will delete template key and value from @message hash.
320
+ # @return [void]
321
+ def template(template_name = nil)
322
+ key = 'template'
323
+ return @message.delete(key) if template_name.to_s.empty?
324
+ set_single(key, template_name)
325
+ end
326
+
327
+ # Set specific template version.
328
+ #
329
+ # @param [String] tag A defined template name to use. Passing nil or
330
+ # empty string will delete template key and value from @message hash.
331
+ # @return [void]
332
+ def template_version(version = nil)
333
+ key = 't:version'
334
+ return @message.delete(key) if version.to_s.empty?
335
+ set_single(key, version)
336
+ end
337
+
338
+ # Turn off or on template rendering in the text part
339
+ # of the message in case of template sending.
340
+ #
341
+ # @param [Boolean] tracking Boolean true or false.
342
+ # @return [void]
343
+ def template_text(mode)
344
+ set_single('t:text', bool_lookup(mode))
345
+ end
346
+
306
347
  private
307
348
 
308
349
  # Sets a single value in the message hash where "multidict" features are not needed.
@@ -342,6 +383,7 @@ module Mailgun
342
383
  def bool_lookup(value)
343
384
  return 'yes' if %w(true yes yep).include? value.to_s.downcase
344
385
  return 'no' if %w(false no nope).include? value.to_s.downcase
386
+ warn 'WARN: for bool type actions next values are preferred: true yes yep | false no nope | htmlonly'
345
387
  value
346
388
  end
347
389
 
@@ -364,7 +406,7 @@ module Mailgun
364
406
  def make_json(obj)
365
407
  return JSON.parse(obj).to_json if obj.is_a?(String)
366
408
  return obj.to_json if obj.is_a?(Hash)
367
- return JSON.generate(obj).to_json
409
+ JSON.generate(obj).to_json
368
410
  rescue
369
411
  raise Mailgun::ParameterError, 'Provided data could not be made into JSON. Try a JSON string or Hash.', obj
370
412
  end
@@ -387,9 +429,9 @@ module Mailgun
387
429
  full_name = vars['full_name']
388
430
  elsif vars['first'] || vars['last']
389
431
  full_name = "#{vars['first']} #{vars['last']}".strip
390
- end
432
+ end
391
433
 
392
- return "'#{full_name}' <#{address}>" if defined?(full_name)
434
+ return "'#{full_name}' <#{address}>" if full_name
393
435
  address
394
436
  end
395
437
 
@@ -409,6 +451,12 @@ module Mailgun
409
451
  'Unable to access attachment file object.'
410
452
  ) unless attachment.respond_to?(:read)
411
453
 
454
+ if attachment.respond_to?(:path) && !attachment.respond_to?(:content_type)
455
+ mime_types = MIME::Types.type_for(attachment.path)
456
+ content_type = mime_types.empty? ? 'application/octet-stream' : mime_types[0].content_type
457
+ attachment.instance_eval "def content_type; '#{content_type}'; end"
458
+ end
459
+
412
460
  unless filename.nil?
413
461
  attachment.instance_variable_set :@original_filename, filename
414
462
  attachment.instance_eval 'def original_filename; @original_filename; end'
@@ -58,5 +58,12 @@ module Mailgun
58
58
  rescue => err
59
59
  raise ParseError.new(err), err
60
60
  end
61
+
62
+ # Returns true if response code is 2xx
63
+ #
64
+ # @return [Boolean] A boolean that binarizes the response code result.
65
+ def success?
66
+ (200..299).include?(code)
67
+ end
61
68
  end
62
69
  end
@@ -0,0 +1,84 @@
1
+ require 'mailgun/exceptions/exceptions'
2
+
3
+ module Mailgun
4
+
5
+ # A Mailgun::Subaccounts object is a simple CRUD interface to Mailgun Subaccounts.
6
+ # Uses Mailgun
7
+ class Subaccounts
8
+ attr_reader :client
9
+
10
+ # Public: creates a new Mailgun::Subaccounts instance.
11
+ # Defaults to Mailgun::Client
12
+ def initialize(client = Mailgun::Client.new(Mailgun.api_key, Mailgun.api_host || 'api.mailgun.net', 'v5'))
13
+ @client = client
14
+ end
15
+
16
+ # Public: Get subaccounts
17
+ # options - [Hash] of
18
+ # limit - [Integer] Maximum number of records to return. (10 by default)
19
+ # skip [Integer] Number of records to skip
20
+ # sort [Array] “asc” or “desc”
21
+ # enabled [boolean] (Optional) Returns all enabled/disabled subaccounts, defaults to all if omitted
22
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
23
+ # ex.('{"Subject": "{{subject}}"}')
24
+ #
25
+ # Returns [Array] A list of subaccounts (hash)
26
+ def list(options = {})
27
+ client.get("accounts/subaccounts", options).to_h!
28
+ end
29
+ alias_method :get_subaccounts, :list
30
+
31
+ # Public: Get subaccount information
32
+ #
33
+ # subaccount_id - [String] subaccount name to lookup for
34
+ # options - [Hash] of
35
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
36
+ # ex.('{"Subject": "{{subject}}"}')
37
+ #
38
+ # Returns [Hash] Information on the requested subaccount.
39
+ def info(subaccount_id, options = {})
40
+ fail(ParameterError, 'No Id of subaccount specified', caller) unless subaccount_id
41
+ client.get("accounts/subaccounts/#{subaccount_id}", options).to_h!
42
+ end
43
+
44
+ # Public: Add Subaccount
45
+ #
46
+ # name - [String] Name of the subaccount being created
47
+ # options - [Hash] of
48
+ # name - [String] Name of the subaccount being created.
49
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
50
+ # ex.('{"Subject": "{{subject}}"}')
51
+ #
52
+ # Returns [Hash] of created subaccount
53
+ def create(name, options = {})
54
+ fail(ParameterError, 'No name given to create subaccount', caller) unless name
55
+ client.post("accounts/subaccounts", options.merge!(name: name)).to_h!
56
+ end
57
+
58
+ # Public: Disable a subaccount
59
+ #
60
+ # subaccount_id - [String] subaccount name to disable
61
+ # options - [Hash] of
62
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
63
+ # ex.('{"Subject": "{{subject}}"}')
64
+ #
65
+ # Returns [Hash] Information on the requested subaccount.
66
+ def disable(subaccount_id, options = {})
67
+ fail(ParameterError, 'No Id of subaccount specified', caller) unless subaccount_id
68
+ client.post("accounts/subaccounts/#{subaccount_id}/disable", options).to_h!
69
+ end
70
+
71
+ # Public: Enable a subaccount
72
+ #
73
+ # subaccount_id - [String] subaccount name to enable
74
+ # options - [Hash] of
75
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
76
+ # ex.('{"Subject": "{{subject}}"}')
77
+ #
78
+ # Returns [Hash] Information on the requested subaccount.
79
+ def enable(subaccount_id, options = {})
80
+ fail(ParameterError, 'No Id of subaccount specified', caller) unless subaccount_id
81
+ client.post("accounts/subaccounts/#{subaccount_id}/enable", options).to_h!
82
+ end
83
+ end
84
+ end
@@ -45,11 +45,11 @@ module Mailgun
45
45
  end
46
46
 
47
47
  def get_bounce(address)
48
- @client.get("#{@domain}/bounces/#{address}", nil)
48
+ @client.get("#{@domain}/bounces/#{escape_address(address)}", nil)
49
49
  end
50
50
 
51
51
  def create_bounce(params = {})
52
- @client.post("#{@domain/bounces}", params)
52
+ @client.post("#{@domain}/bounces", params)
53
53
  end
54
54
 
55
55
  # Creates multiple bounces on the Mailgun API.
@@ -100,7 +100,7 @@ module Mailgun
100
100
  end
101
101
 
102
102
  def delete_bounce(address)
103
- @client.delete("#{@domain}/bounces/#{address}")
103
+ @client.delete("#{@domain}/bounces/#{escape_address(address)}")
104
104
  end
105
105
 
106
106
  def delete_all_bounces
@@ -118,7 +118,7 @@ module Mailgun
118
118
  end
119
119
 
120
120
  def get_unsubscribe(address)
121
- @client.get("#{@domain}/unsubscribes/#{address}")
121
+ @client.get("#{@domain}/unsubscribes/#{escape_address(address)}")
122
122
  end
123
123
 
124
124
  def create_unsubscribe(params = {})
@@ -157,7 +157,10 @@ module Mailgun
157
157
 
158
158
  unsubscribe.each do |k, v|
159
159
  # Hash values MUST be strings.
160
- if not v.is_a? String then
160
+ # However, unsubscribes contain an array of tags
161
+ if v.is_a? Array
162
+ unsubscribe[k] = v.map(&:to_s)
163
+ elsif !v.is_a? String
161
164
  unsubscribe[k] = v.to_s
162
165
  end
163
166
  end
@@ -170,7 +173,7 @@ module Mailgun
170
173
  end
171
174
 
172
175
  def delete_unsubscribe(address, params = {})
173
- @client.delete("#{@domain}/unsubscribes/#{address}")
176
+ @client.delete("#{@domain}/unsubscribes/#{escape_address(address)}")
174
177
  end
175
178
 
176
179
  ####
@@ -184,7 +187,7 @@ module Mailgun
184
187
  end
185
188
 
186
189
  def get_complaint(address)
187
- @client.get("#{@domain}/complaints/#{address}", nil)
190
+ @client.get("#{@domain}/complaints/#{escape_address(address)}", nil)
188
191
  end
189
192
 
190
193
  def create_complaint(params = {})
@@ -236,11 +239,15 @@ module Mailgun
236
239
  end
237
240
 
238
241
  def delete_complaint(address)
239
- @client.delete("#{@domain}/complaints/#{address}")
242
+ @client.delete("#{@domain}/complaints/#{escape_address(address)}")
240
243
  end
241
244
 
242
245
  private
243
246
 
247
+ def escape_address(address)
248
+ CGI.escape address
249
+ end
250
+
244
251
  def get_from_paging(uri, params = {})
245
252
  @client.get(uri, params)
246
253
  end
@@ -0,0 +1,187 @@
1
+ require 'mailgun/exceptions/exceptions'
2
+
3
+ module Mailgun
4
+
5
+ # A Mailgun::Templates object is a simple CRUD interface to Mailgun Templates.
6
+ # Uses Mailgun
7
+ class Templates
8
+
9
+ # Public: creates a new Mailgun::Templates instance.
10
+ # Defaults to Mailgun::Client
11
+ def initialize(client = Mailgun::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Public: Add template
16
+ #
17
+ # domain - [String] Name of the domain for new template(ex. domain.com)
18
+ # options - [Hash] of
19
+ # name - [String] Name of the template being stored.
20
+ # description - [String] Description of the template being stored
21
+ # template - [String] (Optional) Content of the template
22
+ # tag - [String] (Optional) Initial tag of the created version.
23
+ # comment - [String] (Optional) Version comment.
24
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the template.
25
+ # ex.('{"Subject": "{{subject}}"}')
26
+ #
27
+ # Returns [Hash] of created template
28
+ def create(domain, options = {})
29
+ fail(ParameterError, 'No domain given to store template on', caller) unless domain
30
+ @client.post("#{domain}/templates", options).to_h
31
+ end
32
+
33
+ # Public: Get template information
34
+ #
35
+ # domain - [String] Domain name where template is stored
36
+ # template_name - [String] Template name to lookup for
37
+ # options - [Hash] of
38
+ # active - [Boolean] (Optional) If this flag is set to yes the active version
39
+ # of the template is included in the response.
40
+ #
41
+ # Returns [Hash] Information on the requested template.
42
+ def info(domain, template_name, options = {})
43
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
44
+ fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
45
+ @client.get("#{domain}/templates/#{template_name}", options).to_h!
46
+ end
47
+
48
+ # Public: Update the metadata information of the template
49
+ #
50
+ # domain - [String] Domain name where template is stored
51
+ # template_name - [String] Template name to lookup for
52
+ # options - [Hash] of
53
+ # description - [String] Updated description of the template
54
+ #
55
+ # Returns [Hash] of updated domain
56
+ def update(domain, template_name, options = {})
57
+ fail(ParameterError, 'No domain given to add on Mailgun', caller) unless domain
58
+ fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
59
+ @client.put("#{domain}/templates/#{template_name}", options).to_h
60
+ end
61
+
62
+ # Public: Delete Template
63
+ # NOTE: This method deletes all versions of the specified template.
64
+ #
65
+ # domain - [String] Domain name where template is stored
66
+ # template_name - [String] Template name to lookup for
67
+ #
68
+ # Returns [Boolean] if successful or not
69
+ def remove(domain, template_name)
70
+ fail(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
71
+ fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
72
+ @client.delete("#{domain}/templates/#{template_name}").to_h['message'] == 'template has been deleted'
73
+ end
74
+ alias_method :delete, :remove
75
+ alias_method :delete_template, :remove
76
+
77
+ # Public: Get Templates
78
+ #
79
+ # domain - [String] Domain name where template is stored
80
+ # page - [String] Name of a page to retrieve. first, last, next, prev
81
+ # limit - [Integer] Maximum number of records to return. (100 by default)
82
+ # p - [Integer] Pivot is used to retrieve records in chronological order
83
+ #
84
+ # Returns [Array] A list of templates (hash)
85
+ def list(domain, options = {})
86
+ fail(ParameterError, 'No domain given.', caller) unless domain
87
+ @client.get("#{domain}/templates", options).to_h['items']
88
+ end
89
+ alias_method :get_templates, :list
90
+
91
+ # Public: Delete Templates
92
+ # NOTE: This method deletes all stored templates for the domain.
93
+ #
94
+ # domain - [String] Domain name where template is stored
95
+ #
96
+ # Returns [Boolean] if successful or not
97
+ def remove_all(domain)
98
+ fail(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
99
+ @client.delete("#{domain}/templates").to_h['message'] == 'templates have been deleted'
100
+ end
101
+ alias_method :delete_templates, :remove_all
102
+
103
+ # Public: Create a new version of a template
104
+ #
105
+ # domain - [String] Name of the domain for new template(ex. domain.com)
106
+ # template_name - [String] Template name to lookup for
107
+ # options - [Hash] of
108
+ # template - [String] Content of the template
109
+ # tag - [String] Initial tag of the created version.
110
+ # comment - [String] (Optional) Version comment.
111
+ # active - [Boolean] (Optional) If this flag is set to yes, this version becomes active
112
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the template.
113
+ # ex.('{"Subject": "{{subject}}"}')
114
+ #
115
+ # Returns [Hash] of updated template
116
+ def create_version(domain, template_name, options = {})
117
+ fail(ParameterError, 'No domain given.', caller) unless domain
118
+ fail(ParameterError, 'No template name given.', caller) unless template_name
119
+ @client.post("#{domain}/templates/#{template_name}/versions", options).to_h
120
+ end
121
+
122
+ # Public: Get template version information
123
+ #
124
+ # domain - [String] Domain name where template is stored
125
+ # template_name - [String] Template name to lookup for
126
+ # tag - [String] Version tag to lookup for
127
+ #
128
+ # Returns [Hash] Information on the requested template + version.
129
+ def info_version(domain, template_name, tag)
130
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
131
+ fail(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
132
+ fail(ParameterError, 'No version tag given.', caller) unless tag
133
+ @client.get("#{domain}/templates/#{template_name}/versions/#{tag}").to_h!
134
+ end
135
+
136
+ # Public: Update the version of the template
137
+ #
138
+ # domain - [String] Domain name where template is stored
139
+ # template_name - [String] Template name to lookup for
140
+ # tag - [String] Version tag to lookup for
141
+ # options - [Hash] of
142
+ # template - [String] Content of the template
143
+ # comment - [String] (Optional) Version comment.
144
+ # active - [Boolean] (Optional) If this flag is set to yes, this version becomes active
145
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the template.
146
+ # ex.('{"Subject": "{{subject}}"}')
147
+ #
148
+ # Returns [Hash] of updated template's version
149
+ def update_version(domain, template_name, tag, options = {})
150
+ fail(ParameterError, 'No domain given.', caller) unless domain
151
+ fail(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
152
+ fail(ParameterError, 'No version tag given.', caller) unless tag
153
+ @client.put("#{domain}/templates/#{template_name}/versions/#{tag}", options).to_h
154
+ end
155
+
156
+ # Public: Delete the version of the template
157
+ #
158
+ # domain - [String] Domain name where template is stored
159
+ # template_name - [String] Template name to lookup for
160
+ # tag - [String] Version tag to lookup for
161
+ #
162
+ # Returns [Boolean] if successful or not
163
+ def delete_version(domain, template_name, tag)
164
+ fail(ParameterError, 'No domain given.', caller) unless domain
165
+ fail(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
166
+ fail(ParameterError, 'No version tag given.', caller) unless tag
167
+ @client.delete("#{domain}/templates/#{template_name}/versions/#{tag}")
168
+ .to_h['message'] == 'version has been deleted'
169
+ end
170
+
171
+ # Public: Get Template's Versions list
172
+ #
173
+ # domain - [String] Domain name where template is stored
174
+ # template_name - [String] Template name to lookup for
175
+ # options - [Hash] of
176
+ # page - [String] Name of a page to retrieve. first, last, next, prev
177
+ # limit - [Integer] Maximum number of records to return. (100 by default)
178
+ # p - [Integer] Pivot is used to retrieve records in chronological order
179
+ #
180
+ # Returns [Array] A list of template's versions (hash)
181
+ def template_versions_list(domain, template_name, options = {})
182
+ fail(ParameterError, 'No domain given.', caller) unless domain
183
+ fail(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
184
+ @client.get("#{domain}/templates/#{template_name}/versions", options).to_h
185
+ end
186
+ end
187
+ end
@@ -1,4 +1,4 @@
1
1
  # It's the version. Yeay!
2
2
  module Mailgun
3
- VERSION = '1.1.9'
3
+ VERSION = '1.2.13'
4
4
  end
@@ -58,7 +58,7 @@ module Mailgun
58
58
  #
59
59
  # Returns true or false
60
60
  def create_all(domain, url = '')
61
- %w(bounce click deliver drop open spam unsubscribe).each do |action|
61
+ %w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).each do |action|
62
62
  add_webhook domain, action, url
63
63
  end
64
64
  true
@@ -90,7 +90,7 @@ module Mailgun
90
90
  # Returns a Boolean on the success
91
91
  def remove_all(domain)
92
92
  fail Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
93
- %w(bounce click deliver drop open spam unsubscribe).each do |action|
93
+ %w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).each do |action|
94
94
  delete_webhook domain, action
95
95
  end
96
96
  end
data/lib/mailgun-ruby.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  require 'mailgun'
2
- require 'railgun' if defined?(Rails)
2
+ require 'railgun' if defined?(Rails) && defined?(ActionMailer)
data/lib/mailgun.rb CHANGED
@@ -15,6 +15,8 @@ require 'mailgun/events/events'
15
15
  require 'mailgun/exceptions/exceptions'
16
16
  require 'mailgun/domains/domains'
17
17
  require 'mailgun/webhooks/webhooks'
18
+ require 'mailgun/templates/templates'
19
+ require 'mailgun/subaccounts/subaccounts'
18
20
 
19
21
  # Module for interacting with the sweet Mailgun API.
20
22
  #
@@ -22,10 +24,12 @@ require 'mailgun/webhooks/webhooks'
22
24
  module Mailgun
23
25
 
24
26
  class << self
25
- attr_accessor :api_key,
27
+ attr_accessor :api_host,
28
+ :api_key,
26
29
  :api_version,
27
30
  :protocol,
28
31
  :mailgun_host,
32
+ :proxy_url,
29
33
  :test_mode,
30
34
  :domain
31
35