mailgun-ruby 1.4.0 → 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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +2 -3
- data/Gemfile +3 -1
- data/README.md +1 -1
- data/Rakefile +5 -3
- data/docs/AnalyticsTags.md +63 -0
- data/docs/Domains.md +71 -18
- data/docs/MessageBuilder.md +1 -1
- data/lib/mailgun/address.rb +4 -6
- data/lib/mailgun/chains.rb +2 -3
- data/lib/mailgun/client.rb +71 -60
- data/lib/mailgun/domains/domains.rb +10 -96
- data/lib/mailgun/events/events.rb +3 -4
- data/lib/mailgun/exceptions/exceptions.rb +14 -15
- data/lib/mailgun/helpers/api_version_checker.rb +6 -1
- data/lib/mailgun/lists/opt_in_handler.rb +3 -8
- data/lib/mailgun/logs/logs.rb +3 -3
- data/lib/mailgun/messages/batch_message.rb +7 -10
- data/lib/mailgun/messages/message_builder.rb +35 -27
- data/lib/mailgun/metrics/metrics.rb +5 -5
- data/lib/mailgun/response.rb +11 -9
- data/lib/mailgun/subaccounts/subaccounts.rb +12 -9
- data/lib/mailgun/suppressions.rb +37 -48
- data/lib/mailgun/tags/analytics_tags.rb +63 -0
- data/lib/mailgun/tags/tags.rb +35 -18
- data/lib/mailgun/templates/templates.rb +39 -30
- data/lib/mailgun/version.rb +3 -1
- data/lib/mailgun/webhooks/webhooks.rb +22 -19
- data/lib/mailgun-ruby.rb +4 -2
- data/lib/mailgun.rb +37 -21
- data/lib/railgun/attachment.rb +9 -14
- data/lib/railgun/errors.rb +2 -3
- data/lib/railgun/mailer.rb +34 -44
- data/lib/railgun/railtie.rb +10 -1
- data/lib/railgun.rb +14 -5
- data/mailgun.gemspec +15 -14
- data/spec/integration/analytics_tags_spec.rb +54 -0
- data/spec/integration/bounces_spec.rb +12 -11
- data/spec/integration/campaign_spec.rb +20 -18
- data/spec/integration/complaints_spec.rb +8 -6
- data/spec/integration/domains_spec.rb +220 -194
- data/spec/integration/email_validation_spec.rb +35 -34
- data/spec/integration/events_spec.rb +7 -5
- data/spec/integration/list_members_spec.rb +27 -26
- data/spec/integration/list_spec.rb +22 -21
- data/spec/integration/logs_spec.rb +48 -46
- data/spec/integration/mailer_spec.rb +7 -3
- data/spec/integration/mailgun_spec.rb +82 -90
- data/spec/integration/metrics_spec.rb +130 -130
- data/spec/integration/routes_spec.rb +41 -40
- data/spec/integration/stats_spec.rb +4 -2
- data/spec/integration/subaccounts_spec.rb +9 -10
- data/spec/integration/suppressions_spec.rb +21 -20
- data/spec/integration/templates_spec.rb +14 -12
- data/spec/integration/unsubscribes_spec.rb +8 -6
- data/spec/integration/webhook_spec.rb +13 -12
- data/spec/spec_helper.rb +8 -12
- data/spec/unit/connection/test_client.rb +61 -55
- data/spec/unit/events/events_spec.rb +25 -22
- data/spec/unit/exceptions/exceptions_spec.rb +8 -7
- data/spec/unit/lists/opt_in_handler_spec.rb +8 -6
- data/spec/unit/mailgun_spec.rb +64 -63
- data/spec/unit/messages/batch_message_spec.rb +15 -15
- data/spec/unit/messages/message_builder_spec.rb +98 -94
- data/spec/unit/railgun/content_type_spec.rb +24 -23
- data/spec/unit/railgun/mailer_spec.rb +58 -58
- data/vcr_cassettes/analytics_tags.yml +187 -0
- data/vcr_cassettes/domains.yml +152 -0
- metadata +73 -46
- data/.rubocop.yml +0 -8
- data/.rubocop_todo.yml +0 -22
- data/lib/railgun/message.rb +0 -18
- data/spec/integration/tags.rb +0 -139
- data/vcr_cassettes/tags.yml +0 -417
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 45aed85ba39b57f41a049e86f7e4aa86e04092c2fc04c86f4a948857cea182c1
|
|
4
|
+
data.tar.gz: 4588e70de8bf2836028858adc294e1c1f1b5a8d07de6623a14a1c9797049eecd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 346f281a71259601f0215f33a2e7e65d2c76f5f90601522ecf06ff5f9c8c25daff473b1d8a8fc6a71474fdf6ac37c8c89c815831c74b9d1e09238392be139db7
|
|
7
|
+
data.tar.gz: 3e00ab15f716a2381735b12a792c6ddc333d32f1574c3d7b50640e293ca4c1f5a60b796c241d3e7c1447cb7b455a162a179c4167d8020bec11f21b6fa03920f1
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -11,14 +11,13 @@ permissions:
|
|
|
11
11
|
|
|
12
12
|
jobs:
|
|
13
13
|
test:
|
|
14
|
-
|
|
15
14
|
runs-on: ubuntu-latest
|
|
16
15
|
strategy:
|
|
17
16
|
matrix:
|
|
18
|
-
ruby-version: [ '
|
|
17
|
+
ruby-version: [ '3.0', '3.1', '3.2', '3.3', '3.4', '4.0' ]
|
|
19
18
|
|
|
20
19
|
steps:
|
|
21
|
-
- uses: actions/checkout@
|
|
20
|
+
- uses: actions/checkout@v6
|
|
22
21
|
- name: Set up Ruby
|
|
23
22
|
uses: ruby/setup-ruby@v1
|
|
24
23
|
with:
|
data/Gemfile
CHANGED
data/README.md
CHANGED
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
|
|
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
|
|
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
|
|
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
|
data/docs/Domains.md
CHANGED
|
@@ -9,49 +9,102 @@ all calls except credentials.
|
|
|
9
9
|
|
|
10
10
|
Usage - Domains
|
|
11
11
|
-----------------------
|
|
12
|
-
|
|
12
|
+
First, instantiate the Mailgun Client with your API key
|
|
13
13
|
```ruby
|
|
14
|
-
# First, instantiate the Mailgun Client with your API key
|
|
15
14
|
mg_client = Mailgun::Client.new('your-api-key')
|
|
16
15
|
domainer = Mailgun::Domains.new(mg_client)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Core Domain methods:
|
|
17
19
|
|
|
20
|
+
```ruby
|
|
18
21
|
# Get a list of current domains.
|
|
19
22
|
domainer.list
|
|
20
23
|
|
|
21
|
-
# View details of a domain
|
|
22
|
-
domainer.info 'my.perfect.domain'
|
|
23
|
-
|
|
24
24
|
# Add a new domain
|
|
25
25
|
domainer.create 'my.new.moreness'
|
|
26
26
|
# or with options
|
|
27
27
|
domainer.create 'my.new.moreness', { some: 'options' }
|
|
28
28
|
|
|
29
|
-
#
|
|
30
|
-
domainer.
|
|
29
|
+
# View details of a domain
|
|
30
|
+
domainer.get 'my.perfect.domain'
|
|
31
31
|
|
|
32
32
|
# Update a domain
|
|
33
33
|
domainer.update 'my.new.moreness', { some: 'options' }
|
|
34
|
+
|
|
35
|
+
# Verify a domain
|
|
36
|
+
domainer.verify 'my.perfect.domain'
|
|
37
|
+
|
|
38
|
+
# Remove a domain
|
|
39
|
+
domainer.remove 'this.one.is.not.needed.'
|
|
40
|
+
|
|
41
|
+
|
|
34
42
|
```
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
Domain Keys methods:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
# List keys for all domains
|
|
48
|
+
domainer.list_domain_keys { some: 'options' }
|
|
49
|
+
|
|
50
|
+
# Create a domain key
|
|
51
|
+
domainer.create_domain_key { some: 'options' }
|
|
52
|
+
|
|
53
|
+
# Delete a domain key
|
|
54
|
+
domainer.delete_domain_key { some: 'options' }
|
|
55
|
+
|
|
56
|
+
# Activate a domain key
|
|
57
|
+
domainer.activate_domain_key 'my.perfect.domain', 'selector'
|
|
58
|
+
|
|
59
|
+
# List domain keys
|
|
60
|
+
domainer.get_domain_keys 'my.perfect.domain'
|
|
61
|
+
|
|
62
|
+
# Deactivate a domain key
|
|
63
|
+
domainer.deactivate_domain_key 'my.perfect.domain', 'selector'
|
|
38
64
|
|
|
39
|
-
|
|
40
|
-
|
|
65
|
+
# Update DKIM authority
|
|
66
|
+
domainer.update_domain_dkim_authority 'my.perfect.domain', { some: 'options' }
|
|
67
|
+
|
|
68
|
+
# Update a DKIM selector
|
|
69
|
+
domainer.update_domain_dkim_selector 'my.perfect.domain', { some: 'options' }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Domain Tracking methods:
|
|
41
73
|
|
|
42
74
|
```ruby
|
|
43
|
-
#
|
|
44
|
-
|
|
45
|
-
supp_client = mg_client.suppressions('example.org')
|
|
75
|
+
# Get tracking settings
|
|
76
|
+
domainer.get_domain_tracking_settings 'my.perfect.domain'
|
|
46
77
|
|
|
47
|
-
#
|
|
78
|
+
# Update click tracking settings
|
|
79
|
+
domainer.update_domain_tracking_click_settings 'my.perfect.domain', { some: 'options' }
|
|
80
|
+
|
|
81
|
+
# Update open tracking settings
|
|
82
|
+
domainer.update_domain_tracking_open_settings 'my.perfect.domain', { some: 'options' }
|
|
83
|
+
|
|
84
|
+
# Update unsubscribe tracking settings
|
|
85
|
+
domainer.update_domain_tracking_unsubscribe_settings 'my.perfect.domain', { some: 'options' }
|
|
86
|
+
|
|
87
|
+
# Tracking Certificate: Get certificate and status
|
|
88
|
+
domainer.get_domain_tracking_certificate 'my.perfect.domain'
|
|
89
|
+
|
|
90
|
+
# Tracking Certificate: Regenerate expired certificate
|
|
91
|
+
domainer.regenerate_domain_tracking_certificate 'my.perfect.domain', { some: 'options' }
|
|
92
|
+
|
|
93
|
+
# Tracking Certificate: Generate
|
|
94
|
+
domainer.generate_domain_tracking_certificate 'my.perfect.domain', { some: 'options' }
|
|
48
95
|
```
|
|
49
96
|
|
|
50
|
-
|
|
51
|
-
|
|
97
|
+
DKIM Security methods:
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
# Update Automatic Sender Security DKIM key rotation for a domain
|
|
101
|
+
domainer.dkim_rotation 'my.perfect.domain', true, { some: 'options' }
|
|
52
102
|
|
|
103
|
+
# Rotate Automatic Sender Security DKIM key for a domain
|
|
104
|
+
domainer.dkim_rotate 'my.perfect.domain'
|
|
105
|
+
```
|
|
53
106
|
|
|
54
107
|
More Documentation
|
|
55
108
|
------------------
|
|
56
|
-
See the official [Mailgun Domain Docs](https://documentation.mailgun.com/
|
|
109
|
+
See the official [Mailgun Domain Docs](https://documentation.mailgun.com/docs/mailgun/api-reference/send/mailgun/domains)
|
|
57
110
|
for more information
|
data/docs/MessageBuilder.md
CHANGED
|
@@ -50,7 +50,7 @@ mb_obj.message_id("<20141014000000.11111.11111@example.com>")
|
|
|
50
50
|
mb_obj.message_id(nil)
|
|
51
51
|
mb_obj.message_id('')
|
|
52
52
|
|
|
53
|
-
# Set the Message-tags at most
|
|
53
|
+
# Set the Message-tags at most 10.
|
|
54
54
|
mb_obj.add_tag("You can track mails as tag-units!")
|
|
55
55
|
|
|
56
56
|
# Campaigns and headers.
|
data/lib/mailgun/address.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Mailgun
|
|
4
|
-
|
|
5
4
|
# Mailgun::Address is a simple interface to the Email Validation API.
|
|
6
5
|
class Address
|
|
7
6
|
def initialize(api_key = Mailgun.api_key, api_host = Mailgun.api_host)
|
|
@@ -12,12 +11,11 @@ module Mailgun
|
|
|
12
11
|
#
|
|
13
12
|
# @param [String] address Email address to validate (max 512 chars.)
|
|
14
13
|
def validate(address, mailbox_verification = false)
|
|
15
|
-
params = {address:
|
|
14
|
+
params = { address: address }
|
|
16
15
|
params[:mailbox_verification] = true if mailbox_verification
|
|
17
16
|
|
|
18
|
-
res = @client.get
|
|
19
|
-
|
|
17
|
+
res = @client.get 'address/validate', params
|
|
18
|
+
res.to_h!
|
|
20
19
|
end
|
|
21
20
|
end
|
|
22
|
-
|
|
23
21
|
end
|
data/lib/mailgun/chains.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
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
|
data/lib/mailgun/client.rb
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
require 'mailgun/suppressions'
|
|
3
|
-
require 'mailgun/exceptions/exceptions'
|
|
1
|
+
# frozen_string_literal: true
|
|
4
2
|
|
|
5
3
|
module Mailgun
|
|
6
4
|
# A Mailgun::Client object is used to communicate with the Mailgun API. It is a
|
|
@@ -9,37 +7,29 @@ module Mailgun
|
|
|
9
7
|
#
|
|
10
8
|
# See the Github documentation for full examples.
|
|
11
9
|
class Client
|
|
12
|
-
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'
|
|
10
|
+
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'
|
|
13
11
|
|
|
14
12
|
def initialize(api_key = Mailgun.api_key,
|
|
15
13
|
api_host = Mailgun.api_host || 'api.mailgun.net',
|
|
16
|
-
api_version = Mailgun.api_version
|
|
14
|
+
api_version = Mailgun.api_version || 'v3',
|
|
17
15
|
ssl = true,
|
|
18
|
-
test_mode =
|
|
16
|
+
test_mode = !Mailgun.test_mode.nil?,
|
|
19
17
|
timeout = nil,
|
|
20
18
|
proxy_url = Mailgun.proxy_url)
|
|
21
|
-
|
|
22
19
|
endpoint = endpoint_generator(api_host, api_version, ssl)
|
|
23
20
|
|
|
24
21
|
request_options = {
|
|
25
22
|
url: endpoint,
|
|
26
23
|
proxy: proxy_url,
|
|
27
|
-
ssl: {verify: ssl},
|
|
24
|
+
ssl: { verify: ssl },
|
|
28
25
|
headers: {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
|
|
27
|
+
'Accept' => '*/*'
|
|
28
|
+
}
|
|
32
29
|
}
|
|
33
|
-
request_options.merge!(request: {timeout: timeout}) if timeout
|
|
30
|
+
request_options.merge!(request: { timeout: timeout }) if timeout
|
|
34
31
|
|
|
35
|
-
@http_client =
|
|
36
|
-
conn.request :multipart
|
|
37
|
-
conn.request :authorization, :basic, 'api', api_key
|
|
38
|
-
conn.request :url_encoded
|
|
39
|
-
conn.response :raise_error, include_request: true
|
|
40
|
-
conn.adapter Faraday.default_adapter
|
|
41
|
-
conn.options.params_encoder = Faraday::FlatParamsEncoder
|
|
42
|
-
end
|
|
32
|
+
@http_client = build_http_client(api_key, request_options)
|
|
43
33
|
|
|
44
34
|
@test_mode = test_mode
|
|
45
35
|
@api_version = api_version
|
|
@@ -66,7 +56,7 @@ module Mailgun
|
|
|
66
56
|
|
|
67
57
|
# Add subaccount id to headers
|
|
68
58
|
def set_subaccount(subaccount_id)
|
|
69
|
-
@http_client.headers
|
|
59
|
+
@http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
|
|
70
60
|
end
|
|
71
61
|
|
|
72
62
|
# Reset subaccount for primary usage
|
|
@@ -82,9 +72,7 @@ module Mailgun
|
|
|
82
72
|
end
|
|
83
73
|
|
|
84
74
|
# @return [String] client api version
|
|
85
|
-
|
|
86
|
-
@api_version
|
|
87
|
-
end
|
|
75
|
+
attr_reader :api_version
|
|
88
76
|
|
|
89
77
|
# Provides a store of all the emails sent in test mode so you can check them.
|
|
90
78
|
#
|
|
@@ -102,12 +90,12 @@ module Mailgun
|
|
|
102
90
|
def send_message(working_domain, data)
|
|
103
91
|
perform_data_validation(working_domain, data)
|
|
104
92
|
|
|
105
|
-
if test_mode?
|
|
93
|
+
if test_mode?
|
|
106
94
|
Mailgun::Client.deliveries << data.dup
|
|
107
95
|
return Response.from_hash(
|
|
108
96
|
{
|
|
109
|
-
:
|
|
110
|
-
:
|
|
97
|
+
body: "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
|
|
98
|
+
status: 200
|
|
111
99
|
}
|
|
112
100
|
)
|
|
113
101
|
end
|
|
@@ -117,7 +105,7 @@ module Mailgun
|
|
|
117
105
|
# Remove nil values from the data hash
|
|
118
106
|
# Submitting nils to the API will likely cause an error.
|
|
119
107
|
# See also: https://github.com/mailgun/mailgun-ruby/issues/32
|
|
120
|
-
data = data.
|
|
108
|
+
data = data.reject { |_k, v| v.nil? }
|
|
121
109
|
|
|
122
110
|
if data.key?(:message)
|
|
123
111
|
if data[:message].is_a?(String)
|
|
@@ -130,7 +118,7 @@ module Mailgun
|
|
|
130
118
|
when MessageBuilder
|
|
131
119
|
post("#{working_domain}/messages", data.message)
|
|
132
120
|
else
|
|
133
|
-
|
|
121
|
+
raise ParameterError.new('Unknown data type for data parameter.', data)
|
|
134
122
|
end
|
|
135
123
|
end
|
|
136
124
|
|
|
@@ -145,8 +133,8 @@ module Mailgun
|
|
|
145
133
|
def post(resource_path, data, headers = {})
|
|
146
134
|
response = @http_client.post(resource_path, data, headers)
|
|
147
135
|
Response.new(response)
|
|
148
|
-
rescue =>
|
|
149
|
-
raise communication_error
|
|
136
|
+
rescue StandardError => e
|
|
137
|
+
raise communication_error e
|
|
150
138
|
end
|
|
151
139
|
|
|
152
140
|
# Generic Mailgun GET Handler
|
|
@@ -162,8 +150,8 @@ module Mailgun
|
|
|
162
150
|
response = @http_client.get(resource_path, params, headers)
|
|
163
151
|
|
|
164
152
|
Response.new(response)
|
|
165
|
-
rescue =>
|
|
166
|
-
raise communication_error(
|
|
153
|
+
rescue StandardError => e
|
|
154
|
+
raise communication_error(e)
|
|
167
155
|
end
|
|
168
156
|
|
|
169
157
|
# Generic Mailgun PUT Handler
|
|
@@ -173,11 +161,11 @@ module Mailgun
|
|
|
173
161
|
# @param [Hash] data This should be a standard Hash
|
|
174
162
|
# containing required parameters for the requested resource.
|
|
175
163
|
# @return [Mailgun::Response] A Mailgun::Response object.
|
|
176
|
-
def put(resource_path, data)
|
|
177
|
-
response = @http_client.put(resource_path, data)
|
|
164
|
+
def put(resource_path, data, headers = {})
|
|
165
|
+
response = @http_client.put(resource_path, data, headers)
|
|
178
166
|
Response.new(response)
|
|
179
|
-
rescue =>
|
|
180
|
-
raise communication_error
|
|
167
|
+
rescue StandardError => e
|
|
168
|
+
raise communication_error e
|
|
181
169
|
end
|
|
182
170
|
|
|
183
171
|
# Generic Mailgun DELETE Handler
|
|
@@ -185,15 +173,22 @@ module Mailgun
|
|
|
185
173
|
# @param [String] resource_path This is the API resource you wish to interact
|
|
186
174
|
# with. Be sure to include your domain, where necessary.
|
|
187
175
|
# @return [Mailgun::Response] A Mailgun::Response object.
|
|
188
|
-
def delete(resource_path, params = nil)
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
176
|
+
def delete(resource_path, params = nil, body_params = false)
|
|
177
|
+
response =
|
|
178
|
+
if params
|
|
179
|
+
if body_params
|
|
180
|
+
@http_client.delete(resource_path) do |request|
|
|
181
|
+
request.body = params.to_json
|
|
182
|
+
end
|
|
183
|
+
else
|
|
184
|
+
@http_client.delete(resource_path, params: params)
|
|
185
|
+
end
|
|
186
|
+
else
|
|
187
|
+
@http_client.delete(resource_path)
|
|
188
|
+
end
|
|
194
189
|
Response.new(response)
|
|
195
|
-
rescue =>
|
|
196
|
-
raise communication_error
|
|
190
|
+
rescue StandardError => e
|
|
191
|
+
raise communication_error e
|
|
197
192
|
end
|
|
198
193
|
|
|
199
194
|
# Constructs a Suppressions client for the given domain.
|
|
@@ -225,7 +220,7 @@ module Mailgun
|
|
|
225
220
|
# @param [Boolean] ssl True, SSL. False, No SSL.
|
|
226
221
|
# @return [string] concatenated URL string
|
|
227
222
|
def endpoint_generator(api_host, api_version, ssl)
|
|
228
|
-
ssl ?
|
|
223
|
+
scheme = ssl ? 'https' : 'http'
|
|
229
224
|
if api_version
|
|
230
225
|
"#{scheme}://#{api_host}/#{api_version}"
|
|
231
226
|
else
|
|
@@ -239,28 +234,44 @@ module Mailgun
|
|
|
239
234
|
def communication_error(e)
|
|
240
235
|
if e.respond_to?(:response) && e.response
|
|
241
236
|
return case e.response_status
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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
|
|
249
244
|
end
|
|
250
245
|
CommunicationError.new(e.message)
|
|
251
246
|
end
|
|
252
247
|
|
|
253
248
|
def perform_data_validation(working_domain, data)
|
|
254
249
|
message = data.respond_to?(:message) ? data.message : data
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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(
|
|
261
261
|
'Missing a `from` sender, message should contain at least 1 `from` sender',
|
|
262
262
|
working_domain
|
|
263
|
-
)
|
|
263
|
+
)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def build_http_client(api_key, request_options)
|
|
267
|
+
Faraday.new(request_options) do |conn|
|
|
268
|
+
conn.request :multipart
|
|
269
|
+
conn.request :authorization, :basic, 'api', api_key
|
|
270
|
+
conn.request :url_encoded
|
|
271
|
+
conn.response :raise_error, include_request: true
|
|
272
|
+
conn.adapter Faraday.default_adapter
|
|
273
|
+
conn.options.params_encoder = Faraday::FlatParamsEncoder
|
|
274
|
+
end
|
|
264
275
|
end
|
|
265
276
|
end
|
|
266
277
|
end
|