mailgun-ruby 1.4.4 → 1.4.6

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.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +721 -71
  3. data/README.md +4 -1
  4. data/lib/mailgun/api_keys/api_keys.rb +65 -0
  5. data/lib/mailgun/client.rb +2 -2
  6. data/lib/mailgun/domains/domain_keys.rb +5 -5
  7. data/lib/mailgun/domains/domains.rb +12 -9
  8. data/lib/mailgun/events/events.rb +1 -1
  9. data/lib/mailgun/helpers/api_version_checker.rb +23 -12
  10. data/lib/mailgun/helpers/deprecation.rb +32 -0
  11. data/lib/mailgun/lists/mailing_lists.rb +250 -0
  12. data/lib/mailgun/messages/message_builder.rb +17 -7
  13. data/lib/mailgun/response.rb +1 -1
  14. data/lib/mailgun/routes/routes.rb +98 -0
  15. data/lib/mailgun/tags/analytics_tags.rb +2 -2
  16. data/lib/mailgun/tags/tags.rb +16 -8
  17. data/lib/mailgun/version.rb +1 -1
  18. data/lib/mailgun/webhooks/account_webhooks.rb +2 -2
  19. data/lib/mailgun/webhooks/webhooks.rb +65 -18
  20. data/lib/mailgun.rb +2 -0
  21. metadata +7 -318
  22. data/.github/workflows/ci.yml +0 -49
  23. data/.gitignore +0 -24
  24. data/.rubocop.yml +0 -68
  25. data/.ruby-env.yml.example +0 -13
  26. data/Gemfile +0 -9
  27. data/Rakefile +0 -32
  28. data/docs/AccountWebhooks.md +0 -55
  29. data/docs/AnalyticsTags.md +0 -63
  30. data/docs/DomainKeys.md +0 -49
  31. data/docs/Domains.md +0 -82
  32. data/docs/EmailValidation.md +0 -34
  33. data/docs/Events.md +0 -48
  34. data/docs/Logs.md +0 -67
  35. data/docs/MessageBuilder.md +0 -120
  36. data/docs/Metrics.md +0 -108
  37. data/docs/OptInHandler.md +0 -103
  38. data/docs/Snippets.md +0 -519
  39. data/docs/Subaccounts.md +0 -68
  40. data/docs/Suppressions.md +0 -92
  41. data/docs/Templates.md +0 -92
  42. data/docs/Webhooks.md +0 -42
  43. data/docs/railgun/EmailValidation.md +0 -34
  44. data/docs/railgun/Overview.md +0 -11
  45. data/docs/railgun/Parameters.md +0 -83
  46. data/mailgun.gemspec +0 -47
  47. data/spec/integration/account_webhooks_spec.rb +0 -57
  48. data/spec/integration/analytics_tags_spec.rb +0 -54
  49. data/spec/integration/bounces_spec.rb +0 -45
  50. data/spec/integration/campaign_spec.rb +0 -62
  51. data/spec/integration/complaints_spec.rb +0 -40
  52. data/spec/integration/domain_keys_spec.rb +0 -124
  53. data/spec/integration/domains_spec.rb +0 -201
  54. data/spec/integration/email_validation_spec.rb +0 -73
  55. data/spec/integration/events_spec.rb +0 -28
  56. data/spec/integration/list_members_spec.rb +0 -64
  57. data/spec/integration/list_spec.rb +0 -59
  58. data/spec/integration/logs_spec.rb +0 -105
  59. data/spec/integration/mailer_spec.rb +0 -65
  60. data/spec/integration/mailgun_spec.rb +0 -198
  61. data/spec/integration/messages/sample_data/mime.txt +0 -38
  62. data/spec/integration/metrics_spec.rb +0 -224
  63. data/spec/integration/routes_spec.rb +0 -91
  64. data/spec/integration/stats_spec.rb +0 -17
  65. data/spec/integration/subaccounts_spec.rb +0 -57
  66. data/spec/integration/suppressions_spec.rb +0 -320
  67. data/spec/integration/templates_spec.rb +0 -137
  68. data/spec/integration/unsubscribes_spec.rb +0 -44
  69. data/spec/integration/webhook_spec.rb +0 -45
  70. data/spec/spec_helper.rb +0 -50
  71. data/spec/unit/client_spec.rb +0 -424
  72. data/spec/unit/connection/test_client.rb +0 -169
  73. data/spec/unit/events/events_spec.rb +0 -88
  74. data/spec/unit/exceptions/exceptions_spec.rb +0 -31
  75. data/spec/unit/helpers/api_version_checker_spec.rb +0 -206
  76. data/spec/unit/lists/opt_in_handler_spec.rb +0 -28
  77. data/spec/unit/mailgun_spec.rb +0 -171
  78. data/spec/unit/messages/batch_message_spec.rb +0 -148
  79. data/spec/unit/messages/message_builder_spec.rb +0 -745
  80. data/spec/unit/messages/sample_data/mailgun_icon.png +0 -0
  81. data/spec/unit/messages/sample_data/mime.txt +0 -38
  82. data/spec/unit/messages/sample_data/rackspace_logo.jpg +0 -0
  83. data/spec/unit/messages/sample_data/unknown.type +0 -0
  84. data/spec/unit/railgun/content_type_spec.rb +0 -89
  85. data/spec/unit/railgun/mailer_spec.rb +0 -391
  86. data/spec/unit/response_spec.rb +0 -225
  87. data/vcr_cassettes/For_the_suppressions_handling_class/creates_a_single_bounce.yml +0 -55
  88. data/vcr_cassettes/account_webhooks.yml +0 -269
  89. data/vcr_cassettes/analytics_tags.yml +0 -187
  90. data/vcr_cassettes/bounces.yml +0 -136
  91. data/vcr_cassettes/complaints.yml +0 -315
  92. data/vcr_cassettes/domains.todo.yml +0 -42
  93. data/vcr_cassettes/domains.yml +0 -1243
  94. data/vcr_cassettes/email_validation.yml +0 -152
  95. data/vcr_cassettes/events.yml +0 -69
  96. data/vcr_cassettes/exceptions-invalid-api-key.yml +0 -52
  97. data/vcr_cassettes/exceptions-invalid-data.yml +0 -52
  98. data/vcr_cassettes/exceptions-not-allowed.yml +0 -54
  99. data/vcr_cassettes/exceptions.yml +0 -92
  100. data/vcr_cassettes/list_members.yml +0 -328
  101. data/vcr_cassettes/logs.yml +0 -53
  102. data/vcr_cassettes/mailer_invalid_domain.yml +0 -109
  103. data/vcr_cassettes/mailing_list.todo.yml +0 -43
  104. data/vcr_cassettes/mailing_list.yml +0 -682
  105. data/vcr_cassettes/message_deliver.yml +0 -149
  106. data/vcr_cassettes/metrics.yml +0 -116
  107. data/vcr_cassettes/routes.yml +0 -359
  108. data/vcr_cassettes/send_message.yml +0 -158
  109. data/vcr_cassettes/stats.yml +0 -48
  110. data/vcr_cassettes/subaccounts.yml +0 -270
  111. data/vcr_cassettes/suppressions.yml +0 -1392
  112. data/vcr_cassettes/templates.yml +0 -1065
  113. data/vcr_cassettes/unsubscribes.yml +0 -401
  114. data/vcr_cassettes/webhooks.yml +0 -246
@@ -1,424 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'mailgun'
5
- require 'mailgun/exceptions/exceptions'
6
-
7
- describe Mailgun::Client do
8
- # ---------------------------------------------------------------------------
9
- # Shared helpers
10
- # ---------------------------------------------------------------------------
11
- # Build a client in test mode so no real HTTP calls are ever made.
12
- subject(:client) { described_class.new(api_key, 'api.mailgun.net', 'v3', true, true) }
13
-
14
- let(:api_key) { 'test-api-key-abc123' }
15
- let(:message_params) do
16
- {
17
- from: 'bob@example.com',
18
- to: 'sally@example.com',
19
- subject: 'Hello!',
20
- text: 'Test body.'
21
- }
22
- end
23
- let(:domain) { 'example.com' }
24
-
25
- # Fake HTTP response double reusable across examples
26
- def fake_http_response(body: '{"message":"ok"}', status: 200)
27
- double('http_response', body: body, status: status)
28
- end
29
-
30
- # ---------------------------------------------------------------------------
31
- # .new / #initialize
32
- # ---------------------------------------------------------------------------
33
- describe '#initialize' do
34
- it 'instantiates without raising' do
35
- expect { described_class.new(api_key) }.not_to raise_error
36
- end
37
-
38
- it 'stores the api_version' do
39
- c = described_class.new(api_key, 'api.mailgun.net', 'v3')
40
- expect(c.api_version).to eq('v3')
41
- end
42
-
43
- it 'defaults test_mode to false when not specified' do
44
- # Pass test_mode = false explicitly to avoid picking up global config
45
- c = described_class.new(api_key, 'api.mailgun.net', 'v3', true, false)
46
- expect(c.test_mode?).to be(false)
47
- end
48
-
49
- it 'accepts test_mode = true' do
50
- c = described_class.new(api_key, 'api.mailgun.net', nil, true, true)
51
- expect(c.test_mode?).to be(true)
52
- end
53
-
54
- it 'supports EU api host' do
55
- expect { described_class.new(api_key, 'api.eu.mailgun.net') }.not_to raise_error
56
- end
57
- end
58
-
59
- # ---------------------------------------------------------------------------
60
- # #test_mode?
61
- # ---------------------------------------------------------------------------
62
- describe '#test_mode?' do
63
- it 'returns true when the client is in test mode' do
64
- expect(client.test_mode?).to be(true)
65
- end
66
-
67
- it 'returns false when test mode has been disabled' do
68
- client.disable_test_mode!
69
- expect(client.test_mode?).to be(false)
70
- end
71
- end
72
-
73
- # ---------------------------------------------------------------------------
74
- # #enable_test_mode!
75
- # ---------------------------------------------------------------------------
76
- describe '#enable_test_mode!' do
77
- subject(:non_test_client) do
78
- described_class.new(api_key, 'api.mailgun.net', 'v3', true, false)
79
- end
80
-
81
- it 'sets test_mode to true' do
82
- non_test_client.enable_test_mode!
83
- expect(non_test_client.test_mode?).to be(true)
84
- end
85
- end
86
-
87
- # ---------------------------------------------------------------------------
88
- # #disable_test_mode!
89
- # ---------------------------------------------------------------------------
90
- describe '#disable_test_mode!' do
91
- it 'sets test_mode to false' do
92
- client.disable_test_mode!
93
- expect(client.test_mode?).to be(false)
94
- end
95
- end
96
-
97
- # ---------------------------------------------------------------------------
98
- # .deliveries
99
- # ---------------------------------------------------------------------------
100
- describe '.deliveries' do
101
- before { described_class.deliveries.clear }
102
-
103
- it 'returns an Array' do
104
- expect(described_class.deliveries).to be_an(Array)
105
- end
106
-
107
- it 'accumulates messages sent in test mode' do
108
- client.send_message(domain, message_params)
109
- expect(described_class.deliveries).not_to be_empty
110
- end
111
-
112
- it 'stores the message data sent in test mode' do
113
- client.send_message(domain, message_params)
114
- expect(described_class.deliveries.last).to include(from: 'bob@example.com')
115
- end
116
-
117
- it 'accumulates multiple deliveries' do
118
- 2.times { client.send_message(domain, message_params) }
119
- expect(described_class.deliveries.size).to eq(2)
120
- end
121
- end
122
-
123
- # ---------------------------------------------------------------------------
124
- # #send_message — test mode
125
- # ---------------------------------------------------------------------------
126
- describe '#send_message (test mode)' do
127
- before { described_class.deliveries.clear }
128
-
129
- it 'returns a Mailgun::Response' do
130
- result = client.send_message(domain, message_params)
131
- expect(result).to be_a(Mailgun::Response)
132
- end
133
-
134
- it 'returns a 200 status response in test mode' do
135
- result = client.send_message(domain, message_params)
136
- expect(result.code).to eq(200)
137
- end
138
-
139
- it 'returns a body containing the Queued message in test mode' do
140
- result = client.send_message(domain, message_params)
141
- body = JSON.parse(result.body)
142
- expect(body['message']).to eq('Queued. Thank you.')
143
- end
144
-
145
- it 'returns a body with a unique test-mode message id' do
146
- result = client.send_message(domain, message_params)
147
- body = JSON.parse(result.body)
148
- expect(body['id']).to match(/test-mode-mail-.+@localhost/)
149
- end
150
-
151
- it 'does not make real HTTP calls in test mode' do
152
- expect(client.instance_variable_get(:@http_client)).not_to receive(:post)
153
- client.send_message(domain, message_params)
154
- end
155
-
156
- it 'records a copy of message data in .deliveries' do
157
- client.send_message(domain, message_params)
158
- expect(described_class.deliveries.last[:subject]).to eq('Hello!')
159
- end
160
-
161
- context 'with a MessageBuilder object' do
162
- it 'stores the MessageBuilder in .deliveries' do
163
- mb = Mailgun::MessageBuilder.new
164
- mb.set_from_address('bob@example.com')
165
- mb.add_recipient(:to, 'sally@example.com')
166
- mb.set_subject('Hello via builder!')
167
- mb.set_text_body('Body text.')
168
-
169
- result = client.send_message(domain, mb)
170
- expect(result).to be_a(Mailgun::Response)
171
- expect(described_class.deliveries.last).to be_a(Mailgun::MessageBuilder)
172
- end
173
-
174
- it 'raises a Mailgun::ParameterError when mb is empty' do
175
- mb = Mailgun::MessageBuilder.new
176
-
177
- expect { client.send_message(domain, mb) }
178
- .to raise_error(Mailgun::ParameterError)
179
- end
180
-
181
- it 'raises a Mailgun::ParameterError when from is missing' do
182
- mb = Mailgun::MessageBuilder.new
183
- mb.add_recipient(:to, 'sally@example.com')
184
-
185
- expect { client.send_message(domain, mb) }
186
- .to raise_error(Mailgun::ParameterError)
187
- end
188
- end
189
- end
190
-
191
- # ---------------------------------------------------------------------------
192
- # #send_message — live mode (HTTP mocked at the Faraday level)
193
- # ---------------------------------------------------------------------------
194
- describe '#send_message (live mode)' do
195
- subject(:live_client) do
196
- described_class.new(api_key, 'api.mailgun.net', 'v3', true, false)
197
- end
198
-
199
- let(:success_response) { fake_http_response }
200
-
201
- before do
202
- allow(live_client.instance_variable_get(:@http_client))
203
- .to receive(:post)
204
- .and_return(success_response)
205
- end
206
-
207
- it 'posts to the messages endpoint and returns a Response' do
208
- result = live_client.send_message(domain, message_params)
209
- expect(result).to be_a(Mailgun::Response)
210
- end
211
-
212
- it 'strips nil values from the data hash before posting' do
213
- params_with_nil = message_params.merge(cc: nil)
214
- http = live_client.instance_variable_get(:@http_client)
215
- expect(http).to receive(:post) do |_path, data, *|
216
- expect(data).not_to have_key(:cc)
217
- success_response
218
- end
219
- live_client.send_message(domain, params_with_nil)
220
- end
221
-
222
- it 'raises CommunicationError when the underlying request fails' do
223
- allow(live_client.instance_variable_get(:@http_client))
224
- .to receive(:post)
225
- .and_raise(StandardError, 'connection refused')
226
-
227
- expect { live_client.send_message(domain, message_params) }
228
- .to raise_error(Mailgun::CommunicationError)
229
- end
230
-
231
- context 'with a MessageBuilder object' do
232
- it 'posts to the messages endpoint and returns a Response' do
233
- mb = Mailgun::MessageBuilder.new
234
- mb.set_from_address('bob@example.com')
235
- mb.add_recipient(:to, 'sally@example.com')
236
- mb.set_subject('Hello via builder!')
237
- mb.set_text_body('Body text.')
238
-
239
- result = live_client.send_message(domain, mb)
240
- expect(result).to be_a(Mailgun::Response)
241
- end
242
- end
243
- end
244
-
245
- # ---------------------------------------------------------------------------
246
- # #get
247
- # ---------------------------------------------------------------------------
248
- describe '#get' do
249
- let(:http_response) { fake_http_response }
250
-
251
- before do
252
- allow(client.instance_variable_get(:@http_client))
253
- .to receive(:get)
254
- .and_return(http_response)
255
- end
256
-
257
- it 'returns a Mailgun::Response' do
258
- result = client.get("#{domain}/events", { event: 'delivered' })
259
- expect(result).to be_a(Mailgun::Response)
260
- end
261
-
262
- it 'passes the resource path through to the http client' do
263
- http = client.instance_variable_get(:@http_client)
264
- expect(http).to receive(:get).with("#{domain}/events", anything, anything)
265
- .and_return(http_response)
266
- client.get("#{domain}/events")
267
- end
268
-
269
- it 'raises CommunicationError on failure' do
270
- allow(client.instance_variable_get(:@http_client))
271
- .to receive(:get)
272
- .and_raise(StandardError, 'timeout')
273
-
274
- expect { client.get("#{domain}/events") }
275
- .to raise_error(Mailgun::CommunicationError)
276
- end
277
- end
278
-
279
- # ---------------------------------------------------------------------------
280
- # #post
281
- # ---------------------------------------------------------------------------
282
- describe '#post' do
283
- let(:http_response) { fake_http_response }
284
-
285
- before do
286
- allow(client.instance_variable_get(:@http_client))
287
- .to receive(:post)
288
- .and_return(http_response)
289
- end
290
-
291
- it 'returns a Mailgun::Response' do
292
- result = client.post("#{domain}/messages", message_params)
293
- expect(result).to be_a(Mailgun::Response)
294
- end
295
-
296
- it 'raises CommunicationError on failure' do
297
- allow(client.instance_variable_get(:@http_client))
298
- .to receive(:post)
299
- .and_raise(StandardError, 'connection error')
300
-
301
- expect { client.post("#{domain}/messages", message_params) }
302
- .to raise_error(Mailgun::CommunicationError)
303
- end
304
- end
305
-
306
- # ---------------------------------------------------------------------------
307
- # #put
308
- # ---------------------------------------------------------------------------
309
- describe '#put' do
310
- let(:http_response) { fake_http_response }
311
-
312
- before do
313
- allow(client.instance_variable_get(:@http_client))
314
- .to receive(:put)
315
- .and_return(http_response)
316
- end
317
-
318
- it 'returns a Mailgun::Response' do
319
- result = client.put("#{domain}/routes/abc123", { description: 'updated' })
320
- expect(result).to be_a(Mailgun::Response)
321
- end
322
-
323
- it 'raises CommunicationError on failure' do
324
- allow(client.instance_variable_get(:@http_client))
325
- .to receive(:put)
326
- .and_raise(StandardError, 'connection error')
327
-
328
- expect { client.put("#{domain}/routes/abc123", {}) }
329
- .to raise_error(Mailgun::CommunicationError)
330
- end
331
- end
332
-
333
- # ---------------------------------------------------------------------------
334
- # #delete
335
- # ---------------------------------------------------------------------------
336
- describe '#delete' do
337
- let(:http_response) { fake_http_response(body: '{"message":"Bounced address has been removed"}') }
338
-
339
- before do
340
- allow(client.instance_variable_get(:@http_client))
341
- .to receive(:delete)
342
- .and_return(http_response)
343
- end
344
-
345
- it 'returns a Mailgun::Response' do
346
- result = client.delete("#{domain}/bounces/test@example.com")
347
- expect(result).to be_a(Mailgun::Response)
348
- end
349
-
350
- it 'raises CommunicationError on failure' do
351
- allow(client.instance_variable_get(:@http_client))
352
- .to receive(:delete)
353
- .and_raise(StandardError, 'connection error')
354
-
355
- expect { client.delete("#{domain}/bounces/test@example.com") }
356
- .to raise_error(Mailgun::CommunicationError)
357
- end
358
-
359
- it 'sends params in the request' do
360
- result = client.delete("#{domain}/bounces/test@example.com", params: 'test')
361
- expect(result).to be_a(Mailgun::Response)
362
- end
363
-
364
- it 'sends params in the request when body_params truthy' do
365
- result = client.delete("#{domain}/bounces/test@example.com", params: 'test', body_params: true)
366
- expect(result).to be_a(Mailgun::Response)
367
- end
368
- end
369
-
370
- # ---------------------------------------------------------------------------
371
- # #set_api_key
372
- # ---------------------------------------------------------------------------
373
- describe '#set_api_key' do
374
- it 'does not raise when updating the api key' do
375
- expect { client.set_api_key('new-key-xyz') }.not_to raise_error
376
- end
377
- end
378
-
379
- # ---------------------------------------------------------------------------
380
- # #set_subaccount / #reset_subaccount
381
- # ---------------------------------------------------------------------------
382
- describe '#set_subaccount' do
383
- it 'adds the subaccount header to the http client' do
384
- client.set_subaccount('subaccount-id-123')
385
- headers = client.instance_variable_get(:@http_client).headers
386
- expect(headers[Mailgun::Client::SUBACCOUNT_HEADER]).to eq('subaccount-id-123')
387
- end
388
- end
389
-
390
- describe '#reset_subaccount' do
391
- it 'removes the subaccount header from the http client' do
392
- client.set_subaccount('subaccount-id-123')
393
- client.reset_subaccount
394
- headers = client.instance_variable_get(:@http_client).headers
395
- expect(headers[Mailgun::Client::SUBACCOUNT_HEADER]).to be_nil
396
- end
397
- end
398
-
399
- # ---------------------------------------------------------------------------
400
- # #suppressions
401
- # ---------------------------------------------------------------------------
402
- describe '#suppressions' do
403
- it 'returns a Mailgun::Suppressions instance' do
404
- expect(client.suppressions(domain)).to be_a(Mailgun::Suppressions)
405
- end
406
-
407
- it 'scopes the suppressions client to the given domain' do
408
- suppressions = client.suppressions(domain)
409
- expect(suppressions).to be_a(Mailgun::Suppressions)
410
- end
411
- end
412
-
413
- # ---------------------------------------------------------------------------
414
- # Mailgun.configure integration
415
- # ---------------------------------------------------------------------------
416
- describe 'Mailgun.configure integration' do
417
- after { Mailgun.instance_variable_set(:@configuration, nil) }
418
-
419
- it 'picks up the api_key from Mailgun.configure' do
420
- Mailgun.configure { |config| config.api_key = 'configured-key' }
421
- expect { described_class.new }.not_to raise_error
422
- end
423
- end
424
- end
@@ -1,169 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'time'
4
- require 'json'
5
-
6
- module Mailgun
7
- class UnitClient < Mailgun::Client
8
- attr_reader :options, :block, :body, :code, :response
9
-
10
- def initialize(endpoint, &block)
11
- @block = block
12
- @endpoint = endpoint
13
- @body = nil
14
- @code = nil
15
- end
16
-
17
- def [](endpoint, &new_block)
18
- if block_given?
19
- self.class.new(endpoint, &new_block)
20
- elsif block
21
- self.class.new(endpoint, &block)
22
- else
23
- self.class.new(endpoint)
24
- end
25
- end
26
-
27
- def send_message(working_domain, data)
28
- perform_data_validation(working_domain, data)
29
- case data
30
- when Hash
31
- if data.key?(:message)
32
- data[:message] = convert_string_to_file(data[:message]) if data[:message].is_a?(String)
33
- post("#{working_domain}/messages.mime", data)
34
- else
35
- post("#{working_domain}/messages", data)
36
- end
37
- when MessageBuilder
38
- post("#{working_domain}/messages", data.message)
39
- else
40
- raise ParameterError.new('Unknown data type for data parameter.', data)
41
- end
42
- end
43
-
44
- def post(_path, _data)
45
- Mailgun::Response.new(response_generator(@endpoint))
46
- rescue StandardError => e
47
- p e
48
- raise CommunicationError.new(e), e.response if e.respond_to? :response
49
-
50
- raise CommunicationError, e.message
51
- end
52
-
53
- def get(_path, _query_string = nil)
54
- Mailgun::Response.new(response_generator(@endpoint))
55
- rescue StandardError => e
56
- raise CommunicationError.new(e), e.response
57
- end
58
-
59
- def put(_path, _data)
60
- Mailgun::Response.new(response_generator(@endpoint))
61
- rescue StandardError => e
62
- raise CommunicationError.new(e), e.response
63
- end
64
-
65
- def delete(_path)
66
- Mailgun::Response.new(response_generator(@endpoint))
67
- rescue StandardError => e
68
- raise CommunicationError.new(e), e.response
69
- end
70
-
71
- private
72
-
73
- def perform_data_validation(working_domain, data)
74
- raise ParameterError.new('Missing working domain', working_domain) unless working_domain
75
- return true unless data.is_a?(Hash) && data.present?
76
-
77
- message = data.respond_to?(:message) ? data.message : data
78
-
79
- if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
80
- raise ParameterError.new(
81
- 'Missing `to` recipient, message should contain at least 1 recipient',
82
- working_domain
83
- )
84
- end
85
- return unless message.fetch('from', []).empty? && message.fetch(:from, []).empty?
86
-
87
- raise ParameterError.new(
88
- 'Missing a `from` sender, message should contain at least 1 `from` sender',
89
- working_domain
90
- )
91
- end
92
-
93
- def response_generator(resource_endpoint)
94
- if resource_endpoint == 'messages'
95
- t = Time.now
96
- id = "<#{t.to_i}.#{rand(99_999_999)}.5817@example.com>"
97
- return Response.from_hash({ body: JSON.generate({ 'message' => 'Queued. Thank you.', 'id' => id }) })
98
- end
99
- if resource_endpoint == 'bounces'
100
- return Response.from_hash(
101
- { body: JSON.generate(
102
- {
103
- 'total_count' => 1,
104
- 'items' =>
105
- {
106
- 'created_at' => 'Fri, 21 Oct 2011 11:02:55 GMT',
107
- 'status' => 550,
108
- 'address' => 'baz@example.com',
109
- 'error' => "Message was not accepted -- invalid mailbox. \
110
- Local mailbox baz@example.com is unavailable: user not found"
111
- }
112
- }
113
- ) }
114
- )
115
- end
116
- if resource_endpoint == 'lists'
117
- return Response.from_hash(
118
- { body: JSON.generate(
119
- {
120
- 'member' =>
121
- {
122
- 'vars' => { 'age' => 26 },
123
- 'name' => 'Foo Bar',
124
- 'subscribed' => false,
125
- 'address' => 'bar@example.com'
126
- },
127
- 'message' => 'Mailing list member has been updated'
128
- }
129
- ) }
130
- )
131
- end
132
- if resource_endpoint == 'campaigns'
133
- return Response.from_hash(
134
- {
135
- body: JSON.generate(
136
- {
137
- 'message' => 'Campaign has been deleted',
138
- 'id' => 'ABC123'
139
- }
140
- )
141
- }
142
- )
143
- end
144
- return unless resource_endpoint == 'events'
145
-
146
- Response.from_hash(
147
- { body: JSON.generate(
148
- { 'items' => [],
149
- 'paging' => {
150
- 'next' =>
151
- "https://api.mailgun.net/v3/thisisatestdomainformailgun.com/events/W3siYiI6I\
152
- CIyMDE0LTA1LTA3VDAwOjQ1OjUxLjc0MDg5MiswMDowMCIsICJlIjogIjIwMTQtMDUtMDVUMDA6NDU6NTEuNzQwOTgzK\
153
- zAwOjAwIn0sIHsiYiI6ICIyMDE0LTA1LTA3VDAwOjQ1OjUxLjc0MDg5MiswMDowMCIsICJlIjogIjIwMTQtMDUtMDVUM\
154
- DA6NDU6NTEuNzQwOTgzKzAwOjAwIn0sIFsiZiJdLCBudWxsLCB7ImFjY291bnQuaWQiOiAiNGU4MjMwZjYxNDc2ZDg2N\
155
- zEzMDBjNDc2IiwgImRvbWFpbi5uYW1lIjogInRoaXNpc2F0ZXN0ZG9tYWluZm9ybWFpbGd1bi5jb20iLCAic2V2ZXJpd\
156
- HkiOiAiTk9UIGludGVybmFsIn0sIDEwMCwgbnVsbF0=",
157
- 'previous' =>
158
- "https://api.mailgun.net/v2/thisisatestdomainformailgun.com/events/W3siYiI6IC\
159
- IyMDE0LTA1LTA3VDAwOjQ1OjUxLjc0MDg5MiswMDowMCIsICJlIjogIjIwMTQtMDUtMDVUMDA6NDU6NTEuNzQwOTgzKzA\
160
- wOjAwIn0sIHsiYiI6ICIyMDE0LTA1LTA3VDAwOjQ1OjUxLjc0MDg5MiswMDowMCIsICJlIjogIjIwMTQtMDUtMDdUMDA6\
161
- NDU6NTEuNzQxODkyKzAwOjAwIn0sIFsicCIsICJmIl0sIG51bGwsIHsiYWNjb3VudC5pZCI6ICI0ZTgyMzBmNjE0NzZkO\
162
- DY3MTMwMGM0NzYiLCAiZG9tYWluLm5hbWUiOiAidGhpc2lzYXRlc3Rkb21haW5mb3JtYWlsZ3VuLmNvbSIsICJzZXZlcm\
163
- l0eSI6ICJOT1QgaW50ZXJuYWwifSwgMTAwLCBudWxsXQ=="
164
- } }
165
- ) }
166
- )
167
- end
168
- end
169
- end
@@ -1,88 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe 'The method get' do
6
- it 'returns a proper hash of log data.' do
7
- @mg_obj = Mailgun::UnitClient.new('events')
8
- events = Mailgun::Events.new(@mg_obj, 'samples.mailgun.org')
9
- result = events.get
10
-
11
- expect(result.body).to include('items')
12
- expect(result.body).to include('paging')
13
- end
14
- end
15
-
16
- describe 'Pagination' do
17
- it 'returns a proper hash of log data.' do
18
- @mg_obj = Mailgun::UnitClient.new('events')
19
- events = Mailgun::Events.new(@mg_obj, 'samples.mailgun.org')
20
- result = events.get
21
-
22
- json = JSON.parse(result.body)
23
- expect(json).to include('paging')
24
- expect(json['paging']).to include('next')
25
- expect(json['paging']).to include('previous')
26
- end
27
-
28
- it 'calculates proper next-page url' do
29
- events = Mailgun::Events.new(@mg_obj, 'samples.mailgun.org')
30
- output = events.send(
31
- :extract_endpoint_from,
32
- "/v3/samples.mailgun.org/events/W3siYiI6ICIyMDE3LTA1\
33
- LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMj\
34
- A6MDY6NTQuNTc3KzAwOjAwIn0sIHsiYiI6ICIyMDE3LTA1LTEwVDIwOjA2\
35
- OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNT\
36
- c3KzAwOjAwIn0sIFsiZiJdLCBudWxsLCBbWyJhY2NvdW50LmlkIiwgIjU4\
37
- MDUyMTg2NzhmYTE2MTNjNzkwYjUwZiJdLCBbImRvbWFpbi5uYW1lIiwgIn\
38
- NhbmRib3gyOTcwMTUyYWYzZDM0NTU5YmZjN2U3MTcwM2E2Y2YyNC5tYWls\
39
- Z3VuLm9yZyJdXSwgMTAwLCBudWxsXQ=="
40
- )
41
-
42
- expect(output).to eq "W3siYiI6ICIyMDE3LTA1LTEwVDIwOjA2O\
43
- jU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3\
44
- KzAwOjAwIn0sIHsiYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDo\
45
- wMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIF\
46
- siZiJdLCBudWxsLCBbWyJhY2NvdW50LmlkIiwgIjU4MDUyMTg2NzhmYTE2M\
47
- TNjNzkwYjUwZiJdLCBbImRvbWFpbi5uYW1lIiwgInNhbmRib3gyOTcwMTUy\
48
- YWYzZDM0NTU5YmZjN2U3MTcwM2E2Y2YyNC5tYWlsZ3VuLm9yZyJdXSwgMTA\
49
- wLCBudWxsXQ=="
50
- end
51
- end
52
-
53
- describe 'The method next' do
54
- it 'returns the next series of data.' do
55
- @mg_obj = Mailgun::UnitClient.new('events')
56
- events = Mailgun::Events.new(@mg_obj, 'samples.mailgun.org')
57
- result = events.next
58
-
59
- expect(result.body).to include('items')
60
- expect(result.body).to include('paging')
61
- end
62
- end
63
-
64
- describe 'The method previous' do
65
- it 'returns the previous series of data.' do
66
- @mg_obj = Mailgun::UnitClient.new('events')
67
- events = Mailgun::Events.new(@mg_obj, 'samples.mailgun.org')
68
- result = events.previous
69
-
70
- expect(result.body).to include('items')
71
- expect(result.body).to include('paging')
72
- end
73
- end
74
-
75
- describe 'The method each' do
76
- it 'iterates over all event items.' do
77
- @mg_obj = Mailgun::UnitClient.new('events')
78
- events = Mailgun::Events.new(@mg_obj, 'samples.mailgun.org')
79
- # Events from the UnitClient are actually empty.
80
- count = 0
81
- events.each do |_e|
82
- count += 1
83
- end
84
-
85
- # Better than nothing..
86
- expect(count).to eq(0)
87
- end
88
- end