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
@@ -18,21 +18,22 @@ describe 'The method send_message()' do
18
18
  @mb_obj = Mailgun::MessageBuilder.new()
19
19
  @mh_obj = {}
20
20
 
21
- expect {@mg_obj.send_message("test.com", "incorrect data")}.to raise_error
21
+ expect {@mg_obj.send_message("test.com", "incorrect data")}.to raise_error Mailgun::ParameterError
22
22
  expect {@mg_obj.send_message("test.com", @mb_obj)}.not_to raise_error
23
23
  expect {@mg_obj.send_message("test.com", @mh_obj)}.not_to raise_error
24
24
  end
25
25
 
26
26
  it 'sends a message' do
27
- data = {'from' => 'joe@test.com',
27
+ data = { 'from' => 'joe@test.com',
28
28
  'to' => 'bob@example.com',
29
29
  'subject' => 'Test',
30
30
  'text' => 'Test Data'}
31
31
  result = @mg_obj.send_message("testdomain.com", data)
32
32
 
33
33
  result.to_h!
34
- result.body.should include("message")
35
- result.body.should include("id")
34
+
35
+ expect(result.body).to include("message")
36
+ expect(result.body).to include("id")
36
37
  end
37
38
 
38
39
  it 'opens the message MIME and sends the MIME message.' do
@@ -41,8 +42,9 @@ describe 'The method send_message()' do
41
42
  result = @mg_obj.send_message("testdomain.com", data)
42
43
 
43
44
  result.to_h!
44
- result.body.should include("message")
45
- result.body.should include("id")
45
+
46
+ expect(result.body).to include("message")
47
+ expect(result.body).to include("id")
46
48
  end
47
49
  end
48
50
 
@@ -59,8 +61,9 @@ describe 'The method post()' do
59
61
  result = @mg_obj.post("#{@domain}/messages", data)
60
62
 
61
63
  result.to_h!
62
- result.body.should include("message")
63
- result.body.should include("id")
64
+
65
+ expect(result.body).to include("message")
66
+ expect(result.body).to include("id")
64
67
  end
65
68
  end
66
69
 
@@ -78,13 +81,14 @@ describe 'The method put()' do
78
81
  result = @mg_obj.put("lists/#{@list_address}/members#{@member_address}", data)
79
82
 
80
83
  result.to_h!
81
- result.body.should include("member")
82
- result.body["member"].should include("vars")
83
- result.body["member"]["vars"].should include("age")
84
- result.body["member"].should include("name")
85
- result.body["member"].should include("subscribed")
86
- result.body["member"].should include("address")
87
- result.body.should include("message")
84
+
85
+ expect(result.body).to include("member")
86
+ expect(result.body["member"]).to include("vars")
87
+ expect(result.body["member"]["vars"]).to include("age")
88
+ expect(result.body["member"]).to include("name")
89
+ expect(result.body["member"]).to include("subscribed")
90
+ expect(result.body["member"]).to include("address")
91
+ expect(result.body).to include("message")
88
92
  end
89
93
 
90
94
  end
@@ -100,8 +104,9 @@ describe 'The method get()' do
100
104
  result = @mg_obj.get("#{@domain}/bounces", query_string)
101
105
 
102
106
  result.to_h!
103
- result.body.should include("total_count")
104
- result.body.should include("items")
107
+
108
+ expect(result.body).to include("total_count")
109
+ expect(result.body).to include("items")
105
110
  end
106
111
  end
107
112
 
@@ -115,7 +120,8 @@ describe 'The method delete()' do
115
120
  result = @mg_obj.delete("#{@domain}/campaigns/ABC123")
116
121
 
117
122
  result.to_h!
118
- result.body.should include("message")
119
- result.body.should include("id")
123
+
124
+ expect(result.body).to include("message")
125
+ expect(result.body).to include("id")
120
126
  end
121
127
  end
@@ -4,11 +4,12 @@ describe 'BatchMessage attribute readers' do
4
4
  it 'should be readable' do
5
5
  @mb_client = Mailgun::UnitClient.new("messages")
6
6
  @mb_obj = Mailgun::BatchMessage.new(@mb_client, "example.com")
7
- @mb_obj.should respond_to(:message_ids)
8
- @mb_obj.should respond_to(:message)
9
- @mb_obj.should respond_to(:counters)
10
- @mb_obj.should respond_to(:recipient_variables)
11
- @mb_obj.should respond_to(:domain)
7
+
8
+ expect(@mb_obj).to respond_to(:message_ids)
9
+ expect(@mb_obj).to respond_to(:message)
10
+ expect(@mb_obj).to respond_to(:counters)
11
+ expect(@mb_obj).to respond_to(:recipient_variables)
12
+ expect(@mb_obj).to respond_to(:domain)
12
13
  end
13
14
  end
14
15
 
@@ -20,43 +21,43 @@ describe 'The instantiation of Batch Message' do
20
21
  end
21
22
 
22
23
  it 'contains Message, which should be of type Hash and empty' do
23
- @mb_obj.message.should be_a(Hash)
24
- @mb_obj.message.length.should eq(0)
24
+ expect(@mb_obj.message).to be_a(Hash)
25
+ expect(@mb_obj.message.length).to eq(0)
25
26
  end
26
27
 
27
28
  it 'contains recipient_variables, which should be of type Hash and empty' do
28
- @mb_obj.recipient_variables.should be_a(Hash)
29
- @mb_obj.recipient_variables.length.should eq(0)
29
+ expect(@mb_obj.recipient_variables).to be_a(Hash)
30
+ expect(@mb_obj.recipient_variables.length).to eq(0)
30
31
  end
31
32
 
32
33
  it 'contains domain, which should be of type string and contain example.com' do
33
- @mb_obj.domain.should be_a(String)
34
- @mb_obj.domain.should eq('example.com')
34
+ expect(@mb_obj.domain).to be_a(String)
35
+ expect(@mb_obj.domain).to eq('example.com')
35
36
  end
36
37
 
37
38
  it 'contains message_ids, which should be of type hash and empty' do
38
- @mb_obj.message_ids.should be_a(Hash)
39
- @mb_obj.message_ids.length.should eq(0)
39
+ expect(@mb_obj.message_ids).to be_a(Hash)
40
+ expect(@mb_obj.message_ids.length).to eq(0)
40
41
  end
41
42
 
42
43
  it 'contains counters, which should be of type hash and contain several important counters' do
43
- @mb_obj.counters.should be_a(Hash)
44
- @mb_obj.counters.should include(:recipients)
44
+ expect(@mb_obj.counters).to be_a(Hash)
45
+ expect(@mb_obj.counters).to include(:recipients)
45
46
  end
46
47
 
47
48
  it 'contains counters, which should be of type hash and contain several important counters' do
48
- @mb_obj.counters.should be_a(Hash)
49
-
50
- @mb_obj.counters.should include(:recipients)
51
- @mb_obj.counters[:recipients].should include(:to)
52
- @mb_obj.counters[:recipients].should include(:cc)
53
- @mb_obj.counters[:recipients].should include(:bcc)
54
-
55
- @mb_obj.counters.should include(:attributes)
56
- @mb_obj.counters[:attributes].should include(:attachment)
57
- @mb_obj.counters[:attributes].should include(:campaign_id)
58
- @mb_obj.counters[:attributes].should include(:custom_option)
59
- @mb_obj.counters[:attributes].should include(:tag)
49
+ expect(@mb_obj.counters).to be_a(Hash)
50
+
51
+ expect(@mb_obj.counters).to include(:recipients)
52
+ expect(@mb_obj.counters[:recipients]).to include(:to)
53
+ expect(@mb_obj.counters[:recipients]).to include(:cc)
54
+ expect(@mb_obj.counters[:recipients]).to include(:bcc)
55
+
56
+ expect(@mb_obj.counters).to include(:attributes)
57
+ expect(@mb_obj.counters[:attributes]).to include(:attachment)
58
+ expect(@mb_obj.counters[:attributes]).to include(:campaign_id)
59
+ expect(@mb_obj.counters[:attributes]).to include(:custom_option)
60
+ expect(@mb_obj.counters[:attributes]).to include(:tag)
60
61
  end
61
62
  end
62
63
 
@@ -77,9 +78,12 @@ describe 'The method add_recipient' do
77
78
  1000.times do
78
79
  @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
79
80
  end
80
- @mb_obj.counters[:recipients][recipient_type].should eq(1000)
81
+
82
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
83
+
81
84
  @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
82
- @mb_obj.counters[:recipients][recipient_type].should eq(1)
85
+
86
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
83
87
  end
84
88
 
85
89
  it 'adds recipients to the message, calls finalize, and cleans up' do
@@ -87,11 +91,13 @@ describe 'The method add_recipient' do
87
91
  1000.times do
88
92
  @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
89
93
  end
90
- @mb_obj.counters[:recipients][recipient_type].should eq(1000)
94
+
95
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
91
96
  @mb_obj.finalize
92
- @mb_obj.message['recipient-variables'].length.should eq(0)
93
- @mb_obj.message[:to].length.should eq(0)
94
- @mb_obj.counters[:recipients][recipient_type].should eq(0)
97
+
98
+ expect(@mb_obj.message['recipient-variables'].length).to eq(0)
99
+ expect(@mb_obj.message[:to].length).to eq(0)
100
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(0)
95
101
  end
96
102
 
97
103
  it 'adds 5,005 recipients to the message body and validates we receive message_ids back' do
@@ -100,13 +106,15 @@ describe 'The method add_recipient' do
100
106
  @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
101
107
  end
102
108
  @mb_obj.finalize
103
- @mb_obj.message_ids.length.should eq(6)
109
+
110
+ expect(@mb_obj.message_ids.length).to eq(6)
104
111
  end
105
112
 
106
113
  it 'sets recipient-variables, for batch expansion' do
107
114
  recipient_type = :to
108
115
  @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
109
- @mb_obj.recipient_variables[@address_1].should eq(@variables_1)
116
+
117
+ expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
110
118
  end
111
119
 
112
120
  it 'sets multiple recipient-variables, for batch expansion' do
@@ -114,9 +122,10 @@ describe 'The method add_recipient' do
114
122
  @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
115
123
  @mb_obj.add_recipient(recipient_type, @address_2, @variables_2)
116
124
  @mb_obj.add_recipient(recipient_type, @address_3, @variables_3)
117
- @mb_obj.recipient_variables[@address_1].should eq(@variables_1)
118
- @mb_obj.recipient_variables[@address_2].should eq(@variables_2)
119
- @mb_obj.recipient_variables[@address_3].should eq(@variables_3)
125
+
126
+ expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
127
+ expect(@mb_obj.recipient_variables[@address_2]).to eq(@variables_2)
128
+ expect(@mb_obj.recipient_variables[@address_3]).to eq(@variables_3)
120
129
  end
121
130
 
122
131
  end
@@ -3,8 +3,9 @@ require 'spec_helper'
3
3
  describe 'MessageBuilder attribute readers' do
4
4
  it 'should be readable' do
5
5
  @mb_obj = Mailgun::MessageBuilder.new()
6
- @mb_obj.should respond_to(:message)
7
- @mb_obj.should respond_to(:counters)
6
+
7
+ expect(@mb_obj).to respond_to(:message)
8
+ expect(@mb_obj).to respond_to(:counters)
8
9
  end
9
10
  end
10
11
 
@@ -15,21 +16,21 @@ describe 'The instantiation of MessageBuilder' do
15
16
  end
16
17
 
17
18
  it 'contains counters, which should be of type hash and contain several important counters' do
18
- @mb_obj.counters.should be_a(Hash)
19
- @mb_obj.counters.should include(:recipients)
19
+ expect(@mb_obj.counters).to be_a(Hash)
20
+ expect(@mb_obj.counters).to include(:recipients)
20
21
  end
21
22
 
22
23
  it 'contains counters, which should be of type hash and contain several important counters' do
23
- @mb_obj.counters.should be_a(Hash)
24
- @mb_obj.counters.should include(:recipients)
25
- @mb_obj.counters[:recipients].should include(:to)
26
- @mb_obj.counters[:recipients].should include(:cc)
27
- @mb_obj.counters[:recipients].should include(:bcc)
28
- @mb_obj.counters.should include(:attributes)
29
- @mb_obj.counters[:attributes].should include(:attachment)
30
- @mb_obj.counters[:attributes].should include(:campaign_id)
31
- @mb_obj.counters[:attributes].should include(:custom_option)
32
- @mb_obj.counters[:attributes].should include(:tag)
24
+ expect(@mb_obj.counters).to be_a(Hash)
25
+ expect(@mb_obj.counters).to include(:recipients)
26
+ expect(@mb_obj.counters[:recipients]).to include(:to)
27
+ expect(@mb_obj.counters[:recipients]).to include(:cc)
28
+ expect(@mb_obj.counters[:recipients]).to include(:bcc)
29
+ expect(@mb_obj.counters).to include(:attributes)
30
+ expect(@mb_obj.counters[:attributes]).to include(:attachment)
31
+ expect(@mb_obj.counters[:attributes]).to include(:campaign_id)
32
+ expect(@mb_obj.counters[:attributes]).to include(:custom_option)
33
+ expect(@mb_obj.counters[:attributes]).to include(:tag)
33
34
  end
34
35
  end
35
36
 
@@ -43,39 +44,45 @@ describe 'The method add_recipient' do
43
44
  it 'adds a "to" recipient type to the message body and counter is incremented' do
44
45
  recipient_type = :to
45
46
  @mb_obj.add_recipient(recipient_type, @address, @variables)
46
- @mb_obj.message[recipient_type][0].should eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
47
- @mb_obj.counters[:recipients][recipient_type].should eq(1)
47
+
48
+ expect(@mb_obj.message[recipient_type][0]).to eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
49
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
48
50
  end
49
51
 
50
52
  it 'adds a "cc" recipient type to the message body and counter is incremented' do
51
53
  recipient_type = :cc
52
54
  @mb_obj.add_recipient(recipient_type, @address, @variables)
53
- @mb_obj.message[recipient_type][0].should eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
54
- @mb_obj.counters[:recipients][recipient_type].should eq(1)
55
+
56
+ expect(@mb_obj.message[recipient_type][0]).to eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
57
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
55
58
  end
56
59
 
57
60
  it 'adds a "bcc" recipient type to the message body and counter is incremented' do
58
61
  recipient_type = :bcc
59
62
  @mb_obj.add_recipient(recipient_type, @address, @variables)
60
- @mb_obj.message[recipient_type][0].should eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
61
- @mb_obj.counters[:recipients][recipient_type].should eq(1)
63
+
64
+ expect(@mb_obj.message[recipient_type][0]).to eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
65
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
62
66
  end
63
67
 
64
68
  it 'adds a "h:reply-to" recipient type to the message body and counters are not incremented' do
65
69
  recipient_type = 'h:reply-to'
66
70
  @mb_obj.add_recipient(recipient_type, @address, @variables)
67
- @mb_obj.message[recipient_type][0].should eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
68
- @mb_obj.counters[:recipients].each_value{|value| value.should eq(0)}
71
+
72
+ expect(@mb_obj.message[recipient_type][0]).to eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
73
+ @mb_obj.counters[:recipients].each_value{|value| expect(value).to eq(0)}
69
74
  end
70
75
 
71
76
  it 'ensures a random recipient type is added to the message body and counters are not incremented' do
72
77
  recipient_type = 'im-not-really-real'
73
78
  @mb_obj.add_recipient(recipient_type, @address, @variables)
74
- @mb_obj.message[recipient_type][0].should eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
75
- @mb_obj.counters[:recipients].each_value{|value| value.should eq(0)}
79
+
80
+ expect(@mb_obj.message[recipient_type][0]).to eq("'#{@variables['first']} #{@variables['last']}' <#{@address}>")
81
+ @mb_obj.counters[:recipients].each_value{|value| expect(value).to eq(0)}
76
82
  end
77
83
  it 'adds too many to recipients and raises an exception.' do
78
84
  recipient_type = :to
85
+
79
86
  expect{
80
87
  1001.times do
81
88
  @mb_obj.add_recipient(recipient_type, @address, @variables)
@@ -83,6 +90,7 @@ describe 'The method add_recipient' do
83
90
  end
84
91
  it 'adds too many cc recipients and raises an exception.' do
85
92
  recipient_type = :cc
93
+
86
94
  expect{
87
95
  1001.times do
88
96
  @mb_obj.add_recipient(recipient_type, @address, @variables)
@@ -90,6 +98,7 @@ describe 'The method add_recipient' do
90
98
  end
91
99
  it 'adds too many bcc recipients and raises an exception.' do
92
100
  recipient_type = :bcc
101
+
93
102
  expect{
94
103
  1001.times do
95
104
  @mb_obj.add_recipient(recipient_type, @address, @variables)
@@ -98,48 +107,101 @@ describe 'The method add_recipient' do
98
107
  end
99
108
 
100
109
  describe 'The method set_subject' do
110
+ it 'warns of set_subject deprecation' do
111
+ @mb_obj = Mailgun::MessageBuilder.new
112
+ expect(@mb_obj).to receive :warn
113
+ @mb_obj.set_subject 'warn on set_subject'
114
+ end
115
+ end
116
+
117
+ describe 'The method subject' do
101
118
  before(:each) do
102
119
  @mb_obj = Mailgun::MessageBuilder.new
103
120
  end
121
+
104
122
  it 'sets the message subject to blank if called and no parameters are provided' do
105
- @mb_obj.set_subject
106
- @mb_obj.message[:subject][0].should eq('')
123
+ @mb_obj.subject
124
+
125
+ expect(@mb_obj.message[:subject][0]).to eq('')
107
126
  end
127
+
108
128
  it 'sets the message subject if called with the subject parameter' do
109
129
  the_subject = 'This is my subject!'
110
- @mb_obj.set_subject(the_subject)
111
- @mb_obj.message[:subject][0].should eq(the_subject)
130
+ @mb_obj.subject(the_subject)
131
+
132
+ expect(@mb_obj.message[:subject][0]).to eq(the_subject)
112
133
  end
134
+
113
135
  it 'ensures no duplicate subjects can exist and last setter is stored' do
114
136
  the_first_subject = 'This is my first subject!'
115
137
  the_second_subject = 'This is my second subject!'
116
- @mb_obj.set_subject(the_first_subject)
117
- @mb_obj.set_subject(the_second_subject)
118
- @mb_obj.message[:subject].length.should eq(1)
119
- @mb_obj.message[:subject][0].should eq(the_second_subject)
138
+ @mb_obj.subject(the_first_subject)
139
+ @mb_obj.subject(the_second_subject)
140
+
141
+ expect(@mb_obj.message[:subject].length).to eq(1)
142
+ expect(@mb_obj.message[:subject][0]).to eq(the_second_subject)
120
143
  end
121
144
  end
122
145
 
123
146
  describe 'The method set_text_body' do
147
+ it 'warns of set_text_body deprecation' do
148
+ @mb_obj = Mailgun::MessageBuilder.new
149
+ expect(@mb_obj).to receive :warn
150
+ @mb_obj.set_text_body 'warn on set_text_body'
151
+ end
152
+ end
153
+
154
+ describe 'The method body_text' do
124
155
  before(:each) do
125
156
  @mb_obj = Mailgun::MessageBuilder.new
126
157
  end
127
- it 'sets the text_body to blank if called and no parameters are provided' do
128
- @mb_obj.set_text_body
158
+ it 'sets the body_text to blank if called and no parameters are provided' do
159
+ @mb_obj.body_text
129
160
  @mb_obj.message[:text] = ''
130
161
  end
131
- it 'sets the message text if called with the text_body parameter' do
162
+ it 'sets the message text if called with the body_text parameter' do
132
163
  the_text = 'Don\'t mess with Texas!'
133
- @mb_obj.set_text_body(the_text)
164
+ @mb_obj.body_text(the_text)
134
165
  @mb_obj.message[:text] = the_text
135
166
  end
136
167
  it 'ensures no duplicate text bodies can exist and last setter is stored' do
137
168
  the_first_text = 'Mess with Texas!'
138
169
  the_second_text = 'Don\'t mess with Texas!'
139
- @mb_obj.set_text_body(the_first_text)
140
- @mb_obj.set_text_body(the_second_text)
141
- @mb_obj.message[:text].length.should eq(1)
142
- @mb_obj.message[:text][0].should eq(the_second_text)
170
+ @mb_obj.body_text(the_first_text)
171
+ @mb_obj.body_text(the_second_text)
172
+
173
+ expect(@mb_obj.message[:text].length).to eq(1)
174
+ expect(@mb_obj.message[:text][0]).to eq(the_second_text)
175
+ end
176
+ end
177
+
178
+ describe 'The method set_from_address' do
179
+ it 'warns of set_from_address deprecation' do
180
+ @mb_obj = Mailgun::MessageBuilder.new
181
+ expect(@mb_obj).to receive :warn
182
+ @mb_obj.set_from_address 'warn on set_from_address'
183
+ end
184
+ end
185
+
186
+ describe 'The method from' do
187
+ before(:each) do
188
+ @mb_obj = Mailgun::MessageBuilder.new
189
+ end
190
+
191
+ it 'sets the from address' do
192
+ the_from_address = 'test@mailgun.com'
193
+ @mb_obj.from(the_from_address)
194
+
195
+ expect(@mb_obj.message[:from]).to eq([the_from_address])
196
+ end
197
+
198
+ it 'sets the from address with metadata' do
199
+ the_from_address = 'test@mailgun.com'
200
+ the_first_name = 'Magilla'
201
+ the_last_name = 'Gorilla'
202
+ @mb_obj.from(the_from_address, {'first' => the_first_name, 'last' => the_last_name})
203
+
204
+ expect(@mb_obj.message[:from]).to eq(["'#{the_first_name} #{the_last_name}' <#{the_from_address}>"])
143
205
  end
144
206
  end
145
207
 
@@ -155,7 +217,18 @@ describe 'The method add_attachment' do
155
217
  file_paths = [file1, file2]
156
218
 
157
219
  file_paths.each {|item| @mb_obj.add_attachment(item)}
158
- @mb_obj.message[:attachment].length.should eq(2)
220
+
221
+ expect(@mb_obj.message[:attachment].length).to eq(2)
222
+ end
223
+
224
+ it 'adds file-like objects to the message object' do
225
+ io = StringIO.new
226
+ io << File.binread(File.dirname(__FILE__) + "/sample_data/mailgun_icon.png")
227
+
228
+ @mb_obj.add_attachment io, 'mailgun_icon.png'
229
+
230
+ expect(@mb_obj.message[:attachment].length).to eq(1)
231
+ expect(@mb_obj.message[:attachment].first.original_filename).to eq 'mailgun_icon.png'
159
232
  end
160
233
  end
161
234
 
@@ -170,65 +243,92 @@ describe 'The method add_inline_image' do
170
243
  file_paths = [file1, file2]
171
244
 
172
245
  file_paths.each {|item| @mb_obj.add_inline_image(item)}
173
- @mb_obj.message[:inline].length.should eq(2)
246
+
247
+ expect(@mb_obj.message[:inline].length).to eq(2)
174
248
  end
175
249
  end
176
250
 
177
251
  describe 'The method set_test_mode' do
252
+ it 'warns of set_test_mode deprecation' do
253
+ @mb_obj = Mailgun::MessageBuilder.new
254
+ expect(@mb_obj).to receive :warn
255
+ @mb_obj.set_test_mode 'warn on set_test_mode'
256
+ end
257
+ end
258
+
259
+ describe 'The method test_mode' do
178
260
  before(:each) do
179
261
  @mb_obj = Mailgun::MessageBuilder.new
180
262
  end
181
263
  it 'turns on test mode with boolean true' do
182
- @mb_obj.set_test_mode(true)
183
- @mb_obj.message["o:testmode"][0].should eq("yes")
264
+ @mb_obj.test_mode(true)
265
+
266
+ expect(@mb_obj.message["o:testmode"][0]).to eq("yes")
184
267
  end
185
268
  it 'turns on test mode with string true' do
186
- @mb_obj.set_test_mode("true")
187
- @mb_obj.message["o:testmode"][0].should eq("yes")
269
+ @mb_obj.test_mode("true")
270
+
271
+ expect(@mb_obj.message["o:testmode"][0]).to eq("yes")
188
272
  end
189
273
  it 'turns off test mode with boolean false' do
190
- @mb_obj.set_test_mode(false)
191
- @mb_obj.message["o:testmode"][0].should eq("no")
274
+ @mb_obj.test_mode(false)
275
+
276
+ expect(@mb_obj.message["o:testmode"][0]).to eq("no")
192
277
  end
193
278
  it 'turns off test mode with string false' do
194
- @mb_obj.set_test_mode("false")
195
- @mb_obj.message["o:testmode"][0].should eq("no")
279
+ @mb_obj.test_mode("false")
280
+
281
+ expect(@mb_obj.message["o:testmode"][0]).to eq("no")
196
282
  end
197
283
  it 'does not allow multiple values' do
198
- @mb_obj.set_test_mode("false")
199
- @mb_obj.set_test_mode("true")
200
- @mb_obj.set_test_mode("false")
201
- @mb_obj.message["o:testmode"].length.should eq(1)
202
- @mb_obj.message["o:testmode"][0].should eq("no")
284
+ @mb_obj.test_mode("false")
285
+ @mb_obj.test_mode("true")
286
+ @mb_obj.test_mode("false")
287
+
288
+ expect(@mb_obj.message["o:testmode"].length).to eq(1)
289
+ expect(@mb_obj.message["o:testmode"][0]).to eq("no")
203
290
  end
204
291
  end
205
292
 
206
293
  describe 'The method set_dkim' do
294
+ it 'warns of set_dkim deprecation' do
295
+ @mb_obj = Mailgun::MessageBuilder.new
296
+ expect(@mb_obj).to receive :warn
297
+ @mb_obj.set_dkim 'warn on set_dkim'
298
+ end
299
+ end
300
+
301
+ describe 'The method dkim' do
207
302
  before(:each) do
208
303
  @mb_obj = Mailgun::MessageBuilder.new
209
304
  end
210
305
  it 'turns on dkim with boolean true' do
211
- @mb_obj.set_dkim(true)
212
- @mb_obj.message["o:dkim"][0].should eq("yes")
306
+ @mb_obj.dkim(true)
307
+
308
+ expect(@mb_obj.message["o:dkim"][0]).to eq("yes")
213
309
  end
214
310
  it 'turns on dkim with string true' do
215
- @mb_obj.set_dkim("true")
216
- @mb_obj.message["o:dkim"][0].should eq("yes")
311
+ @mb_obj.dkim("true")
312
+
313
+ expect(@mb_obj.message["o:dkim"][0]).to eq("yes")
217
314
  end
218
315
  it 'turns off dkim with boolean false' do
219
- @mb_obj.set_dkim(false)
220
- @mb_obj.message["o:dkim"][0].should eq("no")
316
+ @mb_obj.dkim(false)
317
+
318
+ expect(@mb_obj.message["o:dkim"][0]).to eq("no")
221
319
  end
222
320
  it 'turns off dkim with string false' do
223
- @mb_obj.set_dkim("false")
224
- @mb_obj.message["o:dkim"][0].should eq("no")
321
+ @mb_obj.dkim("false")
322
+
323
+ expect(@mb_obj.message["o:dkim"][0]).to eq("no")
225
324
  end
226
325
  it 'does not allow multiple values' do
227
- @mb_obj.set_dkim("false")
228
- @mb_obj.set_dkim("true")
229
- @mb_obj.set_dkim("false")
230
- @mb_obj.message["o:dkim"].length.should eq(1)
231
- @mb_obj.message["o:dkim"][0].should eq("no")
326
+ @mb_obj.dkim("false")
327
+ @mb_obj.dkim("true")
328
+ @mb_obj.dkim("false")
329
+
330
+ expect(@mb_obj.message["o:dkim"].length).to eq(1)
331
+ expect(@mb_obj.message["o:dkim"][0]).to eq("no")
232
332
  end
233
333
  end
234
334
 
@@ -238,15 +338,17 @@ describe 'The method add_campaign_id' do
238
338
  end
239
339
  it 'adds a campaign ID to the message' do
240
340
  @mb_obj.add_campaign_id('My-Campaign-Id-1')
241
- @mb_obj.message["o:campaign"][0].should eq ("My-Campaign-Id-1")
341
+
342
+ expect(@mb_obj.message["o:campaign"][0]).to eq ("My-Campaign-Id-1")
242
343
  end
243
344
  it 'adds a few more campaign IDs to the message' do
244
345
  @mb_obj.add_campaign_id('My-Campaign-Id-1')
245
346
  @mb_obj.add_campaign_id('My-Campaign-Id-2')
246
347
  @mb_obj.add_campaign_id('My-Campaign-Id-3')
247
- @mb_obj.message["o:campaign"][0].should eq("My-Campaign-Id-1")
248
- @mb_obj.message["o:campaign"][1].should eq("My-Campaign-Id-2")
249
- @mb_obj.message["o:campaign"][2].should eq("My-Campaign-Id-3")
348
+
349
+ expect(@mb_obj.message["o:campaign"][0]).to eq("My-Campaign-Id-1")
350
+ expect(@mb_obj.message["o:campaign"][1]).to eq("My-Campaign-Id-2")
351
+ expect(@mb_obj.message["o:campaign"][2]).to eq("My-Campaign-Id-3")
250
352
  end
251
353
  it 'adds too many campaign IDs to the message' do
252
354
  expect{
@@ -262,15 +364,17 @@ describe 'The method add_tag' do
262
364
  end
263
365
  it 'adds a tag to the message' do
264
366
  @mb_obj.add_tag('My-Tag-1')
265
- @mb_obj.message["o:tag"][0].should eq("My-Tag-1")
367
+
368
+ expect(@mb_obj.message["o:tag"][0]).to eq("My-Tag-1")
266
369
  end
267
370
  it 'adds a few more tags to the message' do
268
371
  @mb_obj.add_tag('My-Tag-1')
269
372
  @mb_obj.add_tag('My-Tag-2')
270
373
  @mb_obj.add_tag('My-Tag-3')
271
- @mb_obj.message["o:tag"][0].should eq("My-Tag-1")
272
- @mb_obj.message["o:tag"][1].should eq("My-Tag-2")
273
- @mb_obj.message["o:tag"][2].should eq("My-Tag-3")
374
+
375
+ expect(@mb_obj.message["o:tag"][0]).to eq("My-Tag-1")
376
+ expect(@mb_obj.message["o:tag"][1]).to eq("My-Tag-2")
377
+ expect(@mb_obj.message["o:tag"][2]).to eq("My-Tag-3")
274
378
  end
275
379
  it 'adds too many tags to the message' do
276
380
  expect{
@@ -281,65 +385,118 @@ describe 'The method add_tag' do
281
385
  end
282
386
 
283
387
  describe 'The method set_open_tracking' do
388
+ it 'warns of set_open_tracking deprecation' do
389
+ @mb_obj = Mailgun::MessageBuilder.new
390
+ expect(@mb_obj).to receive :warn
391
+ @mb_obj.set_open_tracking 'warn on set_open_tracking'
392
+ end
393
+ end
394
+
395
+ describe 'The method track_opens' do
284
396
  before(:each) do
285
397
  @mb_obj = Mailgun::MessageBuilder.new
286
398
  end
287
399
  it 'enables/disables open tracking on a per message basis.' do
288
- @mb_obj.set_open_tracking('Yes')
289
- @mb_obj.message["o:tracking-opens"][0].should eq("yes")
290
- @mb_obj.set_open_tracking('No')
291
- @mb_obj.message["o:tracking-opens"][0].should eq("no")
292
- @mb_obj.set_open_tracking(true)
293
- @mb_obj.message["o:tracking-opens"][0].should eq("yes")
294
- @mb_obj.set_open_tracking(false)
295
- @mb_obj.message["o:tracking-opens"][0].should eq("no")
400
+ @mb_obj.track_opens('Yes')
401
+
402
+ expect(@mb_obj.message["o:tracking-opens"][0]).to eq("yes")
403
+
404
+ @mb_obj.track_opens('No')
405
+
406
+ expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
407
+
408
+ @mb_obj.track_opens(true)
409
+
410
+ expect(@mb_obj.message["o:tracking-opens"][0]).to eq("yes")
411
+
412
+ @mb_obj.track_opens(false)
413
+
414
+ expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
296
415
  end
297
416
  end
298
417
 
299
418
  describe 'The method set_click_tracking' do
419
+ it 'warns of set_click_tracking deprecation' do
420
+ @mb_obj = Mailgun::MessageBuilder.new
421
+ expect(@mb_obj).to receive :warn
422
+ @mb_obj.set_click_tracking 'warn on set_click_tracking'
423
+ end
424
+ end
425
+
426
+ describe 'The method track_clicks' do
300
427
  before(:each) do
301
428
  @mb_obj = Mailgun::MessageBuilder.new
302
429
  end
303
430
  it 'enables/disables click tracking on a per message basis.' do
304
- @mb_obj.set_click_tracking('Yes')
305
- @mb_obj.message["o:tracking-clicks"][0].should eq("yes")
306
- @mb_obj.set_click_tracking('No')
307
- @mb_obj.message["o:tracking-clicks"][0].should eq("no")
308
- @mb_obj.set_click_tracking(true)
309
- @mb_obj.message["o:tracking-clicks"][0].should eq("yes")
310
- @mb_obj.set_click_tracking(false)
311
- @mb_obj.message["o:tracking-clicks"][0].should eq("no")
312
- @mb_obj.set_click_tracking('html')
313
- @mb_obj.message["o:tracking-clicks"][0].should eq("html")
431
+ @mb_obj.track_clicks('Yes')
432
+
433
+ expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("yes")
434
+
435
+ @mb_obj.track_clicks('No')
436
+
437
+ expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
438
+
439
+ @mb_obj.track_clicks(true)
440
+
441
+ expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("yes")
442
+
443
+ @mb_obj.track_clicks(false)
444
+
445
+ expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
446
+
447
+ @mb_obj.track_clicks('html')
448
+
449
+ expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("html")
314
450
  end
315
451
  end
316
452
 
317
453
  describe 'The method set_delivery_time' do
454
+ it 'warns of set_delivery_time deprecation' do
455
+ @mb_obj = Mailgun::MessageBuilder.new
456
+ expect(@mb_obj).to receive :warn
457
+ @mb_obj.set_delivery_time 'October 25, 2013 10:00PM CST'
458
+ end
459
+ end
460
+
461
+ describe 'The method deliver_at' do
318
462
  before(:each) do
319
463
  @mb_obj = Mailgun::MessageBuilder.new
320
464
  end
321
465
  it 'defines a time/date to deliver a message in RFC2822 format.' do
322
- @mb_obj.set_delivery_time('October 25, 2013 10:00PM CST')
323
- @mb_obj.message["o:deliverytime"][0].should eq("Fri, 25 Oct 2013 22:00:00 -0600")
466
+ @mb_obj.deliver_at('October 25, 2013 10:00PM CST')
467
+
468
+ expect(@mb_obj.message["o:deliverytime"][0]).to eq("Fri, 25 Oct 2013 22:00:00 -0600")
324
469
  end
325
470
  end
326
471
 
327
472
  describe 'The method set_custom_data' do
473
+ it 'warns of set_custom_data deprecation' do
474
+ @mb_obj = Mailgun::MessageBuilder.new
475
+ expect(@mb_obj).to receive :warn
476
+ @mb_obj.set_custom_data 'my-data', '{"key":"value"}'
477
+ end
478
+ end
479
+
480
+ describe 'The method header' do
328
481
  before(:each) do
329
482
  @mb_obj = Mailgun::MessageBuilder.new
330
483
  end
331
484
  it 'accepts valid JSON and appends as data to the message.' do
332
- @mb_obj.set_custom_data('my-data', '{"key":"value"}')
333
- @mb_obj.message["v:my-data"][0].should eq("{\"key\":\"value\"}")
485
+ @mb_obj.header('my-data', '{"key":"value"}')
486
+
487
+ expect(@mb_obj.message["v:my-data"][0]).to be_kind_of(String)
488
+ expect(@mb_obj.message["v:my-data"][0].to_s).to eq('{"key"=>"value"}')
334
489
  end
335
490
  it 'accepts a hash and appends as data to the message.' do
336
- data = {'key'=> 'value'}
337
- @mb_obj.set_custom_data('my-data', data)
338
- @mb_obj.message["v:my-data"][0].should eq("{\"key\":\"value\"}")
491
+ data = {'key' => 'value'}
492
+ @mb_obj.header('my-data', data)
493
+
494
+ expect(@mb_obj.message["v:my-data"][0]).to be_kind_of(String)
495
+ expect(@mb_obj.message["v:my-data"][0].to_s).to eq('{"key"=>"value"}')
339
496
  end
340
497
  it 'throws an exception on broken JSON.' do
341
498
  data = 'This is some crappy JSON.'
342
- expect {@mb_obj.set_custom_data('my-data', data)}.to raise_error(Mailgun::ParameterError)
499
+ expect {@mb_obj.header('my-data', data)}.to raise_error(Mailgun::ParameterError)
343
500
  end
344
501
  end
345
502
 
@@ -349,27 +506,41 @@ describe 'The method add_custom_parameter' do
349
506
  end
350
507
  it 'adds an undefined parameter to the message.' do
351
508
  @mb_obj.add_custom_parameter('h:my-sweet-header', 'datagoeshere')
352
- @mb_obj.message["h:my-sweet-header"][0].should eq("datagoeshere")
509
+
510
+ expect(@mb_obj.message["h:my-sweet-header"][0]).to eq("datagoeshere")
353
511
  end
354
512
  end
355
513
 
356
514
  describe 'The method set_message_id' do
515
+ it 'warns of set_message_id deprecation' do
516
+ @mb_obj = Mailgun::MessageBuilder.new
517
+ expect(@mb_obj).to receive :warn
518
+ @mb_obj.set_message_id 'warn on set_message_id'
519
+ end
520
+ end
521
+
522
+ describe 'The method message_id' do
357
523
  before(:each) do
358
524
  @mb_obj = Mailgun::MessageBuilder.new
359
525
  @the_message_id = '<20141014000000.11111.11111@example.com>'
360
526
  end
361
527
  it 'correctly sets the Message-Id header' do
362
- @mb_obj.set_message_id(@the_message_id)
363
- @mb_obj.message['h:Message-Id'].should eq(@the_message_id)
528
+ @mb_obj.message_id(@the_message_id)
529
+
530
+ expect(@mb_obj.message['h:Message-Id']).to eq(@the_message_id)
364
531
  end
365
532
  it 'correctly clears the Message-Id header when passed nil' do
366
- @mb_obj.set_message_id(nil)
367
- @mb_obj.message.has_key?('h:Message-Id').should eq(false)
533
+ @mb_obj.message_id(nil)
534
+
535
+ expect(@mb_obj.message.has_key?('h:Message-Id')).to eq(false)
368
536
  end
369
537
  it 'correctly sets the Message-Id header when passed an empty string' do
370
- @mb_obj.set_message_id(@the_message_id)
371
- @mb_obj.message.has_key?('h:Message-Id').should eq(true)
372
- @mb_obj.set_message_id('')
373
- @mb_obj.message.has_key?('h:Message-Id').should eq(false)
538
+ @mb_obj.message_id(@the_message_id)
539
+
540
+ expect(@mb_obj.message.has_key?('h:Message-Id')).to eq(true)
541
+
542
+ @mb_obj.message_id('')
543
+
544
+ expect(@mb_obj.message.has_key?('h:Message-Id')).to eq(false)
374
545
  end
375
546
  end