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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +30 -8
- data/.rubocop.yml +64 -4
- data/Gemfile +3 -1
- data/README.md +1 -1
- data/Rakefile +5 -8
- data/docs/AnalyticsTags.md +63 -0
- data/lib/mailgun/address.rb +5 -5
- data/lib/mailgun/chains.rb +2 -3
- data/lib/mailgun/client.rb +56 -56
- data/lib/mailgun/domains/domains.rb +11 -10
- data/lib/mailgun/events/events.rb +4 -3
- data/lib/mailgun/exceptions/exceptions.rb +12 -15
- data/lib/mailgun/helpers/api_version_checker.rb +6 -1
- data/lib/mailgun/lists/opt_in_handler.rb +6 -10
- data/lib/mailgun/logs/logs.rb +4 -2
- data/lib/mailgun/messages/batch_message.rb +10 -10
- data/lib/mailgun/messages/message_builder.rb +40 -56
- data/lib/mailgun/metrics/metrics.rb +12 -6
- data/lib/mailgun/response.rb +12 -10
- data/lib/mailgun/subaccounts/subaccounts.rb +13 -8
- data/lib/mailgun/suppressions.rb +36 -43
- data/lib/mailgun/tags/analytics_tags.rb +37 -2
- data/lib/mailgun/tags/tags.rb +29 -19
- data/lib/mailgun/templates/templates.rb +40 -29
- data/lib/mailgun/version.rb +3 -1
- data/lib/mailgun/webhooks/webhooks.rb +22 -19
- data/lib/mailgun-ruby.rb +2 -0
- data/lib/mailgun.rb +4 -4
- data/lib/railgun/attachment.rb +12 -19
- data/lib/railgun/errors.rb +2 -3
- data/lib/railgun/mailer.rb +37 -41
- data/lib/railgun/railtie.rb +2 -0
- data/lib/railgun.rb +2 -0
- data/mailgun.gemspec +15 -11
- 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 +12 -18
- data/spec/integration/email_validation_spec.rb +35 -34
- data/spec/integration/events_spec.rb +8 -8
- data/spec/integration/list_members_spec.rb +27 -26
- data/spec/integration/list_spec.rb +22 -21
- data/spec/integration/logs_spec.rb +49 -47
- data/spec/integration/mailer_spec.rb +7 -3
- data/spec/integration/mailgun_spec.rb +85 -92
- data/spec/integration/metrics_spec.rb +137 -131
- 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 +222 -44
- data/spec/integration/templates_spec.rb +14 -12
- data/spec/integration/unsubscribes_spec.rb +8 -6
- data/spec/integration/webhook_spec.rb +18 -12
- data/spec/spec_helper.rb +15 -8
- data/spec/unit/client_spec.rb +424 -0
- data/spec/unit/connection/test_client.rb +108 -55
- data/spec/unit/events/events_spec.rb +48 -29
- data/spec/unit/exceptions/exceptions_spec.rb +8 -7
- data/spec/unit/helpers/api_version_checker_spec.rb +206 -0
- data/spec/unit/lists/opt_in_handler_spec.rb +11 -7
- data/spec/unit/mailgun_spec.rb +71 -68
- data/spec/unit/messages/batch_message_spec.rb +37 -36
- data/spec/unit/messages/message_builder_spec.rb +170 -169
- data/spec/unit/railgun/content_type_spec.rb +31 -30
- data/spec/unit/railgun/mailer_spec.rb +62 -59
- data/spec/unit/response_spec.rb +225 -0
- data/vcr_cassettes/For_the_suppressions_handling_class/creates_a_single_bounce.yml +55 -0
- data/vcr_cassettes/analytics_tags.yml +187 -0
- data/vcr_cassettes/suppressions.yml +1053 -170
- metadata +95 -29
- data/.rubocop_todo.yml +0 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e4ebcbb3e9a3ff18a06515ffca7027ffa893d0250ad4f6491cd896a78e035428
|
|
4
|
+
data.tar.gz: 27750827e497452d5ea7276601a01621164d9a4decfb3e6f43b1d32c51b7aa62
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7432da86b0a9b20ddaea5e0cb049c0fac6b7f8e37cc907d79bbcd5aa642550a8c8067bdd40e3c87f49679306d662906ede13c9bdbb4fd70ea19498f4325c9d92
|
|
7
|
+
data.tar.gz: 8691eac12d6cd590d8735a60965376ee7f751fe700c6533dc61cab987917292ef21d03e22d0c35c2c58584288602396b5201b8d5b55bdf9834693819a10185cc
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -11,17 +11,39 @@ permissions:
|
|
|
11
11
|
|
|
12
12
|
jobs:
|
|
13
13
|
test:
|
|
14
|
+
permissions: write-all
|
|
14
15
|
runs-on: ubuntu-latest
|
|
15
16
|
strategy:
|
|
16
17
|
matrix:
|
|
17
18
|
ruby-version: [ '3.0', '3.1', '3.2', '3.3', '3.4', '4.0' ]
|
|
18
19
|
|
|
19
20
|
steps:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
- uses: actions/checkout@v6
|
|
22
|
+
- name: Set up Ruby
|
|
23
|
+
uses: ruby/setup-ruby@v1
|
|
24
|
+
with:
|
|
25
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
26
|
+
bundler-cache: true
|
|
27
|
+
- name: Run tests
|
|
28
|
+
run: bundle exec rake spec:all
|
|
29
|
+
- uses: joshmfrankel/simplecov-check-action@main
|
|
30
|
+
with:
|
|
31
|
+
minimum_suite_coverage: 95
|
|
32
|
+
minimum_file_coverage: 80
|
|
33
|
+
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
34
|
+
if: matrix.ruby-version == '4.0'
|
|
35
|
+
|
|
36
|
+
rubocop:
|
|
37
|
+
permissions: write-all
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
|
|
40
|
+
steps:
|
|
41
|
+
- uses: actions/checkout@v6
|
|
42
|
+
- name: Set up Ruby
|
|
43
|
+
uses: ruby/setup-ruby@v1
|
|
44
|
+
with:
|
|
45
|
+
ruby-version: '4.0'
|
|
46
|
+
bundler-cache: true
|
|
47
|
+
- name: Run rubocop
|
|
48
|
+
run: |
|
|
49
|
+
bundle exec rubocop --format github
|
data/.rubocop.yml
CHANGED
|
@@ -1,8 +1,68 @@
|
|
|
1
|
-
|
|
1
|
+
AllCops:
|
|
2
|
+
NewCops: disable
|
|
3
|
+
SuggestExtensions: false
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
plugins:
|
|
6
|
+
- rubocop-rspec
|
|
7
|
+
- rubocop-rake
|
|
8
|
+
|
|
9
|
+
# temporary disable main codebase errors
|
|
10
|
+
Metrics/MethodLength:
|
|
11
|
+
Enabled: false
|
|
12
|
+
Metrics/BlockLength:
|
|
13
|
+
Enabled: false
|
|
14
|
+
Metrics/ClassLength:
|
|
15
|
+
Enabled: false
|
|
16
|
+
Metrics/CyclomaticComplexity:
|
|
17
|
+
Enabled: false
|
|
18
|
+
Metrics/PerceivedComplexity:
|
|
19
|
+
Enabled: false
|
|
20
|
+
Metrics/AbcSize:
|
|
21
|
+
Enabled: false
|
|
22
|
+
Metrics/ParameterLists:
|
|
23
|
+
Enabled: false
|
|
24
|
+
Lint/MissingSuper:
|
|
25
|
+
Enabled: false
|
|
26
|
+
Style/Documentation:
|
|
27
|
+
Enabled: false
|
|
28
|
+
Style/OptionalBooleanParameter:
|
|
29
|
+
Enabled: false
|
|
30
|
+
Naming/FileName:
|
|
31
|
+
Exclude:
|
|
32
|
+
- 'lib/mailgun-ruby.rb'
|
|
33
|
+
|
|
34
|
+
# temporary disable rspec errors
|
|
35
|
+
RSpec/MultipleExpectations:
|
|
36
|
+
Enabled: false
|
|
37
|
+
RSpec/ExampleLength:
|
|
38
|
+
Enabled: false
|
|
39
|
+
RSpec/InstanceVariable:
|
|
40
|
+
Enabled: false
|
|
41
|
+
RSpec/MultipleMemoizedHelpers:
|
|
42
|
+
Enabled: false
|
|
43
|
+
RSpec/StubbedMock:
|
|
44
|
+
Enabled: false
|
|
45
|
+
RSpec/AnyInstance:
|
|
46
|
+
Enabled: false
|
|
47
|
+
RSpec/MessageSpies:
|
|
48
|
+
Enabled: false
|
|
49
|
+
RSpec/DescribeClass:
|
|
50
|
+
Enabled: false
|
|
51
|
+
RSpec/VerifiedDoubles:
|
|
52
|
+
Enabled: false
|
|
53
|
+
RSpec/NoExpectationExample:
|
|
54
|
+
Enabled: false
|
|
55
|
+
RSpec/RepeatedDescription:
|
|
56
|
+
Enabled: false
|
|
57
|
+
RSpec/NestedGroups:
|
|
58
|
+
Enabled: false
|
|
59
|
+
RSpec/MultipleDescribes:
|
|
60
|
+
Enabled: false
|
|
61
|
+
RSpec/BeforeAfterAll:
|
|
4
62
|
Enabled: false
|
|
5
|
-
|
|
63
|
+
RSpec/SpecFilePathFormat:
|
|
6
64
|
Enabled: false
|
|
7
|
-
|
|
65
|
+
RSpec/ContextWording:
|
|
8
66
|
Enabled: false
|
|
67
|
+
RSpec/OverwritingSetup:
|
|
68
|
+
Enabled: false
|
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,19 +19,14 @@ 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
|
|
|
30
32
|
task default: 'spec:unit'
|
|
31
|
-
task spec: 'spec:unit'
|
|
32
|
-
|
|
33
|
-
task :console do
|
|
34
|
-
sh 'pry --gem'
|
|
35
|
-
end
|
|
@@ -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/lib/mailgun/address.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Mailgun
|
|
3
4
|
# Mailgun::Address is a simple interface to the Email Validation API.
|
|
4
5
|
class Address
|
|
5
6
|
def initialize(api_key = Mailgun.api_key, api_host = Mailgun.api_host)
|
|
@@ -10,12 +11,11 @@ module Mailgun
|
|
|
10
11
|
#
|
|
11
12
|
# @param [String] address Email address to validate (max 512 chars.)
|
|
12
13
|
def validate(address, mailbox_verification = false)
|
|
13
|
-
params = {address:
|
|
14
|
+
params = { address: address }
|
|
14
15
|
params[:mailbox_verification] = true if mailbox_verification
|
|
15
16
|
|
|
16
|
-
res = @client.get
|
|
17
|
-
|
|
17
|
+
res = @client.get 'address/validate', params
|
|
18
|
+
res.to_h!
|
|
18
19
|
end
|
|
19
20
|
end
|
|
20
|
-
|
|
21
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,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Mailgun
|
|
2
4
|
# A Mailgun::Client object is used to communicate with the Mailgun API. It is a
|
|
3
5
|
# wrapper around Faraday so you don't have to worry about the HTTP aspect
|
|
@@ -5,28 +7,27 @@ module Mailgun
|
|
|
5
7
|
#
|
|
6
8
|
# See the Github documentation for full examples.
|
|
7
9
|
class Client
|
|
8
|
-
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'
|
|
10
|
+
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'
|
|
9
11
|
|
|
10
12
|
def initialize(api_key = Mailgun.api_key,
|
|
11
13
|
api_host = Mailgun.api_host || 'api.mailgun.net',
|
|
12
|
-
api_version = Mailgun.api_version
|
|
14
|
+
api_version = Mailgun.api_version || 'v3',
|
|
13
15
|
ssl = true,
|
|
14
|
-
test_mode =
|
|
16
|
+
test_mode = !Mailgun.test_mode.nil?,
|
|
15
17
|
timeout = nil,
|
|
16
18
|
proxy_url = Mailgun.proxy_url)
|
|
17
|
-
|
|
18
19
|
endpoint = endpoint_generator(api_host, api_version, ssl)
|
|
19
20
|
|
|
20
21
|
request_options = {
|
|
21
22
|
url: endpoint,
|
|
22
23
|
proxy: proxy_url,
|
|
23
|
-
ssl: {verify: ssl},
|
|
24
|
+
ssl: { verify: ssl },
|
|
24
25
|
headers: {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
|
|
27
|
+
'Accept' => '*/*'
|
|
28
|
+
}
|
|
28
29
|
}
|
|
29
|
-
request_options.merge!(request: {timeout: timeout}) if timeout
|
|
30
|
+
request_options.merge!(request: { timeout: timeout }) if timeout
|
|
30
31
|
|
|
31
32
|
@http_client = build_http_client(api_key, request_options)
|
|
32
33
|
|
|
@@ -49,14 +50,18 @@ module Mailgun
|
|
|
49
50
|
end
|
|
50
51
|
|
|
51
52
|
# Change API key
|
|
53
|
+
# rubocop:disable Naming/AccessorMethodName
|
|
52
54
|
def set_api_key(api_key)
|
|
53
55
|
@http_client.set_basic_auth('api', api_key)
|
|
54
56
|
end
|
|
57
|
+
# rubocop:enable Naming/AccessorMethodName
|
|
55
58
|
|
|
56
59
|
# Add subaccount id to headers
|
|
60
|
+
# rubocop:disable Naming/AccessorMethodName
|
|
57
61
|
def set_subaccount(subaccount_id)
|
|
58
|
-
@http_client.headers
|
|
62
|
+
@http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
|
|
59
63
|
end
|
|
64
|
+
# rubocop:enable Naming/AccessorMethodName
|
|
60
65
|
|
|
61
66
|
# Reset subaccount for primary usage
|
|
62
67
|
def reset_subaccount
|
|
@@ -71,15 +76,13 @@ module Mailgun
|
|
|
71
76
|
end
|
|
72
77
|
|
|
73
78
|
# @return [String] client api version
|
|
74
|
-
|
|
75
|
-
@api_version
|
|
76
|
-
end
|
|
79
|
+
attr_reader :api_version
|
|
77
80
|
|
|
78
81
|
# Provides a store of all the emails sent in test mode so you can check them.
|
|
79
82
|
#
|
|
80
83
|
# @return [Hash]
|
|
81
84
|
def self.deliveries
|
|
82
|
-
|
|
85
|
+
@deliveries ||= []
|
|
83
86
|
end
|
|
84
87
|
|
|
85
88
|
# Simple Message Sending
|
|
@@ -91,12 +94,12 @@ module Mailgun
|
|
|
91
94
|
def send_message(working_domain, data)
|
|
92
95
|
perform_data_validation(working_domain, data)
|
|
93
96
|
|
|
94
|
-
if test_mode?
|
|
97
|
+
if test_mode?
|
|
95
98
|
Mailgun::Client.deliveries << data.dup
|
|
96
99
|
return Response.from_hash(
|
|
97
100
|
{
|
|
98
|
-
:
|
|
99
|
-
:
|
|
101
|
+
body: "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
|
|
102
|
+
status: 200
|
|
100
103
|
}
|
|
101
104
|
)
|
|
102
105
|
end
|
|
@@ -106,7 +109,7 @@ module Mailgun
|
|
|
106
109
|
# Remove nil values from the data hash
|
|
107
110
|
# Submitting nils to the API will likely cause an error.
|
|
108
111
|
# See also: https://github.com/mailgun/mailgun-ruby/issues/32
|
|
109
|
-
data = data.
|
|
112
|
+
data = data.reject { |_k, v| v.nil? }
|
|
110
113
|
|
|
111
114
|
if data.key?(:message)
|
|
112
115
|
if data[:message].is_a?(String)
|
|
@@ -119,7 +122,7 @@ module Mailgun
|
|
|
119
122
|
when MessageBuilder
|
|
120
123
|
post("#{working_domain}/messages", data.message)
|
|
121
124
|
else
|
|
122
|
-
|
|
125
|
+
raise ParameterError.new('Unknown data type for data parameter.', data)
|
|
123
126
|
end
|
|
124
127
|
end
|
|
125
128
|
|
|
@@ -134,8 +137,8 @@ module Mailgun
|
|
|
134
137
|
def post(resource_path, data, headers = {})
|
|
135
138
|
response = @http_client.post(resource_path, data, headers)
|
|
136
139
|
Response.new(response)
|
|
137
|
-
rescue =>
|
|
138
|
-
raise communication_error
|
|
140
|
+
rescue StandardError => e
|
|
141
|
+
raise communication_error e
|
|
139
142
|
end
|
|
140
143
|
|
|
141
144
|
# Generic Mailgun GET Handler
|
|
@@ -151,8 +154,8 @@ module Mailgun
|
|
|
151
154
|
response = @http_client.get(resource_path, params, headers)
|
|
152
155
|
|
|
153
156
|
Response.new(response)
|
|
154
|
-
rescue =>
|
|
155
|
-
raise communication_error(
|
|
157
|
+
rescue StandardError => e
|
|
158
|
+
raise communication_error(e)
|
|
156
159
|
end
|
|
157
160
|
|
|
158
161
|
# Generic Mailgun PUT Handler
|
|
@@ -162,19 +165,11 @@ module Mailgun
|
|
|
162
165
|
# @param [Hash] data This should be a standard Hash
|
|
163
166
|
# containing required parameters for the requested resource.
|
|
164
167
|
# @return [Mailgun::Response] A Mailgun::Response object.
|
|
165
|
-
def put(resource_path,
|
|
166
|
-
response =
|
|
167
|
-
if body_params
|
|
168
|
-
@http_client.put(resource_path) do |request|
|
|
169
|
-
request['Content-Type'] = 'application/json'
|
|
170
|
-
request.params = params.to_json
|
|
171
|
-
end
|
|
172
|
-
else
|
|
173
|
-
@http_client.put(resource_path, params)
|
|
174
|
-
end
|
|
168
|
+
def put(resource_path, data, headers = {})
|
|
169
|
+
response = @http_client.put(resource_path, data, headers)
|
|
175
170
|
Response.new(response)
|
|
176
|
-
rescue =>
|
|
177
|
-
raise communication_error
|
|
171
|
+
rescue StandardError => e
|
|
172
|
+
raise communication_error e
|
|
178
173
|
end
|
|
179
174
|
|
|
180
175
|
# Generic Mailgun DELETE Handler
|
|
@@ -196,8 +191,8 @@ module Mailgun
|
|
|
196
191
|
@http_client.delete(resource_path)
|
|
197
192
|
end
|
|
198
193
|
Response.new(response)
|
|
199
|
-
rescue =>
|
|
200
|
-
raise communication_error
|
|
194
|
+
rescue StandardError => e
|
|
195
|
+
raise communication_error e
|
|
201
196
|
end
|
|
202
197
|
|
|
203
198
|
# Constructs a Suppressions client for the given domain.
|
|
@@ -229,7 +224,7 @@ module Mailgun
|
|
|
229
224
|
# @param [Boolean] ssl True, SSL. False, No SSL.
|
|
230
225
|
# @return [string] concatenated URL string
|
|
231
226
|
def endpoint_generator(api_host, api_version, ssl)
|
|
232
|
-
ssl ?
|
|
227
|
+
scheme = ssl ? 'https' : 'http'
|
|
233
228
|
if api_version
|
|
234
229
|
"#{scheme}://#{api_host}/#{api_version}"
|
|
235
230
|
else
|
|
@@ -240,31 +235,36 @@ module Mailgun
|
|
|
240
235
|
# Raises CommunicationError and stores response in it if present
|
|
241
236
|
#
|
|
242
237
|
# @param [StandardException] e upstream exception object
|
|
243
|
-
def communication_error(
|
|
244
|
-
if
|
|
245
|
-
return case
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
238
|
+
def communication_error(error)
|
|
239
|
+
if error.respond_to?(:response) && error.response
|
|
240
|
+
return case error.response_status
|
|
241
|
+
when Unauthorized::CODE
|
|
242
|
+
Unauthorized.new(error.message, error.response)
|
|
243
|
+
when BadRequest::CODE
|
|
244
|
+
BadRequest.new(error.message, error.response)
|
|
245
|
+
else
|
|
246
|
+
CommunicationError.new(error.message, error.response)
|
|
247
|
+
end
|
|
253
248
|
end
|
|
254
|
-
CommunicationError.new(
|
|
249
|
+
CommunicationError.new(error.message)
|
|
255
250
|
end
|
|
256
251
|
|
|
257
252
|
def perform_data_validation(working_domain, data)
|
|
258
253
|
message = data.respond_to?(:message) ? data.message : data
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
254
|
+
raise ParameterError.new('Missing working domain', working_domain) unless working_domain
|
|
255
|
+
|
|
256
|
+
if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
|
|
257
|
+
raise ParameterError.new(
|
|
258
|
+
'Missing `to` recipient, message should contain at least 1 recipient',
|
|
259
|
+
working_domain
|
|
260
|
+
)
|
|
261
|
+
end
|
|
262
|
+
return unless message.fetch('from', []).empty? && message.fetch(:from, []).empty?
|
|
263
|
+
|
|
264
|
+
raise ParameterError.new(
|
|
265
265
|
'Missing a `from` sender, message should contain at least 1 `from` sender',
|
|
266
266
|
working_domain
|
|
267
|
-
)
|
|
267
|
+
)
|
|
268
268
|
end
|
|
269
269
|
|
|
270
270
|
def build_http_client(api_key, request_options)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Mailgun
|
|
3
4
|
# A Mailgun::Domains object is a simple CRUD interface to Mailgun Domains.
|
|
4
5
|
# Uses Mailgun
|
|
5
6
|
class Domains
|
|
@@ -67,7 +68,6 @@ module Mailgun
|
|
|
67
68
|
@client.put("domains/#{domain}", options).to_h
|
|
68
69
|
end
|
|
69
70
|
|
|
70
|
-
|
|
71
71
|
# Public: Verify domain
|
|
72
72
|
#
|
|
73
73
|
# domain - [String] Domain name
|
|
@@ -101,7 +101,7 @@ module Mailgun
|
|
|
101
101
|
#
|
|
102
102
|
# Returns [Hash] with message key
|
|
103
103
|
def list_domain_keys(options = {})
|
|
104
|
-
@client.get(
|
|
104
|
+
@client.get('dkim/keys', options).to_h
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
# Public: Create a domain key
|
|
@@ -114,7 +114,7 @@ module Mailgun
|
|
|
114
114
|
#
|
|
115
115
|
# Returns [Hash] with message key
|
|
116
116
|
def create_domain_key(options = {})
|
|
117
|
-
@client.post(
|
|
117
|
+
@client.post('dkim/keys', options).to_h
|
|
118
118
|
end
|
|
119
119
|
|
|
120
120
|
# Public: Delete a domain key.
|
|
@@ -125,7 +125,7 @@ module Mailgun
|
|
|
125
125
|
#
|
|
126
126
|
# Returns [Hash] with message key
|
|
127
127
|
def delete_domain_key(options = {})
|
|
128
|
-
@client.delete(
|
|
128
|
+
@client.delete('dkim/keys', options).to_h
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
# Public: Activate a domain key for a specified authority and selector.
|
|
@@ -197,7 +197,8 @@ module Mailgun
|
|
|
197
197
|
#
|
|
198
198
|
# domain - [String] Name of the domain (ex. domain.com)
|
|
199
199
|
# options - [Hash] of
|
|
200
|
-
# active - [Boolean] yes or no. If set to yes, links will be overwritten and pointed to our servers
|
|
200
|
+
# active - [Boolean] yes or no. If set to yes, links will be overwritten and pointed to our servers
|
|
201
|
+
# so we can track clicks.
|
|
201
202
|
#
|
|
202
203
|
# Returns [Hash] Information on the tracking click settings
|
|
203
204
|
def update_domain_tracking_click_settings(domain, options = {})
|
|
@@ -209,7 +210,8 @@ module Mailgun
|
|
|
209
210
|
# domain - [String] Name of the domain (ex. domain.com)
|
|
210
211
|
# options - [Hash] of
|
|
211
212
|
# active - [Boolean] yes or no. If set to yes, a tracking pixel will be inserted below your HTML content.
|
|
212
|
-
# place_at_the_top - [Boolean] yes or no. If set to yes, tracking pixel will be moved
|
|
213
|
+
# place_at_the_top - [Boolean] yes or no. If set to yes, tracking pixel will be moved
|
|
214
|
+
# to top of your HTML content.
|
|
213
215
|
#
|
|
214
216
|
# Returns [Hash] Information on the tracking open settings
|
|
215
217
|
def update_domain_tracking_open_settings(domain, options = {})
|
|
@@ -258,7 +260,6 @@ module Mailgun
|
|
|
258
260
|
|
|
259
261
|
# ==== End of Domain::Tracking methods ====
|
|
260
262
|
|
|
261
|
-
|
|
262
263
|
# ==== Domain::DKIM_Security methods ====
|
|
263
264
|
|
|
264
265
|
# Public: Tracking Certificate: Generate
|
|
@@ -338,13 +339,13 @@ module Mailgun
|
|
|
338
339
|
#
|
|
339
340
|
# Returns [Array] A list of domains (hash)
|
|
340
341
|
def get_domain_stats(domain, options = {})
|
|
341
|
-
|
|
342
|
+
raise(ParameterError, 'No domain given to list stats on Mailgun', caller) unless domain
|
|
343
|
+
|
|
342
344
|
@client.get("#{domain}/stats/total", options).to_h
|
|
343
345
|
end
|
|
344
346
|
|
|
345
347
|
# ==== End of Reporting::Stats methods ====
|
|
346
348
|
|
|
347
|
-
|
|
348
349
|
enforces_api_version 'v1', :list_domain_keys, :create_domain_key, :delete_domain_key, :dkim_rotation,
|
|
349
350
|
:dkim_rotate
|
|
350
351
|
enforces_api_version 'v2', :get_domain_tracking_certificate, :regenerate_domain_tracking_certificate,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Mailgun
|
|
3
4
|
# A Mailgun::Events object makes it really simple to consume
|
|
4
5
|
# Mailgun's events from the Events endpoint.
|
|
5
6
|
#
|
|
@@ -100,7 +101,7 @@ module Mailgun
|
|
|
100
101
|
# Returns a String of the partial URI if the given url follows the regular API format
|
|
101
102
|
# Returns nil in other cases (e.g. when given nil, or an irrelevant url)
|
|
102
103
|
def extract_endpoint_from(url = nil)
|
|
103
|
-
URI.parse(url).path[
|
|
104
|
+
URI.parse(url).path[%r{/v\d/#{@domain}/events/(.+)}, 1]
|
|
104
105
|
rescue URI::InvalidURIError
|
|
105
106
|
nil
|
|
106
107
|
end
|
|
@@ -112,8 +113,8 @@ module Mailgun
|
|
|
112
113
|
# Returns a String of the partial URI
|
|
113
114
|
def construct_url(paging = nil)
|
|
114
115
|
return "#{@domain}/events/#{paging}" if paging
|
|
116
|
+
|
|
115
117
|
"#{@domain}/events"
|
|
116
118
|
end
|
|
117
|
-
|
|
118
119
|
end
|
|
119
120
|
end
|