mailgun-ruby 1.2.11 → 1.2.12

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17aa99cf0b68c595bdc9a923368c206da53c5eb2798d4a4faf9def78d9216129
4
- data.tar.gz: ede5046e9ddb743b5eadff56da85a7b32049c6c6451895442f4356675c946cc9
3
+ metadata.gz: cf800982a326bb3cd2417126fc5a9de3de7ca0c4cf2c9387541244f3ee6d362d
4
+ data.tar.gz: f3515a9207e04c66d69afcaee25ee7aa746ee58f58ea4a48d2511363c1bc6005
5
5
  SHA512:
6
- metadata.gz: a9b57eb3df8f031b360871555f05681f4f585df44973844369f54f4acc0331e34977813275a36dab485d3b3c3268ce2a56d456c4b292bfd90399d226da1c66f5
7
- data.tar.gz: 99d18ff767e314c436891d4a56aa05e7037618c7aa03f3fc28d6be69ad41e49a58744f02024f94376ab9e69f0250ae3455e54d41f9b939da80cba64550ec71ea
6
+ metadata.gz: a65a2e514f1865825d8c4d7673fd2060e4dd3902402c0500e9dcf213052fff6fb7222379bd155430f30d6abf0300361228ec3cb1db53d2998b2718fb27ffb46a
7
+ data.tar.gz: 2f3988bcd4ecd490305543d3719b37f1531f796558879604263810d02d0bca068647634d8d9141f90711d1d9c4f3038ff29b385bdbe0df0537ac80bc451a05e1
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [Unreleased]
6
+
7
+ ## [1.2.12] - 2023-10-22
8
+
9
+ ### Added
10
+
11
+ - Templates CRUD support (https://github.com/mailgun/mailgun-ruby/pull/300).
12
+
13
+ ### Fixed
14
+
15
+ - transform_for_mailgun block iteration issue (https://github.com/mailgun/mailgun-ruby/pull/298).
16
+ - Typos in several files (https://github.com/mailgun/mailgun-ruby/pull/297).
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.2.11'
22
+ gem 'mailgun-ruby', '~>1.2.12'
23
23
  ```
24
24
 
25
25
  Usage
@@ -183,7 +183,7 @@ pages. Or the [Snippets](docs/Snippets.md) file.
183
183
  This SDK includes the following components:
184
184
  - [Messages](docs/Messages.md)
185
185
  - [Message Builder](docs/MessageBuilder.md)
186
- - [Batch Message](docs/MessageBuilder.md)
186
+ - [Batch Message](docs/MessageBuilder.md#usage---batch-message)
187
187
  - [Opt-In Handler](docs/OptInHandler.md)
188
188
  - [Domains](docs/Domains.md)
189
189
  - [Webhooks](docs/Webhooks.md)
@@ -56,7 +56,7 @@ module Mailgun
56
56
  # spam_action - [String] disabled, blocked or tag
57
57
  # Disable, no spam filtering will occur for inbound messages.
58
58
  # Block, inbound spam messages will not be delivered.
59
- # Tag, messages will be tagged wtih a spam header. See Spam Filter.
59
+ # Tag, messages will be tagged with a spam header. See Spam Filter.
60
60
  # wildcard - [Boolean] true or false Determines whether the domain will accept email for sub-domains.
61
61
  #
62
62
  # Returns [Hash] of created domain
@@ -88,7 +88,7 @@ module Mailgun
88
88
  # Return is irrelevant.
89
89
  def extract_paging(response)
90
90
  paging = response.to_h['paging']
91
- next_page_url = paging && paging['next'] # gives nil when any one of the keys doens't exist
91
+ next_page_url = paging && paging['next'] # gives nil when any one of the keys doesn't exist
92
92
  previous_page_url = paging && paging['previous'] # can be replaced with Hash#dig for ruby >= 2.3.0
93
93
  @paging_next = extract_endpoint_from(next_page_url)
94
94
  @paging_previous = extract_endpoint_from(previous_page_url)
@@ -125,7 +125,7 @@ module Mailgun
125
125
  add_file(:attachment, attachment, filename)
126
126
  end
127
127
 
128
- # Adds an inline image to the mesage object.
128
+ # Adds an inline image to the message object.
129
129
  #
130
130
  # @param [String|File] inline_image A file object for attaching an inline image.
131
131
  # @param [String] filename The filename you wish the inline image to be.
@@ -383,7 +383,7 @@ module Mailgun
383
383
  def bool_lookup(value)
384
384
  return 'yes' if %w(true yes yep).include? value.to_s.downcase
385
385
  return 'no' if %w(false no nope).include? value.to_s.downcase
386
- warn 'WARN: for bool type actions next values are prefered: true yes yep | false no nope | htmlonly'
386
+ warn 'WARN: for bool type actions next values are preferred: true yes yep | false no nope | htmlonly'
387
387
  value
388
388
  end
389
389
 
@@ -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,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.2.11'
3
+ VERSION = '1.2.12'
4
4
  end
data/lib/mailgun.rb CHANGED
@@ -15,6 +15,7 @@ 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'
18
19
 
19
20
  # Module for interacting with the sweet Mailgun API.
20
21
  #
@@ -145,7 +145,7 @@ module Railgun
145
145
 
146
146
  # reject blank values
147
147
  message.delete_if do |k, v|
148
- return true if v.nil?
148
+ next true if v.nil?
149
149
 
150
150
  # if it's an array remove empty elements
151
151
  v.delete_if { |i| i.respond_to?(:empty?) && i.empty? } if v.is_a?(Array)
data/mailgun.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
 
8
8
  spec.name = 'mailgun-ruby'
9
9
  spec.version = Mailgun::VERSION
10
- spec.homepage = 'http://www.mailgun.com'
10
+ spec.homepage = 'https://www.mailgun.com/'
11
11
  spec.platform = Gem::Platform::RUBY
12
12
  spec.license = 'Apache-2.0'
13
13
 
@@ -17,6 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.authors = ['Mailgun', 'Travis Swientek']
18
18
  spec.email = 'support@mailgunhq.com'
19
19
 
20
+ spec.metadata['documentation_uri'] = 'https://documentation.mailgun.com/'
21
+ spec.metadata['source_code_uri'] = 'https://github.com/mailgun/mailgun-ruby'
22
+
20
23
  spec.files = `git ls-files -z`.split("\x0")
21
24
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
25
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
@@ -15,6 +15,13 @@ describe 'For the email validation endpoint', order: :defined, vcr: vcr_opts do
15
15
  @all_addrs = @valid + @invalid
16
16
  end
17
17
 
18
+ it 'returns parsed and unparsable lists' do
19
+ res = @mg_obj.parse(@all_addrs)
20
+
21
+ expect(res["parsed"]).to eq(@valid)
22
+ expect(res["unparsable"]).to eq(@invalid)
23
+ end
24
+
18
25
  it 'validates alice@mailgun.net with info' do
19
26
  res = @mg_obj.validate("alice@mailgun.net")
20
27
 
@@ -192,5 +192,21 @@ Testing some Mailgun awesomness!'
192
192
  expect(result.body).to include("message")
193
193
  expect(result.body).to include("id")
194
194
  end
195
+
196
+ it 'receives success response code' do
197
+ @mg_obj.enable_test_mode!
198
+
199
+ expect(@mg_obj.test_mode?).to eq(true)
200
+
201
+ data = { :from => "joe@#{@domain}",
202
+ :to => "bob@#{@domain}",
203
+ :subject => "Test",
204
+ :text => "Test Data" }
205
+
206
+ result = @mg_obj.send_message(@domain, data)
207
+ result.to_h!
208
+
209
+ expect(result.success?).to be(true)
210
+ end
195
211
  end
196
212
 
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+ require 'mailgun'
3
+
4
+ vcr_opts = { :cassette_name => "templates" }
5
+
6
+ describe 'For the templates endpoints', vcr: vcr_opts do
7
+ let(:template_name) { 'test.template' }
8
+ let(:domain) { "integration-test.domain.invalid" }
9
+ let(:tag) { 'v2' }
10
+
11
+ before(:all) do
12
+ @mg_client = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
13
+ @mg_obj = Mailgun::Templates.new(@mg_client)
14
+ end
15
+
16
+ describe '#create' do
17
+ it 'creates the template' do
18
+ result = @mg_obj.create(
19
+ domain,
20
+ {
21
+ name: template_name,
22
+ description: 'Test',
23
+ template: '{{fname}} {{lname}}',
24
+ comment: 'test comment',
25
+ headers: '{"Subject": "{{subject}}"}',
26
+ tag: 'V1'
27
+ }
28
+ )
29
+
30
+ expect(result['template']["name"]).to eq('test.template')
31
+ expect(result['template']["description"]).to eq("Test")
32
+ end
33
+ end
34
+
35
+ describe '#info' do
36
+ it 'gets the templates info' do
37
+ result = @mg_obj.info(domain, 'test.template')
38
+
39
+ expect(result).to include("template")
40
+ expect(result["template"]["name"]).to eq(template_name)
41
+ end
42
+ end
43
+
44
+ describe '#update' do
45
+ it 'updates the template' do
46
+ result = @mg_obj.update(domain, template_name, { description: 'Updated Description' })
47
+
48
+ expect(result['message']).to eq('template has been updated')
49
+ end
50
+ end
51
+
52
+ describe '#list' do
53
+ it 'returns a list of templates' do
54
+ result = @mg_obj.list(domain)
55
+
56
+ expect(result.first).to have_key('name')
57
+ expect(result.first).to have_key('description')
58
+ end
59
+ end
60
+
61
+ describe '#delete' do
62
+ it 'deletes a template' do
63
+ result = @mg_obj.delete(domain, template_name)
64
+
65
+ expect(result).to be_truthy
66
+ end
67
+ end
68
+
69
+ describe '#remove_all' do
70
+ it 'deletes all templates from domain' do
71
+ result = @mg_obj.remove_all(domain)
72
+
73
+ expect(result).to be_truthy
74
+ end
75
+ end
76
+
77
+ describe '#create_version' do
78
+ it 'creates the version for the template' do
79
+ result = @mg_obj.create_version(
80
+ domain,
81
+ template_name,
82
+ {
83
+ template: '{{fname}} {{lname}}',
84
+ comment: 'test comment',
85
+ headers: '{"Subject": "{{subject}}"}',
86
+ tag: tag,
87
+ active: 'yes'
88
+ }
89
+ )
90
+
91
+ expect(result['template']["version"]['tag']).to eq(tag)
92
+ end
93
+ end
94
+
95
+ describe '#info_version' do
96
+ it "gets the template's version info" do
97
+ result = @mg_obj.info_version(domain, template_name, tag)
98
+
99
+ expect(result["template"]["version"]['tag']).to eq(tag)
100
+ end
101
+ end
102
+
103
+ describe '#update_version' do
104
+ it 'updates the template' do
105
+ result = @mg_obj.update_version(
106
+ domain,
107
+ template_name,
108
+ tag,
109
+ {
110
+ template: '{{fname}} {{lname}}',
111
+ comment: 'test comment 2',
112
+ headers: '{"Subject": "{{subject}}"}'
113
+ }
114
+ )
115
+
116
+ expect(result['message']).to eq('version has been updated')
117
+ end
118
+ end
119
+
120
+ describe '#template_versions_list' do
121
+ it "returns template's versions" do
122
+ result = @mg_obj.template_versions_list(domain, template_name)
123
+
124
+ expect(result["template"]["versions"].first).to include('tag')
125
+ end
126
+ end
127
+
128
+ describe '#delete_version' do
129
+ it "deletes template's version" do
130
+ result = @mg_obj.delete_version(domain, template_name, tag)
131
+
132
+ expect(result).to be_truthy
133
+ end
134
+ end
135
+ end
@@ -513,7 +513,7 @@ describe 'The method track_clicks' do
513
513
  end
514
514
 
515
515
  context 'when unexpected value is provided' do
516
- it 'warns about prefered values' do
516
+ it 'warns about preferred values' do
517
517
  expect(@mb_obj).to receive :warn
518
518
  @mb_obj.track_clicks('random')
519
519
  end
@@ -48,11 +48,11 @@ http_interactions:
48
48
  "Alice <alice@example.com>",
49
49
  "bob@example.com"
50
50
  ],
51
- "unparseable": [
51
+ "unparsable": [
52
52
  "example.org"
53
53
  ]
54
54
  }
55
- http_version:
55
+ http_version:
56
56
  recorded_at: Wed, 26 Oct 2016 22:44:50 GMT
57
57
  - request:
58
58
  method: get
@@ -107,7 +107,7 @@ http_interactions:
107
107
  "local_part": "alice"
108
108
  }
109
109
  }
110
- http_version:
110
+ http_version:
111
111
  recorded_at: Wed, 26 Oct 2016 22:44:50 GMT
112
112
  - request:
113
113
  method: get
@@ -162,7 +162,7 @@ http_interactions:
162
162
  "local_part": null
163
163
  }
164
164
  }
165
- http_version:
165
+ http_version:
166
166
  recorded_at: Wed, 26 Oct 2016 22:44:50 GMT
167
167
  - request:
168
168
  method: get
@@ -210,6 +210,6 @@ http_interactions:
210
210
  false, "is_role_address": false, "is_valid": true, "mailbox_verification":
211
211
  "true", "parts": {"display_name": null, "domain": "mailgun.net", "local_part":
212
212
  "alice"}}'
213
- http_version:
213
+ http_version:
214
214
  recorded_at: Tue, 12 Sep 2017 16:55:09 GMT
215
215
  recorded_with: VCR 3.0.3