mailgun-ruby 1.4.1 → 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/Gemfile +3 -1
- data/README.md +1 -1
- data/Rakefile +5 -3
- 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 +47 -51
- data/lib/mailgun/domains/domains.rb +7 -8
- 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 +4 -6
- data/lib/mailgun/logs/logs.rb +4 -2
- data/lib/mailgun/messages/batch_message.rb +8 -9
- data/lib/mailgun/messages/message_builder.rb +36 -24
- data/lib/mailgun/metrics/metrics.rb +6 -4
- data/lib/mailgun/response.rb +11 -9
- data/lib/mailgun/subaccounts/subaccounts.rb +13 -8
- data/lib/mailgun/suppressions.rb +36 -43
- data/lib/mailgun/tags/analytics_tags.rb +33 -2
- data/lib/mailgun/tags/tags.rb +25 -17
- 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 +9 -14
- data/lib/railgun/errors.rb +2 -3
- data/lib/railgun/mailer.rb +35 -39
- data/lib/railgun/railtie.rb +2 -0
- data/lib/railgun.rb +2 -0
- data/mailgun.gemspec +12 -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 +6 -6
- 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 -8
- 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
- metadata +49 -33
- data/.rubocop.yml +0 -8
- data/.rubocop_todo.yml +0 -22
data/lib/mailgun/tags/tags.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Mailgun
|
|
3
4
|
# A Mailgun::Tags object is a simple CRUD interface to Mailgun Tags.
|
|
4
5
|
# Uses Mailgun
|
|
5
6
|
class Tags
|
|
6
|
-
|
|
7
7
|
# Public: creates a new Mailgun::Tags instance.
|
|
8
8
|
# Defaults to Mailgun::Client
|
|
9
9
|
def initialize(client = Mailgun::Client.new)
|
|
@@ -22,7 +22,8 @@ module Mailgun
|
|
|
22
22
|
# Returns [Array] A list of tags (hash)
|
|
23
23
|
def get_tags(domain, options = {})
|
|
24
24
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
25
|
-
|
|
25
|
+
raise(ParameterError, 'No domain given to store template on', caller) unless domain
|
|
26
|
+
|
|
26
27
|
@client.get("#{domain}/tags", options).to_h['items']
|
|
27
28
|
end
|
|
28
29
|
|
|
@@ -34,8 +35,9 @@ module Mailgun
|
|
|
34
35
|
# Returns [Hash] Information on the requested tag.
|
|
35
36
|
def get_tag(domain, tag)
|
|
36
37
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
39
|
+
raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
|
|
40
|
+
|
|
39
41
|
@client.get("#{domain}/tags/#{tag}").to_h!
|
|
40
42
|
end
|
|
41
43
|
|
|
@@ -49,8 +51,9 @@ module Mailgun
|
|
|
49
51
|
# Returns [Boolean] if successful or not
|
|
50
52
|
def update(domain, tag, options = {})
|
|
51
53
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
55
|
+
raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
|
|
56
|
+
|
|
54
57
|
@client.put("#{domain}/tags/#{tag}", options).to_h['message'] == 'Tag updated'
|
|
55
58
|
end
|
|
56
59
|
|
|
@@ -68,8 +71,9 @@ module Mailgun
|
|
|
68
71
|
# Returns [Hash] of tag stats info
|
|
69
72
|
def get_tag_stats(domain, tag, options = {})
|
|
70
73
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
75
|
+
raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
|
|
76
|
+
|
|
73
77
|
@client.get("#{domain}/tags/#{tag}/stats", options).to_h
|
|
74
78
|
end
|
|
75
79
|
|
|
@@ -82,8 +86,9 @@ module Mailgun
|
|
|
82
86
|
# Returns [Boolean] if successful or not
|
|
83
87
|
def remove(domain, tag)
|
|
84
88
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
85
|
-
|
|
86
|
-
|
|
89
|
+
raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
|
|
90
|
+
raise(ParameterError, 'No template name given to find on provided domain', caller) unless tag
|
|
91
|
+
|
|
87
92
|
@client.delete("#{domain}/tags/#{tag}").to_h['message'] == 'Tag deleted'
|
|
88
93
|
end
|
|
89
94
|
|
|
@@ -95,8 +100,9 @@ module Mailgun
|
|
|
95
100
|
# Returns [Hash] of countries of origin for a given domain
|
|
96
101
|
def get_countries_aggregated_stats(domain, tag)
|
|
97
102
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
98
|
-
|
|
99
|
-
|
|
103
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
104
|
+
raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
|
|
105
|
+
|
|
100
106
|
@client.get("#{domain}/tags/#{tag}/stats/aggregates/countries").to_h
|
|
101
107
|
end
|
|
102
108
|
|
|
@@ -108,8 +114,9 @@ module Mailgun
|
|
|
108
114
|
# Returns [Hash] of email providers for a given domain
|
|
109
115
|
def get_providers_aggregated_stats(domain, tag)
|
|
110
116
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
111
|
-
|
|
112
|
-
|
|
117
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
118
|
+
raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
|
|
119
|
+
|
|
113
120
|
@client.get("#{domain}/tags/#{tag}/stats/aggregates/providers").to_h
|
|
114
121
|
end
|
|
115
122
|
|
|
@@ -121,8 +128,9 @@ module Mailgun
|
|
|
121
128
|
# Returns [Hash] of devices for a given domain
|
|
122
129
|
def get_devices_aggregated_stats(domain, tag)
|
|
123
130
|
warn('This API is deprecated in favor of our new analytics Tags API')
|
|
124
|
-
|
|
125
|
-
|
|
131
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
132
|
+
raise(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
|
|
133
|
+
|
|
126
134
|
@client.get("#{domain}/tags/#{tag}/stats/aggregates/devices").to_h
|
|
127
135
|
end
|
|
128
136
|
end
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Mailgun
|
|
3
4
|
# A Mailgun::Templates object is a simple CRUD interface to Mailgun Templates.
|
|
4
5
|
# Uses Mailgun
|
|
5
6
|
class Templates
|
|
6
|
-
|
|
7
7
|
# Public: creates a new Mailgun::Templates instance.
|
|
8
8
|
# Defaults to Mailgun::Client
|
|
9
9
|
def initialize(client = Mailgun::Client.new)
|
|
@@ -24,7 +24,8 @@ module Mailgun
|
|
|
24
24
|
#
|
|
25
25
|
# Returns [Hash] of created template
|
|
26
26
|
def create(domain, options = {})
|
|
27
|
-
|
|
27
|
+
raise(ParameterError, 'No domain given to store template on', caller) unless domain
|
|
28
|
+
|
|
28
29
|
@client.post("#{domain}/templates", options).to_h
|
|
29
30
|
end
|
|
30
31
|
|
|
@@ -38,8 +39,9 @@ module Mailgun
|
|
|
38
39
|
#
|
|
39
40
|
# Returns [Hash] Information on the requested template.
|
|
40
41
|
def info(domain, template_name, options = {})
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
43
|
+
raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
|
|
44
|
+
|
|
43
45
|
@client.get("#{domain}/templates/#{template_name}", options).to_h!
|
|
44
46
|
end
|
|
45
47
|
|
|
@@ -52,8 +54,9 @@ module Mailgun
|
|
|
52
54
|
#
|
|
53
55
|
# Returns [Hash] of updated domain
|
|
54
56
|
def update(domain, template_name, options = {})
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
raise(ParameterError, 'No domain given to add on Mailgun', caller) unless domain
|
|
58
|
+
raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
|
|
59
|
+
|
|
57
60
|
@client.put("#{domain}/templates/#{template_name}", options).to_h
|
|
58
61
|
end
|
|
59
62
|
|
|
@@ -65,12 +68,13 @@ module Mailgun
|
|
|
65
68
|
#
|
|
66
69
|
# Returns [Boolean] if successful or not
|
|
67
70
|
def remove(domain, template_name)
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
|
|
72
|
+
raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
|
|
73
|
+
|
|
70
74
|
@client.delete("#{domain}/templates/#{template_name}").to_h['message'] == 'template has been deleted'
|
|
71
75
|
end
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
alias delete remove
|
|
77
|
+
alias delete_template remove
|
|
74
78
|
|
|
75
79
|
# Public: Get Templates
|
|
76
80
|
#
|
|
@@ -81,10 +85,11 @@ module Mailgun
|
|
|
81
85
|
#
|
|
82
86
|
# Returns [Array] A list of templates (hash)
|
|
83
87
|
def list(domain, options = {})
|
|
84
|
-
|
|
88
|
+
raise(ParameterError, 'No domain given.', caller) unless domain
|
|
89
|
+
|
|
85
90
|
@client.get("#{domain}/templates", options).to_h['items']
|
|
86
91
|
end
|
|
87
|
-
|
|
92
|
+
alias get_templates list
|
|
88
93
|
|
|
89
94
|
# Public: Delete Templates
|
|
90
95
|
# NOTE: This method deletes all stored templates for the domain.
|
|
@@ -93,10 +98,11 @@ module Mailgun
|
|
|
93
98
|
#
|
|
94
99
|
# Returns [Boolean] if successful or not
|
|
95
100
|
def remove_all(domain)
|
|
96
|
-
|
|
101
|
+
raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
|
|
102
|
+
|
|
97
103
|
@client.delete("#{domain}/templates").to_h['message'] == 'templates have been deleted'
|
|
98
104
|
end
|
|
99
|
-
|
|
105
|
+
alias delete_templates remove_all
|
|
100
106
|
|
|
101
107
|
# Public: Create a new version of a template
|
|
102
108
|
#
|
|
@@ -112,8 +118,9 @@ module Mailgun
|
|
|
112
118
|
#
|
|
113
119
|
# Returns [Hash] of updated template
|
|
114
120
|
def create_version(domain, template_name, options = {})
|
|
115
|
-
|
|
116
|
-
|
|
121
|
+
raise(ParameterError, 'No domain given.', caller) unless domain
|
|
122
|
+
raise(ParameterError, 'No template name given.', caller) unless template_name
|
|
123
|
+
|
|
117
124
|
@client.post("#{domain}/templates/#{template_name}/versions", options).to_h
|
|
118
125
|
end
|
|
119
126
|
|
|
@@ -125,9 +132,10 @@ module Mailgun
|
|
|
125
132
|
#
|
|
126
133
|
# Returns [Hash] Information on the requested template + version.
|
|
127
134
|
def info_version(domain, template_name, tag)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
135
|
+
raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
|
|
136
|
+
raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
|
|
137
|
+
raise(ParameterError, 'No version tag given.', caller) unless tag
|
|
138
|
+
|
|
131
139
|
@client.get("#{domain}/templates/#{template_name}/versions/#{tag}").to_h!
|
|
132
140
|
end
|
|
133
141
|
|
|
@@ -145,9 +153,10 @@ module Mailgun
|
|
|
145
153
|
#
|
|
146
154
|
# Returns [Hash] of updated template's version
|
|
147
155
|
def update_version(domain, template_name, tag, options = {})
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
156
|
+
raise(ParameterError, 'No domain given.', caller) unless domain
|
|
157
|
+
raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
|
|
158
|
+
raise(ParameterError, 'No version tag given.', caller) unless tag
|
|
159
|
+
|
|
151
160
|
@client.put("#{domain}/templates/#{template_name}/versions/#{tag}", options).to_h
|
|
152
161
|
end
|
|
153
162
|
|
|
@@ -159,11 +168,12 @@ module Mailgun
|
|
|
159
168
|
#
|
|
160
169
|
# Returns [Boolean] if successful or not
|
|
161
170
|
def delete_version(domain, template_name, tag)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
171
|
+
raise(ParameterError, 'No domain given.', caller) unless domain
|
|
172
|
+
raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
|
|
173
|
+
raise(ParameterError, 'No version tag given.', caller) unless tag
|
|
174
|
+
|
|
165
175
|
@client.delete("#{domain}/templates/#{template_name}/versions/#{tag}")
|
|
166
|
-
|
|
176
|
+
.to_h['message'] == 'version has been deleted'
|
|
167
177
|
end
|
|
168
178
|
|
|
169
179
|
# Public: Get Template's Versions list
|
|
@@ -177,8 +187,9 @@ module Mailgun
|
|
|
177
187
|
#
|
|
178
188
|
# Returns [Array] A list of template's versions (hash)
|
|
179
189
|
def template_versions_list(domain, template_name, options = {})
|
|
180
|
-
|
|
181
|
-
|
|
190
|
+
raise(ParameterError, 'No domain given.', caller) unless domain
|
|
191
|
+
raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
|
|
192
|
+
|
|
182
193
|
@client.get("#{domain}/templates/#{template_name}/versions", options).to_h
|
|
183
194
|
end
|
|
184
195
|
end
|
data/lib/mailgun/version.rb
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Mailgun
|
|
3
4
|
# A Mailgun::Webhooks object is a simple CRUD interface to Mailgun Webhooks.
|
|
4
5
|
# Uses Mailgun
|
|
5
6
|
class Webhooks
|
|
6
|
-
ACTIONS = %w
|
|
7
|
+
ACTIONS = %w[accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed].freeze
|
|
7
8
|
|
|
8
9
|
# Public creates a new Mailgun::Webhooks instance.
|
|
9
10
|
# Defaults to Mailgun::Client
|
|
@@ -21,7 +22,7 @@ module Mailgun
|
|
|
21
22
|
res = @client.get("domains/#{domain}/webhooks", options)
|
|
22
23
|
res.to_h['webhooks']
|
|
23
24
|
end
|
|
24
|
-
|
|
25
|
+
alias get_webhooks list
|
|
25
26
|
|
|
26
27
|
# Public: Get webook information for a specific action
|
|
27
28
|
#
|
|
@@ -36,7 +37,7 @@ module Mailgun
|
|
|
36
37
|
rescue NoMethodError
|
|
37
38
|
''
|
|
38
39
|
end
|
|
39
|
-
|
|
40
|
+
alias get_webhook_url info
|
|
40
41
|
|
|
41
42
|
# Public: Add webhook
|
|
42
43
|
#
|
|
@@ -49,8 +50,8 @@ module Mailgun
|
|
|
49
50
|
res = @client.post("domains/#{domain}/webhooks", id: action, url: url)
|
|
50
51
|
res.to_h['webhook']['urls'].include?(url) && res.to_h['message'] == 'Webhook has been created'
|
|
51
52
|
end
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
alias add create
|
|
54
|
+
alias add_webhook create
|
|
54
55
|
|
|
55
56
|
# Public: Sets all webhooks to the same URL
|
|
56
57
|
#
|
|
@@ -63,10 +64,10 @@ module Mailgun
|
|
|
63
64
|
add_webhook domain, action, url
|
|
64
65
|
end
|
|
65
66
|
true
|
|
66
|
-
rescue
|
|
67
|
+
rescue StandardError
|
|
67
68
|
false
|
|
68
69
|
end
|
|
69
|
-
|
|
70
|
+
alias add_all_webhooks create_all
|
|
70
71
|
|
|
71
72
|
# Public: Update webhook
|
|
72
73
|
#
|
|
@@ -76,12 +77,13 @@ module Mailgun
|
|
|
76
77
|
#
|
|
77
78
|
# Returns a Boolean of whether the webhook was updated
|
|
78
79
|
def update(domain, action, url = '')
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
raise Mailgun::ParameterError('Domain not provided to update webhooks') unless domain
|
|
81
|
+
raise Mailgun::ParameterError('Action not provided to identify webhook to update') unless action
|
|
82
|
+
|
|
81
83
|
res = @client.put("domains/#{domain}/webhooks/#{action}", id: action, url: url)
|
|
82
84
|
res.to_h['webhook']['urls'] == url && res.to_h['message'] == 'Webhook has been updated'
|
|
83
85
|
end
|
|
84
|
-
|
|
86
|
+
alias update_webhook update
|
|
85
87
|
|
|
86
88
|
# Public: Delete a specific webhook
|
|
87
89
|
#
|
|
@@ -90,14 +92,15 @@ module Mailgun
|
|
|
90
92
|
#
|
|
91
93
|
# Returns a Boolean of the success
|
|
92
94
|
def remove(domain, action)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
+
raise Mailgun::ParameterError('Domain not provided to remove webhook from') unless domain
|
|
96
|
+
raise Mailgun::ParameterError('Action not provided to identify webhook to remove') unless action
|
|
97
|
+
|
|
95
98
|
@client.delete("domains/#{domain}/webhooks/#{action}").to_h['message'] == 'Webhook has been deleted'
|
|
96
99
|
rescue Mailgun::CommunicationError
|
|
97
100
|
false
|
|
98
101
|
end
|
|
99
|
-
|
|
100
|
-
|
|
102
|
+
alias delete remove
|
|
103
|
+
alias delete_webhook remove
|
|
101
104
|
|
|
102
105
|
# Public: Delete all webhooks for a domain
|
|
103
106
|
#
|
|
@@ -105,13 +108,13 @@ module Mailgun
|
|
|
105
108
|
#
|
|
106
109
|
# Returns a Boolean on the success
|
|
107
110
|
def remove_all(domain)
|
|
108
|
-
|
|
111
|
+
raise Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
|
|
112
|
+
|
|
109
113
|
ACTIONS.each do |action|
|
|
110
114
|
delete_webhook domain, action
|
|
111
115
|
end
|
|
112
116
|
end
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
117
|
+
alias delete_all remove_all
|
|
118
|
+
alias delete_all_webooks remove_all
|
|
116
119
|
end
|
|
117
120
|
end
|
data/lib/mailgun-ruby.rb
CHANGED
data/lib/mailgun.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# require ruby dependencies
|
|
2
4
|
require 'json'
|
|
3
5
|
require 'openssl'
|
|
@@ -16,7 +18,7 @@ require 'mailgun/exceptions/exceptions'
|
|
|
16
18
|
require 'mailgun/helpers/api_version_checker'
|
|
17
19
|
|
|
18
20
|
# load zeitwerk
|
|
19
|
-
Zeitwerk::Loader.for_gem.tap do |loader|
|
|
21
|
+
Zeitwerk::Loader.for_gem.tap do |loader|
|
|
20
22
|
loader.ignore("#{__dir__}/mailgun-ruby.rb")
|
|
21
23
|
loader.ignore("#{__dir__}/railgun.rb")
|
|
22
24
|
loader.ignore("#{__dir__}/railgun")
|
|
@@ -41,7 +43,6 @@ end
|
|
|
41
43
|
#
|
|
42
44
|
# See the Github documentation for full examples.
|
|
43
45
|
module Mailgun
|
|
44
|
-
|
|
45
46
|
class << self
|
|
46
47
|
attr_accessor :api_host,
|
|
47
48
|
:api_key,
|
|
@@ -56,7 +57,6 @@ module Mailgun
|
|
|
56
57
|
yield self
|
|
57
58
|
true
|
|
58
59
|
end
|
|
59
|
-
|
|
60
|
+
alias config configure
|
|
60
61
|
end
|
|
61
|
-
|
|
62
62
|
end
|
data/lib/railgun/attachment.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Railgun
|
|
3
4
|
class Attachment < StringIO
|
|
4
|
-
|
|
5
5
|
attr_reader :filename, :content_type, :path,
|
|
6
6
|
:original_filename, :overwritten_filename
|
|
7
7
|
|
|
@@ -9,17 +9,15 @@ module Railgun
|
|
|
9
9
|
@path = ''
|
|
10
10
|
@inline = args.detect { |opt| opt[:inline] }
|
|
11
11
|
|
|
12
|
-
if @inline
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
@filename = if @inline
|
|
13
|
+
attachment.cid
|
|
14
|
+
else
|
|
15
|
+
attachment.filename
|
|
16
|
+
end
|
|
17
17
|
|
|
18
18
|
@original_filename = @filename
|
|
19
19
|
|
|
20
|
-
if args.detect { |opt| opt[:filename] }
|
|
21
|
-
@filename = opt[:filename]
|
|
22
|
-
end
|
|
20
|
+
@filename = opt[:filename] if args.detect { |opt| opt[:filename] }
|
|
23
21
|
|
|
24
22
|
@overwritten_filename = @filename
|
|
25
23
|
|
|
@@ -41,9 +39,7 @@ module Railgun
|
|
|
41
39
|
end
|
|
42
40
|
|
|
43
41
|
def attach_to_message!(mb)
|
|
44
|
-
if mb.nil?
|
|
45
|
-
nil
|
|
46
|
-
end
|
|
42
|
+
nil if mb.nil?
|
|
47
43
|
|
|
48
44
|
if inline?
|
|
49
45
|
mb.add_inline_image self, @filename
|
|
@@ -51,6 +47,5 @@ module Railgun
|
|
|
51
47
|
mb.add_attachment self, @filename
|
|
52
48
|
end
|
|
53
49
|
end
|
|
54
|
-
|
|
55
50
|
end
|
|
56
51
|
end
|
data/lib/railgun/errors.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Railgun
|
|
3
4
|
class Error < StandardError
|
|
4
|
-
|
|
5
5
|
attr_reader :object
|
|
6
6
|
|
|
7
7
|
def initialize(message = nil, object = nil)
|
|
@@ -15,7 +15,6 @@ module Railgun
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
class InternalError < Error
|
|
18
|
-
|
|
19
18
|
attr_reader :source_exception
|
|
20
19
|
|
|
21
20
|
def initialize(source_exc, message = nil, object = nil)
|
data/lib/railgun/mailer.rb
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Railgun
|
|
3
4
|
# Railgun::Mailer is an ActionMailer provider for sending mail through
|
|
4
5
|
# Mailgun.
|
|
5
6
|
class Mailer
|
|
6
|
-
|
|
7
7
|
# List of the headers that will be ignored when copying headers from `mail.header_fields`
|
|
8
|
-
IGNORED_HEADERS = %w[
|
|
8
|
+
IGNORED_HEADERS = %w[to from subject reply-to mime-version template].freeze
|
|
9
9
|
|
|
10
10
|
# [Hash] config ->
|
|
11
11
|
# Requires *at least* `api_key` and `domain` keys.
|
|
@@ -17,15 +17,15 @@ module Railgun
|
|
|
17
17
|
def initialize(config)
|
|
18
18
|
@config = config
|
|
19
19
|
|
|
20
|
-
[
|
|
21
|
-
raise Railgun::ConfigurationError.new("Config requires `#{k}` key", @config) unless @config.
|
|
20
|
+
%i[api_key domain].each do |k|
|
|
21
|
+
raise Railgun::ConfigurationError.new("Config requires `#{k}` key", @config) unless @config.key?(k)
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
@mg_client = Mailgun::Client.new(
|
|
25
25
|
config[:api_key],
|
|
26
26
|
config[:api_host] || 'api.mailgun.net',
|
|
27
27
|
config[:api_version] || 'v3',
|
|
28
|
-
config[:api_ssl].nil?
|
|
28
|
+
config[:api_ssl].nil? || config[:api_ssl],
|
|
29
29
|
false,
|
|
30
30
|
config[:timeout]
|
|
31
31
|
)
|
|
@@ -34,10 +34,10 @@ module Railgun
|
|
|
34
34
|
# To avoid exception in mail gem v2.6
|
|
35
35
|
@settings = { return_response: true }
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
return unless @config[:fake_message_send] || false
|
|
38
|
+
|
|
39
|
+
Rails.logger.info 'NOTE: fake message sending has been enabled for mailgun-ruby!'
|
|
40
|
+
@mg_client.enable_test_mode!
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def deliver!(mail)
|
|
@@ -52,7 +52,7 @@ module Railgun
|
|
|
52
52
|
mg_message = Railgun.transform_for_mailgun(mail)
|
|
53
53
|
response = @mg_client.send_message(@mg_domain, mg_message)
|
|
54
54
|
|
|
55
|
-
if response.code == 200
|
|
55
|
+
if response.code == 200
|
|
56
56
|
mg_id = response.to_h['id']
|
|
57
57
|
mail.message_id = mg_id
|
|
58
58
|
end
|
|
@@ -68,9 +68,9 @@ module Railgun
|
|
|
68
68
|
# Set @mg_domain from mail[:domain] header if present, then remove it to prevent being sent.
|
|
69
69
|
def set_mg_domain(mail)
|
|
70
70
|
return mail[:domain].value if mail[:domain]
|
|
71
|
+
|
|
71
72
|
domain
|
|
72
73
|
end
|
|
73
|
-
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
module_function
|
|
@@ -102,7 +102,7 @@ module Railgun
|
|
|
102
102
|
# note: this will filter out parameters such as `from`, `to`, and so forth
|
|
103
103
|
# as they are accepted as POST parameters on the message endpoint.
|
|
104
104
|
|
|
105
|
-
msg_headers =
|
|
105
|
+
msg_headers = {}
|
|
106
106
|
|
|
107
107
|
# h:* attributes (headers)
|
|
108
108
|
|
|
@@ -111,11 +111,11 @@ module Railgun
|
|
|
111
111
|
mail.headers(mail.mailgun_headers || {})
|
|
112
112
|
mail.header_fields.each do |field|
|
|
113
113
|
header = field.name.downcase
|
|
114
|
-
if msg_headers.include? header
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
msg_headers[header] = if msg_headers.include? header
|
|
115
|
+
[msg_headers[header], field.value].flatten
|
|
116
|
+
else
|
|
117
|
+
field.value
|
|
118
|
+
end
|
|
119
119
|
end
|
|
120
120
|
|
|
121
121
|
msg_headers.each do |k, v|
|
|
@@ -140,7 +140,7 @@ module Railgun
|
|
|
140
140
|
message['recipient-variables'] = mail.mailgun_recipient_variables.to_json if mail.mailgun_recipient_variables
|
|
141
141
|
|
|
142
142
|
# reject blank values
|
|
143
|
-
message.delete_if do |
|
|
143
|
+
message.delete_if do |_k, v|
|
|
144
144
|
next true if v.nil?
|
|
145
145
|
|
|
146
146
|
# if it's an array remove empty elements
|
|
@@ -149,7 +149,7 @@ module Railgun
|
|
|
149
149
|
v.respond_to?(:empty?) && v.empty?
|
|
150
150
|
end
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
message
|
|
153
153
|
end
|
|
154
154
|
|
|
155
155
|
# Acts on a Rails/ActionMailer message object and uses Mailgun::MessageBuilder
|
|
@@ -169,7 +169,7 @@ module Railgun
|
|
|
169
169
|
mb.body_text extract_body_text(mail)
|
|
170
170
|
mb.amp_html extract_amp_html(mail)
|
|
171
171
|
|
|
172
|
-
[
|
|
172
|
+
%i[to cc bcc].each do |rcpt_type|
|
|
173
173
|
addrs = mail[rcpt_type] || nil
|
|
174
174
|
case addrs
|
|
175
175
|
when String
|
|
@@ -196,7 +196,7 @@ module Railgun
|
|
|
196
196
|
attach.attach_to_message! mb
|
|
197
197
|
end
|
|
198
198
|
|
|
199
|
-
|
|
199
|
+
mb.message
|
|
200
200
|
end
|
|
201
201
|
|
|
202
202
|
# Returns the decoded HTML body from the Mail::Message object if available,
|
|
@@ -206,11 +206,9 @@ module Railgun
|
|
|
206
206
|
#
|
|
207
207
|
# @return [String]
|
|
208
208
|
def extract_body_html(mail)
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
nil
|
|
213
|
-
end
|
|
209
|
+
retrieve_html_part(mail).body.decoded || nil
|
|
210
|
+
rescue StandardError
|
|
211
|
+
nil
|
|
214
212
|
end
|
|
215
213
|
|
|
216
214
|
# Returns the decoded text body from the Mail::Message object if it is available,
|
|
@@ -220,11 +218,9 @@ module Railgun
|
|
|
220
218
|
#
|
|
221
219
|
# @return [String]
|
|
222
220
|
def extract_body_text(mail)
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
nil
|
|
227
|
-
end
|
|
221
|
+
retrieve_text_part(mail).body.decoded || nil
|
|
222
|
+
rescue StandardError
|
|
223
|
+
nil
|
|
228
224
|
end
|
|
229
225
|
|
|
230
226
|
# Returns the decoded AMP HTML from the Mail::Message object if it is available,
|
|
@@ -234,11 +230,9 @@ module Railgun
|
|
|
234
230
|
#
|
|
235
231
|
# @return [String]
|
|
236
232
|
def extract_amp_html(mail)
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
nil
|
|
241
|
-
end
|
|
233
|
+
retrieve_amp_part(mail).body.decoded || nil
|
|
234
|
+
rescue StandardError
|
|
235
|
+
nil
|
|
242
236
|
end
|
|
243
237
|
|
|
244
238
|
# Returns the mail object from the Mail::Message object if text part exists,
|
|
@@ -250,7 +244,8 @@ module Railgun
|
|
|
250
244
|
# @return [Mail::Message] mail message with its content-type = text/plain
|
|
251
245
|
def retrieve_text_part(mail)
|
|
252
246
|
return mail.text_part if mail.multipart?
|
|
253
|
-
|
|
247
|
+
|
|
248
|
+
(mail.mime_type =~ %r{^text/plain$}i) && mail
|
|
254
249
|
end
|
|
255
250
|
|
|
256
251
|
# Returns the mail object from the Mail::Message object if html part exists,
|
|
@@ -262,7 +257,8 @@ module Railgun
|
|
|
262
257
|
# @return [Mail::Message] mail message with its content-type = text/html
|
|
263
258
|
def retrieve_html_part(mail)
|
|
264
259
|
return mail.html_part if mail.multipart?
|
|
265
|
-
|
|
260
|
+
|
|
261
|
+
(mail.mime_type =~ %r{^text/html$}i) && mail
|
|
266
262
|
end
|
|
267
263
|
|
|
268
264
|
# Returns the mail object from the Mail::Message object if AMP part exists,
|