mailgun-ruby 1.4.5 → 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 (120) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -1
  3. data/README.md +3 -1
  4. data/lib/mailgun/api_keys/api_keys.rb +2 -2
  5. data/lib/mailgun/client.rb +1 -1
  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 +17 -9
  20. data/lib/mailgun.rb +1 -0
  21. metadata +6 -326
  22. data/.github/workflows/ci.yml +0 -49
  23. data/.github_changelog_generator +0 -1
  24. data/.gitignore +0 -24
  25. data/.rubocop.yml +0 -68
  26. data/.ruby-env.yml.example +0 -13
  27. data/Gemfile +0 -9
  28. data/Rakefile +0 -32
  29. data/docs/AccountWebhooks.md +0 -55
  30. data/docs/AnalyticsTags.md +0 -63
  31. data/docs/ApiKeys.md +0 -65
  32. data/docs/DomainKeys.md +0 -49
  33. data/docs/Domains.md +0 -82
  34. data/docs/EmailValidation.md +0 -34
  35. data/docs/Events.md +0 -48
  36. data/docs/Logs.md +0 -67
  37. data/docs/MessageBuilder.md +0 -120
  38. data/docs/Metrics.md +0 -108
  39. data/docs/OptInHandler.md +0 -103
  40. data/docs/Snippets.md +0 -519
  41. data/docs/Subaccounts.md +0 -68
  42. data/docs/Suppressions.md +0 -92
  43. data/docs/Templates.md +0 -92
  44. data/docs/Webhooks.md +0 -65
  45. data/docs/railgun/EmailValidation.md +0 -34
  46. data/docs/railgun/Overview.md +0 -11
  47. data/docs/railgun/Parameters.md +0 -83
  48. data/mailgun.gemspec +0 -47
  49. data/spec/integration/account_webhooks_spec.rb +0 -57
  50. data/spec/integration/analytics_tags_spec.rb +0 -54
  51. data/spec/integration/api_keys_spec.rb +0 -50
  52. data/spec/integration/bounces_spec.rb +0 -45
  53. data/spec/integration/campaign_spec.rb +0 -62
  54. data/spec/integration/complaints_spec.rb +0 -40
  55. data/spec/integration/domain_keys_spec.rb +0 -124
  56. data/spec/integration/domains_spec.rb +0 -201
  57. data/spec/integration/email_validation_spec.rb +0 -73
  58. data/spec/integration/events_spec.rb +0 -28
  59. data/spec/integration/list_members_spec.rb +0 -64
  60. data/spec/integration/list_spec.rb +0 -59
  61. data/spec/integration/logs_spec.rb +0 -105
  62. data/spec/integration/mailer_spec.rb +0 -65
  63. data/spec/integration/mailgun_spec.rb +0 -198
  64. data/spec/integration/messages/sample_data/mime.txt +0 -38
  65. data/spec/integration/metrics_spec.rb +0 -224
  66. data/spec/integration/routes_spec.rb +0 -91
  67. data/spec/integration/stats_spec.rb +0 -17
  68. data/spec/integration/subaccounts_spec.rb +0 -57
  69. data/spec/integration/suppressions_spec.rb +0 -320
  70. data/spec/integration/templates_spec.rb +0 -137
  71. data/spec/integration/unsubscribes_spec.rb +0 -44
  72. data/spec/integration/webhook_spec.rb +0 -79
  73. data/spec/spec_helper.rb +0 -53
  74. data/spec/unit/client_spec.rb +0 -448
  75. data/spec/unit/connection/test_client.rb +0 -169
  76. data/spec/unit/events/events_spec.rb +0 -88
  77. data/spec/unit/exceptions/exceptions_spec.rb +0 -31
  78. data/spec/unit/helpers/api_version_checker_spec.rb +0 -206
  79. data/spec/unit/lists/opt_in_handler_spec.rb +0 -28
  80. data/spec/unit/mailgun_spec.rb +0 -171
  81. data/spec/unit/messages/batch_message_spec.rb +0 -148
  82. data/spec/unit/messages/message_builder_spec.rb +0 -745
  83. data/spec/unit/messages/sample_data/mailgun_icon.png +0 -0
  84. data/spec/unit/messages/sample_data/mime.txt +0 -38
  85. data/spec/unit/messages/sample_data/rackspace_logo.jpg +0 -0
  86. data/spec/unit/messages/sample_data/unknown.type +0 -0
  87. data/spec/unit/railgun/content_type_spec.rb +0 -89
  88. data/spec/unit/railgun/mailer_spec.rb +0 -391
  89. data/spec/unit/response_spec.rb +0 -225
  90. data/spec/unit/webhooks_spec.rb +0 -121
  91. data/vcr_cassettes/For_the_suppressions_handling_class/creates_a_single_bounce.yml +0 -55
  92. data/vcr_cassettes/account_webhooks.yml +0 -269
  93. data/vcr_cassettes/analytics_tags.yml +0 -187
  94. data/vcr_cassettes/api_keys.yml +0 -212
  95. data/vcr_cassettes/bounces.yml +0 -136
  96. data/vcr_cassettes/complaints.yml +0 -315
  97. data/vcr_cassettes/domains.todo.yml +0 -42
  98. data/vcr_cassettes/domains.yml +0 -1243
  99. data/vcr_cassettes/email_validation.yml +0 -152
  100. data/vcr_cassettes/events.yml +0 -69
  101. data/vcr_cassettes/exceptions-invalid-api-key.yml +0 -52
  102. data/vcr_cassettes/exceptions-invalid-data.yml +0 -52
  103. data/vcr_cassettes/exceptions-not-allowed.yml +0 -54
  104. data/vcr_cassettes/exceptions.yml +0 -92
  105. data/vcr_cassettes/list_members.yml +0 -328
  106. data/vcr_cassettes/logs.yml +0 -53
  107. data/vcr_cassettes/mailer_invalid_domain.yml +0 -109
  108. data/vcr_cassettes/mailing_list.todo.yml +0 -43
  109. data/vcr_cassettes/mailing_list.yml +0 -682
  110. data/vcr_cassettes/message_deliver.yml +0 -149
  111. data/vcr_cassettes/metrics.yml +0 -116
  112. data/vcr_cassettes/routes.yml +0 -359
  113. data/vcr_cassettes/send_message.yml +0 -158
  114. data/vcr_cassettes/stats.yml +0 -48
  115. data/vcr_cassettes/subaccounts.yml +0 -270
  116. data/vcr_cassettes/suppressions.yml +0 -1392
  117. data/vcr_cassettes/templates.yml +0 -1065
  118. data/vcr_cassettes/unsubscribes.yml +0 -401
  119. data/vcr_cassettes/webhooks.yml +0 -246
  120. data/vcr_cassettes/webhooks_v4.yml +0 -275
@@ -1,448 +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
-
58
- context 'when test_mode is not passed' do
59
- around do |example|
60
- original = Mailgun.test_mode
61
- example.run
62
- ensure
63
- Mailgun.test_mode = original
64
- end
65
-
66
- it 'enables test mode when Mailgun.test_mode is true' do
67
- Mailgun.test_mode = true
68
- expect(described_class.new(api_key).test_mode?).to be(true)
69
- end
70
-
71
- it 'disables test mode when Mailgun.test_mode is false' do
72
- Mailgun.test_mode = false
73
- expect(described_class.new(api_key).test_mode?).to be(false)
74
- end
75
-
76
- it 'disables test mode when Mailgun.test_mode is not set' do
77
- Mailgun.test_mode = nil
78
- expect(described_class.new(api_key).test_mode?).to be(false)
79
- end
80
- end
81
- end
82
-
83
- # ---------------------------------------------------------------------------
84
- # #test_mode?
85
- # ---------------------------------------------------------------------------
86
- describe '#test_mode?' do
87
- it 'returns true when the client is in test mode' do
88
- expect(client.test_mode?).to be(true)
89
- end
90
-
91
- it 'returns false when test mode has been disabled' do
92
- client.disable_test_mode!
93
- expect(client.test_mode?).to be(false)
94
- end
95
- end
96
-
97
- # ---------------------------------------------------------------------------
98
- # #enable_test_mode!
99
- # ---------------------------------------------------------------------------
100
- describe '#enable_test_mode!' do
101
- subject(:non_test_client) do
102
- described_class.new(api_key, 'api.mailgun.net', 'v3', true, false)
103
- end
104
-
105
- it 'sets test_mode to true' do
106
- non_test_client.enable_test_mode!
107
- expect(non_test_client.test_mode?).to be(true)
108
- end
109
- end
110
-
111
- # ---------------------------------------------------------------------------
112
- # #disable_test_mode!
113
- # ---------------------------------------------------------------------------
114
- describe '#disable_test_mode!' do
115
- it 'sets test_mode to false' do
116
- client.disable_test_mode!
117
- expect(client.test_mode?).to be(false)
118
- end
119
- end
120
-
121
- # ---------------------------------------------------------------------------
122
- # .deliveries
123
- # ---------------------------------------------------------------------------
124
- describe '.deliveries' do
125
- before { described_class.deliveries.clear }
126
-
127
- it 'returns an Array' do
128
- expect(described_class.deliveries).to be_an(Array)
129
- end
130
-
131
- it 'accumulates messages sent in test mode' do
132
- client.send_message(domain, message_params)
133
- expect(described_class.deliveries).not_to be_empty
134
- end
135
-
136
- it 'stores the message data sent in test mode' do
137
- client.send_message(domain, message_params)
138
- expect(described_class.deliveries.last).to include(from: 'bob@example.com')
139
- end
140
-
141
- it 'accumulates multiple deliveries' do
142
- 2.times { client.send_message(domain, message_params) }
143
- expect(described_class.deliveries.size).to eq(2)
144
- end
145
- end
146
-
147
- # ---------------------------------------------------------------------------
148
- # #send_message — test mode
149
- # ---------------------------------------------------------------------------
150
- describe '#send_message (test mode)' do
151
- before { described_class.deliveries.clear }
152
-
153
- it 'returns a Mailgun::Response' do
154
- result = client.send_message(domain, message_params)
155
- expect(result).to be_a(Mailgun::Response)
156
- end
157
-
158
- it 'returns a 200 status response in test mode' do
159
- result = client.send_message(domain, message_params)
160
- expect(result.code).to eq(200)
161
- end
162
-
163
- it 'returns a body containing the Queued message in test mode' do
164
- result = client.send_message(domain, message_params)
165
- body = JSON.parse(result.body)
166
- expect(body['message']).to eq('Queued. Thank you.')
167
- end
168
-
169
- it 'returns a body with a unique test-mode message id' do
170
- result = client.send_message(domain, message_params)
171
- body = JSON.parse(result.body)
172
- expect(body['id']).to match(/test-mode-mail-.+@localhost/)
173
- end
174
-
175
- it 'does not make real HTTP calls in test mode' do
176
- expect(client.instance_variable_get(:@http_client)).not_to receive(:post)
177
- client.send_message(domain, message_params)
178
- end
179
-
180
- it 'records a copy of message data in .deliveries' do
181
- client.send_message(domain, message_params)
182
- expect(described_class.deliveries.last[:subject]).to eq('Hello!')
183
- end
184
-
185
- context 'with a MessageBuilder object' do
186
- it 'stores the MessageBuilder in .deliveries' do
187
- mb = Mailgun::MessageBuilder.new
188
- mb.set_from_address('bob@example.com')
189
- mb.add_recipient(:to, 'sally@example.com')
190
- mb.set_subject('Hello via builder!')
191
- mb.set_text_body('Body text.')
192
-
193
- result = client.send_message(domain, mb)
194
- expect(result).to be_a(Mailgun::Response)
195
- expect(described_class.deliveries.last).to be_a(Mailgun::MessageBuilder)
196
- end
197
-
198
- it 'raises a Mailgun::ParameterError when mb is empty' do
199
- mb = Mailgun::MessageBuilder.new
200
-
201
- expect { client.send_message(domain, mb) }
202
- .to raise_error(Mailgun::ParameterError)
203
- end
204
-
205
- it 'raises a Mailgun::ParameterError when from is missing' do
206
- mb = Mailgun::MessageBuilder.new
207
- mb.add_recipient(:to, 'sally@example.com')
208
-
209
- expect { client.send_message(domain, mb) }
210
- .to raise_error(Mailgun::ParameterError)
211
- end
212
- end
213
- end
214
-
215
- # ---------------------------------------------------------------------------
216
- # #send_message — live mode (HTTP mocked at the Faraday level)
217
- # ---------------------------------------------------------------------------
218
- describe '#send_message (live mode)' do
219
- subject(:live_client) do
220
- described_class.new(api_key, 'api.mailgun.net', 'v3', true, false)
221
- end
222
-
223
- let(:success_response) { fake_http_response }
224
-
225
- before do
226
- allow(live_client.instance_variable_get(:@http_client))
227
- .to receive(:post)
228
- .and_return(success_response)
229
- end
230
-
231
- it 'posts to the messages endpoint and returns a Response' do
232
- result = live_client.send_message(domain, message_params)
233
- expect(result).to be_a(Mailgun::Response)
234
- end
235
-
236
- it 'strips nil values from the data hash before posting' do
237
- params_with_nil = message_params.merge(cc: nil)
238
- http = live_client.instance_variable_get(:@http_client)
239
- expect(http).to receive(:post) do |_path, data, *|
240
- expect(data).not_to have_key(:cc)
241
- success_response
242
- end
243
- live_client.send_message(domain, params_with_nil)
244
- end
245
-
246
- it 'raises CommunicationError when the underlying request fails' do
247
- allow(live_client.instance_variable_get(:@http_client))
248
- .to receive(:post)
249
- .and_raise(StandardError, 'connection refused')
250
-
251
- expect { live_client.send_message(domain, message_params) }
252
- .to raise_error(Mailgun::CommunicationError)
253
- end
254
-
255
- context 'with a MessageBuilder object' do
256
- it 'posts to the messages endpoint and returns a Response' do
257
- mb = Mailgun::MessageBuilder.new
258
- mb.set_from_address('bob@example.com')
259
- mb.add_recipient(:to, 'sally@example.com')
260
- mb.set_subject('Hello via builder!')
261
- mb.set_text_body('Body text.')
262
-
263
- result = live_client.send_message(domain, mb)
264
- expect(result).to be_a(Mailgun::Response)
265
- end
266
- end
267
- end
268
-
269
- # ---------------------------------------------------------------------------
270
- # #get
271
- # ---------------------------------------------------------------------------
272
- describe '#get' do
273
- let(:http_response) { fake_http_response }
274
-
275
- before do
276
- allow(client.instance_variable_get(:@http_client))
277
- .to receive(:get)
278
- .and_return(http_response)
279
- end
280
-
281
- it 'returns a Mailgun::Response' do
282
- result = client.get("#{domain}/events", { event: 'delivered' })
283
- expect(result).to be_a(Mailgun::Response)
284
- end
285
-
286
- it 'passes the resource path through to the http client' do
287
- http = client.instance_variable_get(:@http_client)
288
- expect(http).to receive(:get).with("#{domain}/events", anything, anything)
289
- .and_return(http_response)
290
- client.get("#{domain}/events")
291
- end
292
-
293
- it 'raises CommunicationError on failure' do
294
- allow(client.instance_variable_get(:@http_client))
295
- .to receive(:get)
296
- .and_raise(StandardError, 'timeout')
297
-
298
- expect { client.get("#{domain}/events") }
299
- .to raise_error(Mailgun::CommunicationError)
300
- end
301
- end
302
-
303
- # ---------------------------------------------------------------------------
304
- # #post
305
- # ---------------------------------------------------------------------------
306
- describe '#post' do
307
- let(:http_response) { fake_http_response }
308
-
309
- before do
310
- allow(client.instance_variable_get(:@http_client))
311
- .to receive(:post)
312
- .and_return(http_response)
313
- end
314
-
315
- it 'returns a Mailgun::Response' do
316
- result = client.post("#{domain}/messages", message_params)
317
- expect(result).to be_a(Mailgun::Response)
318
- end
319
-
320
- it 'raises CommunicationError on failure' do
321
- allow(client.instance_variable_get(:@http_client))
322
- .to receive(:post)
323
- .and_raise(StandardError, 'connection error')
324
-
325
- expect { client.post("#{domain}/messages", message_params) }
326
- .to raise_error(Mailgun::CommunicationError)
327
- end
328
- end
329
-
330
- # ---------------------------------------------------------------------------
331
- # #put
332
- # ---------------------------------------------------------------------------
333
- describe '#put' do
334
- let(:http_response) { fake_http_response }
335
-
336
- before do
337
- allow(client.instance_variable_get(:@http_client))
338
- .to receive(:put)
339
- .and_return(http_response)
340
- end
341
-
342
- it 'returns a Mailgun::Response' do
343
- result = client.put("#{domain}/routes/abc123", { description: 'updated' })
344
- expect(result).to be_a(Mailgun::Response)
345
- end
346
-
347
- it 'raises CommunicationError on failure' do
348
- allow(client.instance_variable_get(:@http_client))
349
- .to receive(:put)
350
- .and_raise(StandardError, 'connection error')
351
-
352
- expect { client.put("#{domain}/routes/abc123", {}) }
353
- .to raise_error(Mailgun::CommunicationError)
354
- end
355
- end
356
-
357
- # ---------------------------------------------------------------------------
358
- # #delete
359
- # ---------------------------------------------------------------------------
360
- describe '#delete' do
361
- let(:http_response) { fake_http_response(body: '{"message":"Bounced address has been removed"}') }
362
-
363
- before do
364
- allow(client.instance_variable_get(:@http_client))
365
- .to receive(:delete)
366
- .and_return(http_response)
367
- end
368
-
369
- it 'returns a Mailgun::Response' do
370
- result = client.delete("#{domain}/bounces/test@example.com")
371
- expect(result).to be_a(Mailgun::Response)
372
- end
373
-
374
- it 'raises CommunicationError on failure' do
375
- allow(client.instance_variable_get(:@http_client))
376
- .to receive(:delete)
377
- .and_raise(StandardError, 'connection error')
378
-
379
- expect { client.delete("#{domain}/bounces/test@example.com") }
380
- .to raise_error(Mailgun::CommunicationError)
381
- end
382
-
383
- it 'sends params in the request' do
384
- result = client.delete("#{domain}/bounces/test@example.com", params: 'test')
385
- expect(result).to be_a(Mailgun::Response)
386
- end
387
-
388
- it 'sends params in the request when body_params truthy' do
389
- result = client.delete("#{domain}/bounces/test@example.com", params: 'test', body_params: true)
390
- expect(result).to be_a(Mailgun::Response)
391
- end
392
- end
393
-
394
- # ---------------------------------------------------------------------------
395
- # #set_api_key
396
- # ---------------------------------------------------------------------------
397
- describe '#set_api_key' do
398
- it 'does not raise when updating the api key' do
399
- expect { client.set_api_key('new-key-xyz') }.not_to raise_error
400
- end
401
- end
402
-
403
- # ---------------------------------------------------------------------------
404
- # #set_subaccount / #reset_subaccount
405
- # ---------------------------------------------------------------------------
406
- describe '#set_subaccount' do
407
- it 'adds the subaccount header to the http client' do
408
- client.set_subaccount('subaccount-id-123')
409
- headers = client.instance_variable_get(:@http_client).headers
410
- expect(headers[Mailgun::Client::SUBACCOUNT_HEADER]).to eq('subaccount-id-123')
411
- end
412
- end
413
-
414
- describe '#reset_subaccount' do
415
- it 'removes the subaccount header from the http client' do
416
- client.set_subaccount('subaccount-id-123')
417
- client.reset_subaccount
418
- headers = client.instance_variable_get(:@http_client).headers
419
- expect(headers[Mailgun::Client::SUBACCOUNT_HEADER]).to be_nil
420
- end
421
- end
422
-
423
- # ---------------------------------------------------------------------------
424
- # #suppressions
425
- # ---------------------------------------------------------------------------
426
- describe '#suppressions' do
427
- it 'returns a Mailgun::Suppressions instance' do
428
- expect(client.suppressions(domain)).to be_a(Mailgun::Suppressions)
429
- end
430
-
431
- it 'scopes the suppressions client to the given domain' do
432
- suppressions = client.suppressions(domain)
433
- expect(suppressions).to be_a(Mailgun::Suppressions)
434
- end
435
- end
436
-
437
- # ---------------------------------------------------------------------------
438
- # Mailgun.configure integration
439
- # ---------------------------------------------------------------------------
440
- describe 'Mailgun.configure integration' do
441
- after { Mailgun.instance_variable_set(:@configuration, nil) }
442
-
443
- it 'picks up the api_key from Mailgun.configure' do
444
- Mailgun.configure { |config| config.api_key = 'configured-key' }
445
- expect { described_class.new }.not_to raise_error
446
- end
447
- end
448
- 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