mailgun-ruby 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +8 -0
  4. data/.rubocop_todo.yml +22 -0
  5. data/.ruby-env.yml.example +12 -0
  6. data/.travis.yml +6 -12
  7. data/Domains.md +36 -0
  8. data/MessageBuilder.md +14 -14
  9. data/Messages.md +44 -30
  10. data/OptInHandler.md +34 -34
  11. data/README.md +74 -24
  12. data/Rakefile +22 -20
  13. data/Snippets.md +26 -26
  14. data/Webhooks.md +40 -0
  15. data/lib/mailgun.rb +26 -228
  16. data/lib/mailgun/chains.rb +16 -0
  17. data/lib/mailgun/client.rb +143 -0
  18. data/lib/mailgun/domains/domains.rb +84 -0
  19. data/lib/mailgun/events/events.rb +53 -35
  20. data/lib/mailgun/exceptions/exceptions.rb +43 -10
  21. data/lib/mailgun/lists/opt_in_handler.rb +18 -19
  22. data/lib/mailgun/messages/batch_message.rb +31 -48
  23. data/lib/mailgun/messages/message_builder.rb +160 -144
  24. data/lib/mailgun/response.rb +55 -0
  25. data/lib/mailgun/version.rb +2 -3
  26. data/lib/mailgun/webhooks/webhooks.rb +101 -0
  27. data/mailgun.gemspec +16 -10
  28. data/spec/integration/bounces_spec.rb +44 -0
  29. data/spec/integration/campaign_spec.rb +60 -0
  30. data/spec/integration/complaints_spec.rb +38 -0
  31. data/spec/integration/domains_spec.rb +39 -0
  32. data/spec/integration/email_validation_spec.rb +29 -0
  33. data/spec/integration/events_spec.rb +20 -0
  34. data/spec/integration/list_members_spec.rb +63 -0
  35. data/spec/integration/list_spec.rb +58 -0
  36. data/spec/integration/mailgun_spec.rb +26 -550
  37. data/spec/integration/routes_spec.rb +74 -0
  38. data/spec/integration/stats_spec.rb +15 -0
  39. data/spec/integration/unsubscribes_spec.rb +42 -0
  40. data/spec/integration/webhook_spec.rb +54 -0
  41. data/spec/spec_helper.rb +37 -7
  42. data/spec/unit/connection/test_client.rb +15 -95
  43. data/spec/unit/events/events_spec.rb +9 -6
  44. data/spec/unit/lists/opt_in_handler_spec.rb +6 -4
  45. data/spec/unit/mailgun_spec.rb +25 -19
  46. data/spec/unit/messages/batch_message_spec.rb +47 -38
  47. data/spec/unit/messages/message_builder_spec.rb +282 -111
  48. data/vcr_cassettes/bounces.yml +175 -0
  49. data/vcr_cassettes/complaints.yml +175 -0
  50. data/vcr_cassettes/domains.todo.yml +42 -0
  51. data/vcr_cassettes/domains.yml +360 -0
  52. data/vcr_cassettes/email_validation.yml +104 -0
  53. data/vcr_cassettes/events.yml +61 -0
  54. data/vcr_cassettes/list_members.yml +320 -0
  55. data/vcr_cassettes/mailing_list.todo.yml +43 -0
  56. data/vcr_cassettes/mailing_list.yml +390 -0
  57. data/vcr_cassettes/routes.yml +359 -0
  58. data/vcr_cassettes/send_message.yml +107 -0
  59. data/vcr_cassettes/stats.yml +44 -0
  60. data/vcr_cassettes/unsubscribes.yml +191 -0
  61. data/vcr_cassettes/webhooks.yml +276 -0
  62. metadata +114 -10
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+ require 'mailgun'
3
+
4
+ vcr_opts = { :cassette_name => "list_members" }
5
+
6
+ describe 'For the Mailing Lists Members endpoint', order: :defined, vcr: vcr_opts do
7
+ before(:all) do
8
+ @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
9
+ @domain = TESTDOMAIN
10
+ @ml_address = "integration_test_list@#{@domain}"
11
+ @ml_member = "integration_test_member_member@#{@domain}"
12
+ end
13
+
14
+ it 'creates a list' do
15
+ result = @mg_obj.post("lists", {:address => @ml_address,
16
+ :name => 'Integration Test List',
17
+ :description => 'This list should be deleted automatically.',
18
+ :access_level => 'members'})
19
+ result.to_h!
20
+ expect(result.body["message"]).to eq("Mailing list has been created")
21
+ expect(result.body["list"]["address"]).to eq(@ml_address)
22
+ expect(result.body["list"]["name"]).to eq('Integration Test List')
23
+ end
24
+
25
+ it 'adds a list member' do
26
+ result = @mg_obj.post("lists/#{@ml_address}/members",
27
+ {:address => @ml_member,
28
+ :name => 'Jane Doe',
29
+ :subscribed => true,
30
+ :upsert => 'no'})
31
+
32
+ result.to_h!
33
+ expect(result.body["message"]).to eq("Mailing list member has been created")
34
+ expect(result.body["member"]["address"]).to eq(@ml_member)
35
+ expect(result.body["member"]["name"]).to eq('Jane Doe')
36
+ end
37
+
38
+ it 'gets a list member.' do
39
+ result = @mg_obj.get("lists/#{@ml_address}/members/#{@ml_member}")
40
+
41
+ result.to_h!
42
+ expect(result.body["member"]["address"]).to eq(@ml_member)
43
+ expect(result.body["member"]["name"]).to eq('Jane Doe')
44
+ end
45
+
46
+ it 'updates a list member.' do
47
+ result = @mg_obj.put("lists/#{@ml_address}/members/#{@ml_member}",
48
+ {:name => 'Jane Doe Update',
49
+ :subscribed => false})
50
+
51
+ result.to_h!
52
+ expect(result.body["message"]).to eq("Mailing list member has been updated")
53
+ expect(result.body["member"]["address"]).to eq(@ml_member)
54
+ expect(result.body["member"]["name"]).to eq('Jane Doe Update')
55
+ expect(result.body["member"]["subscribed"]).to eq(false)
56
+ end
57
+
58
+ it 'removes a list member' do
59
+ @mg_obj.delete("lists/#{@ml_address}/members/#{@ml_member}")
60
+ @mg_obj.delete("lists/#{@ml_address}")
61
+ end
62
+
63
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'mailgun'
3
+
4
+ vcr_opts = { :cassette_name => "mailing_list" }
5
+
6
+ describe 'For the Mailing Lists endpoint', vcr: vcr_opts do
7
+ before(:all) do
8
+ @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
9
+ @domain = TESTDOMAIN
10
+ @ml_address = "integration_test_list@#{@domain}"
11
+ end
12
+
13
+ it 'creates a list' do
14
+ result = @mg_obj.post("lists", {:address => @ml_address,
15
+ :name => 'Integration Test List',
16
+ :description => 'This list should be deleted automatically.',
17
+ :access_level => 'members'})
18
+
19
+ result.to_h!
20
+ expect(result.body["message"]).to eq("Mailing list has been created")
21
+ expect(result.body["list"]["address"]).to eq(@ml_address)
22
+ expect(result.body["list"]["name"]).to eq('Integration Test List')
23
+ end
24
+
25
+ it 'gets a list.' do
26
+ result = @mg_obj.get("lists/#{@ml_address}")
27
+
28
+ result.to_h!
29
+ expect(result.body["list"]["address"]).to eq(@ml_address)
30
+ expect(result.body["list"]["name"]).to eq('Integration Test List')
31
+ end
32
+
33
+ it 'gets a list of all lists.' do
34
+ result = @mg_obj.get("lists", {:limit => 50})
35
+
36
+ result.to_h!
37
+ expect(result.body["total_count"]).to be > 0
38
+ end
39
+
40
+ it 'updates a list.' do
41
+ result = @mg_obj.put("lists/#{@ml_address}",
42
+ {:address => @ml_address,
43
+ :name => 'Integration Test List Update',
44
+ :description => 'This list should be deleted automatically.',
45
+ :access_level => 'readonly'})
46
+
47
+ result.to_h!
48
+ expect(result.body["message"]).to eq("Mailing list has been updated")
49
+ expect(result.body["list"]["address"]).to eq(@ml_address)
50
+ expect(result.body["list"]["name"]).to eq('Integration Test List Update')
51
+ expect(result.body["list"]["access_level"]).to eq('readonly')
52
+ end
53
+
54
+ it 'deletes a list' do
55
+ @mg_obj.delete("lists/#{@ml_address}")
56
+ end
57
+
58
+ end
@@ -1,44 +1,47 @@
1
1
  require 'spec_helper'
2
2
  require 'mailgun'
3
3
 
4
- describe 'Mailgun instantiation' do
4
+ vcr_opts = { :cassette_name => "instance" }
5
+
6
+ describe 'Mailgun instantiation', vcr: vcr_opts do
5
7
  it 'instantiates an HttpClient object' do
6
8
  expect {@mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)}.not_to raise_error
7
9
  end
8
10
  end
9
11
 
10
- describe 'The method send_message()' do
12
+ vcr_opts = { :cassette_name => "send_message" }
13
+
14
+ describe 'The method send_message()', vcr: vcr_opts do
11
15
  before(:all) do
12
16
  @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
13
- random_number = rand(1 .. 5000000)
14
- @domain = "integration-test-#{random_number}.example.com"
15
- @mg_obj.post("domains", {:name => @domain})
17
+ @domain = TESTDOMAIN
16
18
  end
17
-
19
+
18
20
  it 'sends a standard message in test mode.' do
19
- result = @mg_obj.send_message(@domain, {:from => 'bob@domain.com',
20
- :to => 'sally@example.com',
21
- :subject => 'Hash Integration Test',
22
- :text => 'INTEGRATION TESTING',
23
- 'o:testmode' => true})
21
+ result = @mg_obj.send_message(@domain, {:from => "bob@#{@domain}",
22
+ :to => "sally@#{@domain}",
23
+ :subject => 'Hash Integration Test',
24
+ :text => 'INTEGRATION TESTING',
25
+ 'o:testmode' => true}
26
+ )
24
27
  result.to_h!
25
- result.body.should include("message")
26
- result.body.should include("id")
28
+ expect(result.body).to include("message")
29
+ expect(result.body).to include("id")
27
30
  end
28
31
 
29
32
  it 'sends a message builder message in test mode.' do
30
33
  mb_obj = Mailgun::MessageBuilder.new()
31
- mb_obj.set_from_address("sender@example.com", {'first' => 'Sending', 'last' => 'User'})
32
- mb_obj.add_recipient(:to, "recipient@example.com", {'first' => 'Recipient', 'last' => 'User'})
33
- mb_obj.set_subject("Message Builder Integration Test")
34
- mb_obj.set_text_body("This is the text body.")
35
- mb_obj.set_test_mode(true)
34
+ mb_obj.from("sender@#{@domain}", {'first' => 'Sending', 'last' => 'User'})
35
+ mb_obj.add_recipient(:to, "recipient@#{@domain}", {'first' => 'Recipient', 'last' => 'User'})
36
+ mb_obj.subject("Message Builder Integration Test")
37
+ mb_obj.body_text("This is the text body.")
38
+ mb_obj.test_mode(true)
36
39
 
37
40
  result = @mg_obj.send_message(@domain, mb_obj)
38
41
 
39
42
  result.to_h!
40
- result.body.should include("message")
41
- result.body.should include("id")
43
+ expect(result.body).to include("message")
44
+ expect(result.body).to include("id")
42
45
  end
43
46
 
44
47
  it 'sends a custom MIME message in test mode.' do
@@ -58,541 +61,14 @@ Sender: me@samples.mailgun.org
58
61
 
59
62
  Testing some Mailgun awesomness!'
60
63
 
61
- message_params = {:to => 'sally@example.com',
64
+ message_params = {:to => "sally@#{@domain}",
62
65
  :message => mime_string}
63
66
 
64
67
  result = @mg_obj.send_message(@domain, message_params)
65
68
 
66
69
  result.to_h!
67
- result.body.should include("message")
68
- result.body.should include("id")
69
- end
70
-
71
- after(:all) do
72
- @mg_obj.delete("domains/#{@domain}")
73
- end
74
- end
75
-
76
- describe 'For the domains endpoint' do
77
- before(:all) do
78
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
79
- random_number = rand(1 .. 5000000)
80
- @domain = "integration-test-#{random_number}.example.com"
81
- @result = @mg_obj.post("domains", {:name => @domain,
82
- :smtp_password => 'super_secret',
83
- :spam_action => 'tag'})
84
-
85
- @result.to_h!
86
- @result.body.should include("domain")
87
- expect(@result.body["domain"]["name"]).to eq(@domain)
88
- expect(@result.body["domain"]["spam_action"]).to eq("tag")
89
- expect(@result.body["domain"]["smtp_password"]).to eq("super_secret")
90
- end
91
-
92
- it 'get the domain.' do
93
- result = @mg_obj.get("domains/#{@domain}")
94
-
95
- result.to_h!
96
- result.body.should include("domain")
97
- result.body["domain"]["name"].should eq(@domain)
98
- end
99
-
100
- it 'gets a list of domains.' do
101
- result = @mg_obj.get("domains")
102
-
103
- result.to_h!
104
- expect(result.body["total_count"]).to be > 0
105
- end
106
-
107
- it 'deletes a domain.' do
108
- result = @mg_obj.delete("domains/#{@domain}")
109
-
110
- result.to_h!
111
- expect(result.body["message"]).to eq("Domain has been deleted")
112
- end
113
-
114
- after(:all) do
115
- @mg_obj.delete("domains/#{@domain}")
116
- end
117
- end
118
-
119
- describe 'For the Unsubscribes endpoint' do
120
- before(:all) do
121
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
122
- random_number = rand(1 .. 5000000)
123
- @domain = "integration-test-#{random_number}.example.com"
124
- @mg_obj.post("domains", {:name => @domain})
125
- end
126
-
127
- before(:each) do
128
- random_number = rand(1 .. 5000000)
129
- @email = "integration-test-#{random_number}@example.com"
130
- @result = @mg_obj.post("#{@domain}/unsubscribes",
131
- {:address => @email,
132
- :tag => '*'})
133
-
134
- @result.to_h!
135
- expect(@result.body["message"]).to eq("Address has been added to the unsubscribes table")
136
- expect(@result.body["address"]).to eq(@email)
137
- end
138
-
139
- it 'get an unsubscribee.' do
140
- result = @mg_obj.get("#{@domain}/unsubscribes/#{@email}")
141
-
142
- result.to_h!
143
- expect(result.body["total_count"]).to eq 1
144
- expect(result.body["items"][0]["address"]).to eq(@email)
145
- end
146
-
147
- it 'gets a list of unsubscribes.' do
148
- result = @mg_obj.get("#{@domain}/unsubscribes")
149
-
150
- result.to_h!
151
- expect(result.body["total_count"]).to be > 0
152
- end
153
-
154
- after(:each) do
155
- @mg_obj.delete("#{@domain}/unsubscribes/#{@email}")
156
- end
157
-
158
- after(:all) do
159
- @mg_obj.delete("domains/#{@domain}")
160
- end
161
- end
162
-
163
- describe 'For the Complaints endpoint' do
164
- before(:all) do
165
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
166
- random_number = rand(1 .. 5000000)
167
- @domain = "integration-test-#{random_number}.example.com"
168
- @mg_obj.post("domains", {:name => @domain})
169
- end
170
-
171
- before(:each) do
172
- random_number = rand(1 .. 5000000)
173
- @email = "integration-test-#{random_number}@example.com"
174
- @result = @mg_obj.post("#{@domain}/complaints", {:address => @email})
175
-
176
- @result.to_h!
177
- expect(@result.body["message"]).to eq("Address has been added to the complaints table")
178
- expect(@result.body["address"]).to eq(@email)
179
- end
180
-
181
- it 'get a complaint.' do
182
- result = @mg_obj.get("#{@domain}/complaints/#{@email}")
183
-
184
- result.to_h!
185
- expect(result.body["complaint"]["count"]).to eq 1
186
- expect(result.body["complaint"]["address"]).to eq(@email)
187
- end
188
-
189
- it 'gets a list of complaints.' do
190
- result = @mg_obj.get("#{@domain}/complaints")
191
-
192
- result.to_h!
193
- expect(result.body["total_count"]).to be > 0
194
- end
195
-
196
- after(:each) do
197
- @mg_obj.delete("#{@domain}/complaints/#{@email}")
198
- end
199
-
200
- after(:all) do
201
- @mg_obj.delete("domains/#{@domain}")
202
- end
203
- end
204
-
205
- describe 'For the Bounces endpoint' do
206
- before(:all) do
207
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
208
- random_number = rand(1 .. 5000000)
209
- @domain = "integration-test-#{random_number}.example.com"
210
- @mg_obj.post("domains", {:name => @domain})
211
- end
212
-
213
- before(:each) do
214
- random_number = rand(1 .. 5000000)
215
- @email = "integration-test-#{random_number}@example.com"
216
- @result = @mg_obj.post("#{@domain}/bounces",
217
- {:address => @email,
218
- :code => 550,
219
- :error => "Integration Test"})
220
-
221
- @result.to_h!
222
- expect(@result.body["message"]).to eq("Address has been added to the bounces table")
223
- expect(@result.body["address"]).to eq(@email)
224
- end
225
-
226
- it 'get a bounce.' do
227
- result = @mg_obj.get("#{@domain}/bounces/#{@email}")
228
-
229
- result.to_h!
230
- expect(result.body["bounce"]["code"]).to eq("550")
231
- expect(result.body["bounce"]["address"]).to eq(@email)
232
- expect(result.body["bounce"]["error"]).to eq("Integration Test")
233
- end
234
-
235
- it 'gets a list of bounces.' do
236
- result = @mg_obj.get("#{@domain}/bounces")
237
-
238
- result.to_h!
239
- expect(result.body["total_count"]).to be > 0
240
- end
241
-
242
- after(:each) do
243
- @mg_obj.delete("#{@domain}/bounces/#{@email}")
244
- end
245
-
246
- after(:all) do
247
- @mg_obj.delete("domains/#{@domain}")
248
- end
249
- end
250
-
251
- describe 'For the Stats endpoint' do
252
- before(:all) do
253
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
254
- random_number = rand(1 .. 5000000)
255
- @domain = "integration-test-#{random_number}.example.com"
256
- @mg_obj.post("domains", {:name => @domain})
257
- end
258
-
259
- it 'get some stats.' do
260
- @mg_obj.get("#{@domain}/stats", {:limit => 50, :skip => 10, :event => 'sent'})
261
- end
262
-
263
- after(:all) do
264
- @mg_obj.delete("domains/#{@domain}")
265
- end
266
- end
267
-
268
- describe 'For the Events endpoint' do
269
- before(:all) do
270
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
271
- random_number = rand(1 .. 5000000)
272
- @domain = "integration-test-#{random_number}.example.com"
273
- @mg_obj.post("domains", {:name => @domain})
274
- end
275
-
276
- it 'get an event.' do
277
- result = @mg_obj.get("#{@domain}/events", {:limit => 1})
278
-
279
- result.to_h!
280
- expect(result.body["items"].length).to be_within(1).of(1)
281
- expect(result.body["paging"]).to include("next")
282
- expect(result.body["paging"]).to include("previous")
283
- end
284
-
285
- after(:all) do
286
- @mg_obj.delete("domains/#{@domain}")
70
+ expect(result.body).to include("message")
71
+ expect(result.body).to include("id")
287
72
  end
288
73
  end
289
74
 
290
- describe 'For the Routes endpoint' do
291
- before(:all) do
292
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
293
- end
294
-
295
- before(:each) do
296
- result = @mg_obj.post("routes", {:priority => 10,
297
- :description => 'Integration Test Route',
298
- :expression => 'match_recipient(".*@example.com")',
299
- :action => 'forward("alice@example.com")'})
300
-
301
- result.to_h!
302
- expect(result.body["message"]).to eq("Route has been created")
303
- expect(result.body["route"]["description"]).to eq("Integration Test Route")
304
- expect(result.body["route"]["actions"]).to include('forward("alice@example.com")')
305
- expect(result.body["route"]["expression"]).to include('match_recipient(".*@example.com")')
306
- expect(result.body["route"]["priority"]).to eq(10)
307
-
308
- @route_id = result.body["route"]["id"]
309
- end
310
-
311
- it 'get the route.' do
312
- result = @mg_obj.get("routes/#{@route_id}")
313
-
314
- result.to_h!
315
- expect(result.body["route"]["description"]).to eq("Integration Test Route")
316
- expect(result.body["route"]["actions"]).to include('forward("alice@example.com")')
317
- expect(result.body["route"]["expression"]).to include('match_recipient(".*@example.com")')
318
- expect(result.body["route"]["priority"]).to eq(10)
319
- end
320
-
321
- it 'gets a list of all routes.' do
322
- result = @mg_obj.get("routes", {:limit => 50})
323
-
324
- result.to_h!
325
- expect(result.body["total_count"]).to be > 0
326
- end
327
-
328
- it 'updates the route.' do
329
- result = @mg_obj.put("routes/#{@route_id}", {:priority => 10,
330
- :description => 'Integration Test Route Update',
331
- :expression => 'match_recipient(".*@example.com")',
332
- :action => 'forward("update@example.com")'})
333
-
334
- result.to_h!
335
- expect(result.body["message"]).to eq("Route has been updated")
336
- expect(result.body["description"]).to eq("Integration Test Route Update")
337
- expect(result.body["actions"]).to include('forward("update@example.com")')
338
- expect(result.body["expression"]).to include('match_recipient(".*@example.com")')
339
- expect(result.body["priority"]).to eq(10)
340
- end
341
-
342
- after(:each) do
343
- @mg_obj.delete("routes/#{@route_id}")
344
- end
345
- end
346
-
347
- describe 'For the campaigns endpoint' do
348
- before(:all) do
349
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
350
- random_number = rand(1 .. 5000000)
351
- @domain = "integration-test-#{random_number}.example.com"
352
- @mg_obj.post("domains", {:name => @domain})
353
- end
354
-
355
- before(:each) do
356
- random_number = rand(1 .. 5000000)
357
- @campaign_id = "integration_test_#{random_number}"
358
- result = @mg_obj.post("#{@domain}/campaigns", {:name => 'My Campaign',
359
- :id => @campaign_id})
360
-
361
- result.to_h!
362
- expect(result.body["message"]).to eq("Campaign created")
363
- expect(result.body["campaign"]["id"]).to eq(@campaign_id)
364
- expect(result.body["campaign"]["name"]).to eq('My Campaign')
365
- end
366
-
367
- it 'get a campaign.' do
368
- result = @mg_obj.get("#{@domain}/campaigns/#{@campaign_id}")
369
-
370
- result.to_h!
371
- expect(result.body["id"]).to eq(@campaign_id)
372
- expect(result.body["name"]).to eq('My Campaign')
373
- end
374
-
375
- it 'gets a list of all campaigns.' do
376
- result = @mg_obj.get("#{@domain}/campaigns", {:limit => 50})
377
-
378
- result.to_h!
379
- expect(result.body["total_count"]).to be > 0
380
- end
381
-
382
- it 'update a campaign.' do
383
- result = @mg_obj.put("#{@domain}/campaigns/#{@campaign_id}", {:name => 'My Updated Campaign',
384
- :id => @campaign_id})
385
-
386
- result.to_h!
387
- expect(result.body["message"]).to eq("Campaign updated")
388
- expect(result.body["campaign"]["id"]).to eq(@campaign_id)
389
- expect(result.body["campaign"]["name"]).to eq('My Updated Campaign')
390
- end
391
-
392
- it 'get campaign events.' do
393
- expect{@mg_obj.get("#{@domain}/campaigns/#{@campaign_id}/events", {:groupby => "clicked"})}.not_to raise_error
394
- end
395
-
396
- it 'get campaign stats.' do
397
- expect{@mg_obj.get("#{@domain}/campaigns/#{@campaign_id}/stats", {:groupby => "domain"})}.not_to raise_error
398
- end
399
-
400
- after(:each) do
401
- @mg_obj.delete("#{@domain}/campaigns/#{@campaign_id}")
402
- end
403
- after(:all) do
404
- @mg_obj.delete("domains/#{@domain}")
405
- end
406
- end
407
-
408
- describe 'For the webhooks endpoint' do
409
- before(:all) do
410
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
411
- random_number = rand(1 .. 5000000)
412
- @domain = "integration-test-#{random_number}.example.com"
413
- @mg_obj.post("domains", {:name => @domain})
414
- end
415
-
416
- before(:each) do
417
- random_number = rand(1 .. 5000000)
418
- @campaign_id = "integration_test_#{random_number}"
419
- result = @mg_obj.post("domains/#{@domain}/webhooks", {:id => 'bounce',
420
- :url => 'http://example.com/mailgun/events/bounce'})
421
-
422
- result.to_h!
423
- expect(result.body["message"]).to eq("Webhook has been created")
424
- expect(result.body["webhook"]["url"]).to eq('http://example.com/mailgun/events/bounce')
425
- end
426
-
427
- it 'get a webhook.' do
428
- result = @mg_obj.get("domains/#{@domain}/webhooks/bounce")
429
-
430
- result.to_h!
431
- expect(result.body["webhook"]["url"]).to eq('http://example.com/mailgun/events/bounce')
432
- end
433
-
434
- it 'gets a list of all webhooks.' do
435
- result = @mg_obj.get("domains/#{@domain}/webhooks")
436
-
437
- result.to_h!
438
- expect(result.body["webhooks"]["bounce"]["url"]).to eq('http://example.com/mailgun/events/bounce')
439
- end
440
-
441
- it 'update a webhook.' do
442
- result = @mg_obj.put("domains/#{@domain}/webhooks/bounce", {:id => 'bounce',
443
- :url => 'http://example.com/mailgun/events/new_bounce'})
444
-
445
- result.to_h!
446
- expect(result.body["message"]).to eq("Webhook has been updated")
447
- expect(result.body["webhook"]["url"]).to eq('http://example.com/mailgun/events/new_bounce')
448
- end
449
-
450
- after(:each) do
451
- @mg_obj.delete("domains/#{@domain}/webhooks/bounce")
452
- end
453
-
454
- after(:all) do
455
- @mg_obj.delete("domains/#{@domain}")
456
- end
457
- end
458
-
459
- describe 'For the Mailing Lists endpoint' do
460
- before(:all) do
461
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
462
- random_number = rand(1 .. 5000000)
463
- @domain = "integration-test-#{random_number}.example.com"
464
- @mg_obj.post("domains", {:name => @domain})
465
- end
466
-
467
- before(:each) do
468
- random_number = rand(1 .. 5000000)
469
- @ml_address = "integration_test_#{random_number}@#{@domain}"
470
- result = @mg_obj.post("lists", {:address => @ml_address,
471
- :name => 'Integration Test List',
472
- :description => 'This list should be deleted automatically.',
473
- :access_level => 'members'})
474
-
475
- result.to_h!
476
- expect(result.body["message"]).to eq("Mailing list has been created")
477
- expect(result.body["list"]["address"]).to eq(@ml_address)
478
- expect(result.body["list"]["name"]).to eq('Integration Test List')
479
- end
480
-
481
- it 'get a list.' do
482
- result = @mg_obj.get("lists/#{@ml_address}")
483
-
484
- result.to_h!
485
- expect(result.body["list"]["address"]).to eq(@ml_address)
486
- expect(result.body["list"]["name"]).to eq('Integration Test List')
487
- end
488
-
489
- it 'gets a list of all lists.' do
490
- result = @mg_obj.get("lists", {:limit => 50})
491
-
492
- result.to_h!
493
- expect(result.body["total_count"]).to be > 0
494
- end
495
-
496
- it 'update a list.' do
497
- result = @mg_obj.put("lists/#{@ml_address}",
498
- {:address => @ml_address,
499
- :name => 'Integration Test List Update',
500
- :description => 'This list should be deleted automatically.',
501
- :access_level => 'readonly'})
502
-
503
- result.to_h!
504
- expect(result.body["message"]).to eq("Mailing list has been updated")
505
- expect(result.body["list"]["address"]).to eq(@ml_address)
506
- expect(result.body["list"]["name"]).to eq('Integration Test List Update')
507
- expect(result.body["list"]["access_level"]).to eq('readonly')
508
- end
509
-
510
- after(:each) do
511
- @mg_obj.delete("lists/#{@ml_address}")
512
- end
513
-
514
- after(:all) do
515
- @mg_obj.delete("domains/#{@domain}")
516
- end
517
- end
518
-
519
- describe 'For the Mailing Lists Members endpoint' do
520
- before(:all) do
521
- @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
522
- random_number = rand(1 .. 5000000)
523
- @domain = "integration-test-#{random_number}.example.com"
524
- @ml_address = "integration_test_#{random_number}@#{@domain}"
525
- @ml_member = "integration_test_member_#{random_number}@#{@domain}"
526
- @mg_obj.post("domains", {:name => @domain})
527
- @mg_obj.post("lists", {:address => @ml_address,
528
- :name => 'Integration Test List',
529
- :description => 'This list should be deleted automatically.',
530
- :access_level => 'members'})
531
- end
532
-
533
- before(:each) do
534
- result = @mg_obj.post("lists/#{@ml_address}/members",
535
- {:address => @ml_member,
536
- :name => 'Jane Doe',
537
- :subscribed => true,
538
- :upsert => 'no'})
539
-
540
- result.to_h!
541
- expect(result.body["message"]).to eq("Mailing list member has been created")
542
- expect(result.body["member"]["address"]).to eq(@ml_member)
543
- expect(result.body["member"]["name"]).to eq('Jane Doe')
544
- end
545
-
546
- it 'get a list member.' do
547
- result = @mg_obj.get("lists/#{@ml_address}/members/#{@ml_member}")
548
-
549
- result.to_h!
550
- expect(result.body["member"]["address"]).to eq(@ml_member)
551
- expect(result.body["member"]["name"]).to eq('Jane Doe')
552
- end
553
-
554
- it 'updates a list member.' do
555
- result = @mg_obj.put("lists/#{@ml_address}/members/#{@ml_member}",
556
- {:name => 'Jane Doe Update',
557
- :subscribed => false})
558
-
559
- result.to_h!
560
- expect(result.body["message"]).to eq("Mailing list member has been updated")
561
- expect(result.body["member"]["address"]).to eq(@ml_member)
562
- expect(result.body["member"]["name"]).to eq('Jane Doe Update')
563
- expect(result.body["member"]["subscribed"]).to eq(false)
564
- end
565
-
566
- after(:each) do
567
- @mg_obj.delete("lists/#{@ml_address}/members/#{@ml_member}")
568
- end
569
-
570
- after(:all) do
571
- @mg_obj.delete("domains/#{@domain}")
572
- end
573
- end
574
-
575
- describe 'For the Email Validation endpoint' do
576
- before(:all) do
577
- @mg_obj = Mailgun::Client.new(PUB_APIKEY)
578
- end
579
-
580
- it 'validates an address.' do
581
- result = @mg_obj.get("address/validate",
582
- {:address => "test@example.com"})
583
-
584
- result.to_h!
585
- expect(result.body["is_valid"]).to eq(false)
586
- expect(result.body["address"]).to eq("test@example.com")
587
- end
588
-
589
- it 'parses an address.' do
590
- result = @mg_obj.get("address/parse",
591
- {:addresses => "Alice <alice@example.com>,bob@example.com,example.com"})
592
-
593
- result.to_h!
594
- expect(result.body["parsed"]).to include("Alice <alice@example.com>")
595
- expect(result.body["parsed"]).to include("bob@example.com")
596
- expect(result.body["unparseable"]).to include("example.com")
597
- end
598
- end