mailgun-ruby 1.1.2 → 1.2.4

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 (46) hide show
  1. checksums.yaml +5 -5
  2. data/.ruby-env.yml.example +1 -1
  3. data/.travis.yml +8 -5
  4. data/Gemfile +1 -1
  5. data/README.md +77 -9
  6. data/{Domains.md → docs/Domains.md} +18 -0
  7. data/{Events.md → docs/Events.md} +0 -0
  8. data/{MessageBuilder.md → docs/MessageBuilder.md} +24 -5
  9. data/{Messages.md → docs/Messages.md} +3 -3
  10. data/{OptInHandler.md → docs/OptInHandler.md} +0 -0
  11. data/{Snippets.md → docs/Snippets.md} +21 -2
  12. data/docs/Suppressions.md +82 -0
  13. data/{Webhooks.md → docs/Webhooks.md} +1 -1
  14. data/docs/railgun/Overview.md +11 -0
  15. data/docs/railgun/Parameters.md +83 -0
  16. data/lib/mailgun/address.rb +5 -2
  17. data/lib/mailgun/client.rb +39 -8
  18. data/lib/mailgun/events/events.rb +40 -12
  19. data/lib/mailgun/messages/batch_message.rb +3 -2
  20. data/lib/mailgun/messages/message_builder.rb +99 -26
  21. data/lib/mailgun/suppressions.rb +273 -0
  22. data/lib/mailgun/version.rb +1 -1
  23. data/lib/mailgun/webhooks/webhooks.rb +1 -1
  24. data/lib/mailgun-ruby.rb +2 -1
  25. data/lib/railgun/attachment.rb +56 -0
  26. data/lib/railgun/errors.rb +27 -0
  27. data/lib/railgun/mailer.rb +237 -0
  28. data/lib/railgun/message.rb +17 -0
  29. data/lib/railgun/railtie.rb +10 -0
  30. data/lib/railgun.rb +8 -0
  31. data/mailgun.gemspec +12 -12
  32. data/spec/integration/email_validation_spec.rb +14 -0
  33. data/spec/integration/events_spec.rb +9 -1
  34. data/spec/integration/mailgun_spec.rb +0 -0
  35. data/spec/integration/suppressions_spec.rb +142 -0
  36. data/spec/spec_helper.rb +3 -1
  37. data/spec/unit/events/events_spec.rb +36 -2
  38. data/spec/unit/messages/batch_message_spec.rb +1 -0
  39. data/spec/unit/messages/message_builder_spec.rb +95 -19
  40. data/spec/unit/messages/sample_data/unknown.type +0 -0
  41. data/spec/unit/railgun/content_type_spec.rb +71 -0
  42. data/spec/unit/railgun/mailer_spec.rb +242 -0
  43. data/vcr_cassettes/email_validation.yml +57 -9
  44. data/vcr_cassettes/events.yml +48 -1
  45. data/vcr_cassettes/suppressions.yml +727 -0
  46. metadata +68 -36
@@ -28,7 +28,11 @@ describe 'For the email validation endpoint', order: :defined, vcr: vcr_opts do
28
28
  expected = {
29
29
  "address" => "alice@mailgun.net",
30
30
  "did_you_mean" => nil,
31
+ "is_disposable_address" => false,
32
+ "is_role_address" => false,
31
33
  "is_valid" => true,
34
+ "mailbox_verification" => "true",
35
+ "reason" => nil,
32
36
  "parts" => {
33
37
  "display_name" => nil,
34
38
  "domain" => "mailgun.net",
@@ -38,13 +42,23 @@ describe 'For the email validation endpoint', order: :defined, vcr: vcr_opts do
38
42
  expect(res).to eq(expected)
39
43
  end
40
44
 
45
+ it 'performs mailbox validation for alice@mailgun.net' do
46
+ res = @mg_obj.validate("alice@mailgun.net", true)
47
+
48
+ expect(res["mailbox_verification"]).to eq("true")
49
+ end
50
+
41
51
  it 'fails to validate example.org' do
42
52
  res = @mg_obj.validate("example.org")
43
53
 
44
54
  expected = {
45
55
  "address" => "example.org",
46
56
  "did_you_mean" => nil,
57
+ "is_disposable_address" => false,
58
+ "is_role_address" => false,
47
59
  "is_valid" => false,
60
+ "mailbox_verification" => "unknown",
61
+ "reason" => "Validation failed for 'example.org', reason: 'malformed address; missing @ sign'",
48
62
  "parts" => {
49
63
  "display_name" => nil,
50
64
  "domain" => nil,
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'mailgun'
3
+ require 'mailgun/events/events'
3
4
 
4
5
  vcr_opts = { :cassette_name => "events" }
5
6
 
@@ -7,9 +8,10 @@ describe 'For the Events endpoint', vcr: vcr_opts do
7
8
  before(:all) do
8
9
  @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
9
10
  @domain = TESTDOMAIN
11
+ @events = Mailgun::Events.new(@mg_obj, @domain)
10
12
  end
11
13
 
12
- it 'get an event.' do
14
+ it 'can get an event.' do
13
15
  result = @mg_obj.get("#{@domain}/events", {:limit => 1})
14
16
 
15
17
  result.to_h!
@@ -17,4 +19,10 @@ describe 'For the Events endpoint', vcr: vcr_opts do
17
19
  expect(result.body["paging"]).to include("next")
18
20
  expect(result.body["paging"]).to include("previous")
19
21
  end
22
+
23
+ it 'can iterate over all events with `each`' do
24
+ @events.each do |e|
25
+ expect(e["id"]).to eq("JAx9z641TuGGUyaJlD9sCQ")
26
+ end
27
+ end
20
28
  end
File without changes
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mailgun'
4
+ require 'mailgun/suppressions'
5
+
6
+ vcr_opts = { :cassette_name => 'suppressions' }
7
+
8
+ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts do
9
+
10
+ before(:all) do
11
+ @mg_obj = Mailgun::Client.new(APIKEY)
12
+ @suppress = Mailgun::Suppressions.new(@mg_obj, TESTDOMAIN)
13
+
14
+ @addresses = ['test1@example.com', 'test2@example.org', 'test3@example.net', 'test4@example.info']
15
+ end
16
+
17
+ it 'can batch-add bounces' do
18
+ bounces = []
19
+ @addresses.each do |addr|
20
+ bounces.push({
21
+ :address => addr,
22
+ :code => 500,
23
+ :error => 'integration testing',
24
+ })
25
+ end
26
+
27
+ response, nested = @suppress.create_bounces bounces
28
+ response.to_h!
29
+
30
+ expect(response.code).to eq(200)
31
+ expect(response.body['message']).to eq('4 addresses have been added to the bounces table')
32
+ expect(nested.length).to eq(0)
33
+ end
34
+
35
+ it 'raises ParameterError if no bounce[:address] is present' do
36
+ bounces = []
37
+ bounces.push({
38
+ :code => 500,
39
+ :error => 'integration testing',
40
+ })
41
+
42
+ expect { @suppress.create_bounces bounces }.to raise_error(Mailgun::ParameterError)
43
+ end
44
+
45
+ it 'removes a single bounce address' do
46
+ @addresses.each do |addr|
47
+ response = @suppress.delete_bounce addr
48
+ response.to_h!
49
+
50
+ expect(response.code).to eq(200)
51
+ expect(response.body['message']).to eq('Bounced address has been removed')
52
+ end
53
+ end
54
+
55
+ it 'can batch-add unsubscribes with tags as string' do
56
+ unsubscribes = []
57
+ @addresses.each do |addr|
58
+ unsubscribes.push({
59
+ :address => addr,
60
+ :tag => 'integration',
61
+ })
62
+ end
63
+
64
+ response, nested = @suppress.create_unsubscribes unsubscribes
65
+ response.to_h!
66
+
67
+ expect(response.code).to eq(200)
68
+ expect(response.body['message']).to eq('4 addresses have been added to the unsubscribes table')
69
+ expect(nested.length).to eq(0)
70
+ end
71
+
72
+ it 'can batch-add unsubscribes with tags as array' do
73
+ unsubscribes = []
74
+ @addresses.each do |addr|
75
+ unsubscribes.push({
76
+ :address => addr,
77
+ :tags => ['integration'],
78
+ })
79
+ end
80
+
81
+ response, nested = @suppress.create_unsubscribes unsubscribes
82
+ response.to_h!
83
+
84
+ expect(response.code).to eq(200)
85
+ expect(response.body['message']).to eq('4 addresses have been added to the unsubscribes table')
86
+ expect(nested.length).to eq(0)
87
+ end
88
+
89
+ it 'raises ParameterError if no unsubscribe[:address] is present' do
90
+ unsubscribes = []
91
+ unsubscribes.push({
92
+ :tag => 'integration',
93
+ })
94
+
95
+ expect { @suppress.create_unsubscribes unsubscribes }.to raise_error(Mailgun::ParameterError)
96
+ end
97
+
98
+ it 'removes a single unsubscribe address' do
99
+ @addresses.each do |addr|
100
+ response = @suppress.delete_unsubscribe addr
101
+ response.to_h!
102
+
103
+ expect(response.code).to eq(200)
104
+ expect(response.body['message']).to eq('Unsubscribe event has been removed')
105
+ end
106
+ end
107
+
108
+ it 'can batch-add complaints' do
109
+ complaints = []
110
+ @addresses.each do |addr|
111
+ complaints.push :address => addr
112
+ end
113
+
114
+ response, nested = @suppress.create_complaints complaints
115
+ response.to_h!
116
+
117
+ expect(response.code).to eq(200)
118
+ expect(response.body['message']).to eq('4 complaint addresses have been added to the complaints table')
119
+ expect(nested.length).to eq(0)
120
+ end
121
+
122
+ it 'raises ParameterError if no complaint[:address] is present' do
123
+ complaints = []
124
+ complaints.push({
125
+ :tag => 'integration',
126
+ })
127
+
128
+ expect { @suppress.create_complaints complaints }.to raise_error(Mailgun::ParameterError)
129
+ end
130
+
131
+ it 'removes a single complaint address' do
132
+ @addresses.each do |addr|
133
+ response = @suppress.delete_complaint addr
134
+ response.to_h!
135
+
136
+ expect(response.code).to eq(200)
137
+ expect(response.body['message']).to eq('Spam complaint has been removed')
138
+ end
139
+ end
140
+
141
+ # TODO: Add tests for pagination support.
142
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'base64'
2
3
  require 'bundler'
3
4
  require 'bundler/setup'
4
5
  Bundler.setup(:development)
@@ -37,9 +38,10 @@ TESTDOMAIN = envs['MAILGUN_TESTDOMAIN']
37
38
  VCR.configure do |c|
38
39
  c.cassette_library_dir = 'vcr_cassettes'
39
40
  c.hook_into :webmock
40
- c.configure_rspec_metadata!
41
41
  c.default_cassette_options = { record: :new_episodes }
42
42
  c.filter_sensitive_data('<APIKEY>') { APIKEY }
43
43
  c.filter_sensitive_data('DOMAIN.TEST') { TESTDOMAIN }
44
44
  c.filter_sensitive_data('<PUBKEY>') { PUB_APIKEY }
45
+
46
+ c.configure_rspec_metadata!
45
47
  end
@@ -5,19 +5,38 @@ describe 'The method get' do
5
5
  @mg_obj = Mailgun::UnitClient.new('events')
6
6
  events = Mailgun::Events.new(@mg_obj, "samples.mailgun.org")
7
7
  result = events.get()
8
-
8
+
9
9
  expect(result.body).to include("items")
10
10
  expect(result.body).to include("paging")
11
11
  end
12
12
  end
13
13
 
14
+ describe 'Pagination' do
15
+ it 'should return a proper hash of log data.' do
16
+ @mg_obj = Mailgun::UnitClient.new('events')
17
+ events = Mailgun::Events.new(@mg_obj, "samples.mailgun.org")
18
+ result = events.get()
19
+
20
+ json = JSON.parse(result.body)
21
+ expect(json).to include("paging")
22
+ expect(json["paging"]).to include("next")
23
+ expect(json["paging"]).to include{"previous"}
24
+ end
25
+
26
+ it 'should calculate proper next-page url' do
27
+ events = Mailgun::Events.new(@mg_obj, "samples.mailgun.org")
28
+ output = events.send(:extract_endpoint_from, '/v3/samples.mailgun.org/events/W3siYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIHsiYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIFsiZiJdLCBudWxsLCBbWyJhY2NvdW50LmlkIiwgIjU4MDUyMTg2NzhmYTE2MTNjNzkwYjUwZiJdLCBbImRvbWFpbi5uYW1lIiwgInNhbmRib3gyOTcwMTUyYWYzZDM0NTU5YmZjN2U3MTcwM2E2Y2YyNC5tYWlsZ3VuLm9yZyJdXSwgMTAwLCBudWxsXQ==')
29
+
30
+ expect(output).to eq 'W3siYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIHsiYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIFsiZiJdLCBudWxsLCBbWyJhY2NvdW50LmlkIiwgIjU4MDUyMTg2NzhmYTE2MTNjNzkwYjUwZiJdLCBbImRvbWFpbi5uYW1lIiwgInNhbmRib3gyOTcwMTUyYWYzZDM0NTU5YmZjN2U3MTcwM2E2Y2YyNC5tYWlsZ3VuLm9yZyJdXSwgMTAwLCBudWxsXQ=='
31
+ end
32
+ end
14
33
 
15
34
  describe 'The method next' do
16
35
  it 'should return the next series of data.' do
17
36
  @mg_obj = Mailgun::UnitClient.new('events')
18
37
  events = Mailgun::Events.new(@mg_obj, "samples.mailgun.org")
19
38
  result = events.next()
20
-
39
+
21
40
  expect(result.body).to include("items")
22
41
  expect(result.body).to include("paging")
23
42
  end
@@ -33,3 +52,18 @@ describe 'The method previous' do
33
52
  expect(result.body).to include("paging")
34
53
  end
35
54
  end
55
+
56
+ describe 'The method each' do
57
+ it 'should iterate over all event items.' do
58
+ @mg_obj = Mailgun::UnitClient.new('events')
59
+ events = Mailgun::Events.new(@mg_obj, "samples.mailgun.org")
60
+ # Events from the UnitClient are actually empty.
61
+ count = 0
62
+ events.each do |e|
63
+ count = count + 1
64
+ end
65
+
66
+ # Better than nothing..
67
+ expect(count).to eq(0)
68
+ end
69
+ end
@@ -95,6 +95,7 @@ describe 'The method add_recipient' do
95
95
  expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
96
96
  @mb_obj.finalize
97
97
 
98
+ expect(@mb_obj.recipient_variables).to eq({})
98
99
  expect(@mb_obj.message['recipient-variables'].length).to eq(0)
99
100
  expect(@mb_obj.message[:to].length).to eq(0)
100
101
  expect(@mb_obj.counters[:recipients][recipient_type]).to eq(0)
@@ -50,6 +50,16 @@ describe 'The method add_recipient' do
50
50
  expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
51
51
  end
52
52
 
53
+ context 'when variables is empty and recepeint type - "to"' do
54
+ it 'adds email address as "to" recipient type and increments counter' do
55
+ recipient_type = :to
56
+ @mb_obj.add_recipient(recipient_type, @address, {})
57
+
58
+ expect(@mb_obj.message[recipient_type][0]).to eq("#{@address}")
59
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
60
+ end
61
+ end
62
+
53
63
  it 'adds a "cc" recipient type to the message body and counter is incremented' do
54
64
  recipient_type = :cc
55
65
  @mb_obj.add_recipient(recipient_type, @address, @variables)
@@ -70,7 +80,7 @@ describe 'The method add_recipient' do
70
80
  recipient_type = 'h:reply-to'
71
81
  @mb_obj.add_recipient(recipient_type, @address, @variables)
72
82
 
73
- expect(@mb_obj.message[recipient_type][0]).to eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
83
+ expect(@mb_obj.message[recipient_type]).to eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
74
84
  @mb_obj.counters[:recipients].each_value{|value| expect(value).to eq(0)}
75
85
  end
76
86
 
@@ -196,7 +206,7 @@ describe 'The method from' do
196
206
  expect(@mb_obj.message[:from]).to eq([the_from_address])
197
207
  end
198
208
 
199
- it 'sets the from address with metadata' do
209
+ it 'sets the from address with first/last metadata' do
200
210
  the_from_address = 'test@mailgun.com'
201
211
  the_first_name = 'Magilla'
202
212
  the_last_name = 'Gorilla'
@@ -204,6 +214,21 @@ describe 'The method from' do
204
214
 
205
215
  expect(@mb_obj.message[:from]).to eq(["'#{the_first_name} #{the_last_name}' <#{the_from_address}>"])
206
216
  end
217
+
218
+ it 'sets the from address with full name metadata' do
219
+ the_from_address = 'test@mailgun.com'
220
+ full_name = 'Magilla Gorilla'
221
+ @mb_obj.from(the_from_address, {'full_name' => full_name})
222
+
223
+ expect(@mb_obj.message[:from]).to eq(["'#{full_name}' <#{the_from_address}>"])
224
+ end
225
+
226
+ it 'fails when first/last and full_name are used' do
227
+ the_from_address = 'test@mailgun.com'
228
+ full_name = 'Magilla Gorilla'
229
+ first_name = 'Magilla'
230
+ expect{@mb_obj.from(the_from_address, {'full_name' => full_name, 'first' => first_name})}.to raise_error(Mailgun::ParameterError)
231
+ end
207
232
  end
208
233
 
209
234
  describe 'The method add_attachment' do
@@ -231,6 +256,16 @@ describe 'The method add_attachment' do
231
256
  expect(@mb_obj.message[:attachment].length).to eq(1)
232
257
  expect(@mb_obj.message[:attachment].first.original_filename).to eq 'mailgun_icon.png'
233
258
  end
259
+
260
+ context 'when attachment has unknown type' do
261
+ it 'sets content type application/octet-stream for attachment' do
262
+ file = File.dirname(__FILE__) + "/sample_data/unknown.type"
263
+
264
+ @mb_obj.add_attachment(file)
265
+
266
+ expect(@mb_obj.message[:attachment][0].content_type).to eq('application/octet-stream')
267
+ end
268
+ end
234
269
  end
235
270
 
236
271
  describe 'The method add_inline_image' do
@@ -249,6 +284,31 @@ describe 'The method add_inline_image' do
249
284
  end
250
285
  end
251
286
 
287
+ describe 'The method list_unsubscribe' do
288
+ before(:each) do
289
+ @mb_obj = Mailgun::MessageBuilder.new
290
+ end
291
+
292
+ it 'sets the message list_unsubscribe to blank if called and no parameters are provided' do
293
+ @mb_obj.list_unsubscribe
294
+ expect(@mb_obj.message['h:List-Unsubscribe']).to eq('')
295
+ end
296
+
297
+ it 'sets the message list_unsubscribe if called with one list_unsubscribe parameter' do
298
+ unsubscribe_to = 'http://example.com/stop-hassle'
299
+ @mb_obj.list_unsubscribe(unsubscribe_to)
300
+ expect(@mb_obj.message['h:List-Unsubscribe']).to eq("<#{unsubscribe_to}>")
301
+ end
302
+
303
+ it 'sets the message list_unsubscribe if called with many list_unsubscribe parameters' do
304
+ unsubscribe_to = %w(http://example.com/stop-hassle mailto:stop-hassle@example.com)
305
+ @mb_obj.list_unsubscribe(*unsubscribe_to)
306
+ expect(@mb_obj.message['h:List-Unsubscribe']).to eq(
307
+ unsubscribe_to.map { |var| "<#{var}>" }.join(',')
308
+ )
309
+ end
310
+ end
311
+
252
312
  describe 'The method set_test_mode' do
253
313
  it 'warns of set_test_mode deprecation' do
254
314
  @mb_obj = Mailgun::MessageBuilder.new
@@ -400,19 +460,19 @@ describe 'The method track_opens' do
400
460
  it 'enables/disables open tracking on a per message basis.' do
401
461
  @mb_obj.track_opens('Yes')
402
462
 
403
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("yes")
463
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("yes")
404
464
 
405
465
  @mb_obj.track_opens('No')
406
466
 
407
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
467
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("no")
408
468
 
409
469
  @mb_obj.track_opens(true)
410
470
 
411
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("yes")
471
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("yes")
412
472
 
413
473
  @mb_obj.track_opens(false)
414
474
 
415
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
475
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("no")
416
476
  end
417
477
  end
418
478
 
@@ -431,23 +491,23 @@ describe 'The method track_clicks' do
431
491
  it 'enables/disables click tracking on a per message basis.' do
432
492
  @mb_obj.track_clicks('Yes')
433
493
 
434
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("yes")
494
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("yes")
435
495
 
436
496
  @mb_obj.track_clicks('No')
437
497
 
438
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
498
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("no")
439
499
 
440
500
  @mb_obj.track_clicks(true)
441
501
 
442
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("yes")
502
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("yes")
443
503
 
444
504
  @mb_obj.track_clicks(false)
445
505
 
446
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
506
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("no")
447
507
 
448
508
  @mb_obj.track_clicks('html')
449
509
 
450
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("html")
510
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("html")
451
511
  end
452
512
  end
453
513
 
@@ -485,19 +545,35 @@ describe 'The method header' do
485
545
  it 'accepts valid JSON and appends as data to the message.' do
486
546
  @mb_obj.header('my-data', '{"key":"value"}')
487
547
 
488
- expect(@mb_obj.message["v:my-data"][0]).to be_kind_of(String)
489
- expect(@mb_obj.message["v:my-data"][0].to_s).to eq('{"key"=>"value"}')
548
+ expect(@mb_obj.message["h:my-data"]).to be_kind_of(String)
549
+ expect(@mb_obj.message["h:my-data"].to_s).to eq('{"key":"value"}')
550
+ end
551
+ end
552
+
553
+ describe 'The method variable' do
554
+ before(:each) do
555
+ @mb_obj = Mailgun::MessageBuilder.new
556
+ end
557
+ it 'accepts valid JSON and stores it as message[param].' do
558
+ @mb_obj.variable('my-data', '{"key":"value"}')
559
+
560
+ expect(@mb_obj.message["v:my-data"]).to be_kind_of(String)
561
+ expect(@mb_obj.message["v:my-data"].to_s).to eq('{"key":"value"}')
490
562
  end
491
563
  it 'accepts a hash and appends as data to the message.' do
492
564
  data = {'key' => 'value'}
493
- @mb_obj.header('my-data', data)
565
+ @mb_obj.variable('my-data', data)
494
566
 
495
- expect(@mb_obj.message["v:my-data"][0]).to be_kind_of(String)
496
- expect(@mb_obj.message["v:my-data"][0].to_s).to eq('{"key"=>"value"}')
567
+ expect(@mb_obj.message["v:my-data"]).to be_kind_of(String)
568
+ expect(@mb_obj.message["v:my-data"].to_s).to eq('{"key":"value"}')
497
569
  end
498
- it 'throws an exception on broken JSON.' do
499
- data = 'This is some crappy JSON.'
500
- expect {@mb_obj.header('my-data', data)}.to raise_error(Mailgun::ParameterError)
570
+ it 'accepts string values' do
571
+ data = 'String Value.'
572
+
573
+ @mb_obj.variable('my-data', data)
574
+
575
+ expect(@mb_obj.message["v:my-data"]).to be_kind_of(String)
576
+ expect(@mb_obj.message["v:my-data"].to_s).to eq('String Value.')
501
577
  end
502
578
  end
503
579
 
File without changes
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'mailgun'
3
+ require 'railgun'
4
+
5
+ describe 'extract_body' do
6
+
7
+ let(:text_mail_option) {
8
+ {
9
+ from: 'bob@example.com',
10
+ to: 'sally@example.com',
11
+ subject: 'RAILGUN TEST SAMPLE',
12
+ body: text_content,
13
+ content_type: 'text/plain',
14
+ }
15
+ }
16
+ let(:text_content) { '[TEST] Hello, world.' }
17
+
18
+ let(:html_mail_option) {
19
+ {
20
+ from: 'bob@example.com',
21
+ to: 'sally@example.com',
22
+ subject: 'RAILGUN TEST SAMPLE',
23
+ body: html_content,
24
+ content_type: 'text/html',
25
+ }
26
+ }
27
+ let(:html_content) { '<h3> [TEST] </h3> <br/> Hello, world!' }
28
+
29
+ context 'with <Content-Type: text/plain>' do
30
+ let(:sample_mail) { Mail.new(text_mail_option) }
31
+
32
+ it 'should return body text' do
33
+ expect(Railgun.extract_body_text(sample_mail)).to eq(text_content)
34
+ end
35
+
36
+ it 'should not return body html' do
37
+ expect(Railgun.extract_body_html(sample_mail)).to be_nil
38
+ end
39
+ end
40
+
41
+ context 'with <Content-Type: text/html>' do
42
+ let(:sample_mail) { Mail.new(html_mail_option) }
43
+
44
+ it 'should not return body text' do
45
+ expect(Railgun.extract_body_text(sample_mail)).to be_nil
46
+ end
47
+
48
+ it 'should return body html' do
49
+ expect(Railgun.extract_body_html(sample_mail)).to eq(html_content)
50
+ end
51
+ end
52
+
53
+ context 'with <Content-Type: multipart/alternative>' do
54
+ let(:text_mail) { Mail.new(text_mail_option) }
55
+ let(:html_mail) { Mail.new(html_mail_option) }
56
+
57
+ before do
58
+ @sample_mail = Mail::Part.new(content_type: "multipart/alternative")
59
+ @sample_mail.add_part text_mail
60
+ @sample_mail.add_part html_mail
61
+ end
62
+
63
+ it 'should return body text' do
64
+ expect(Railgun.extract_body_text(@sample_mail)).to eq(text_content)
65
+ end
66
+
67
+ it 'should return body html' do
68
+ expect(Railgun.extract_body_html(@sample_mail)).to eq(html_content)
69
+ end
70
+ end
71
+ end