mailgun-ruby 1.0.3 → 1.1.0
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.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +8 -0
- data/.rubocop_todo.yml +22 -0
- data/.ruby-env.yml.example +12 -0
- data/.travis.yml +6 -12
- data/Domains.md +36 -0
- data/MessageBuilder.md +14 -14
- data/Messages.md +44 -30
- data/OptInHandler.md +34 -34
- data/README.md +74 -24
- data/Rakefile +22 -20
- data/Snippets.md +26 -26
- data/Webhooks.md +40 -0
- data/lib/mailgun.rb +26 -228
- data/lib/mailgun/chains.rb +16 -0
- data/lib/mailgun/client.rb +143 -0
- data/lib/mailgun/domains/domains.rb +84 -0
- data/lib/mailgun/events/events.rb +53 -35
- data/lib/mailgun/exceptions/exceptions.rb +43 -10
- data/lib/mailgun/lists/opt_in_handler.rb +18 -19
- data/lib/mailgun/messages/batch_message.rb +31 -48
- data/lib/mailgun/messages/message_builder.rb +160 -144
- data/lib/mailgun/response.rb +55 -0
- data/lib/mailgun/version.rb +2 -3
- data/lib/mailgun/webhooks/webhooks.rb +101 -0
- data/mailgun.gemspec +16 -10
- data/spec/integration/bounces_spec.rb +44 -0
- data/spec/integration/campaign_spec.rb +60 -0
- data/spec/integration/complaints_spec.rb +38 -0
- data/spec/integration/domains_spec.rb +39 -0
- data/spec/integration/email_validation_spec.rb +29 -0
- data/spec/integration/events_spec.rb +20 -0
- data/spec/integration/list_members_spec.rb +63 -0
- data/spec/integration/list_spec.rb +58 -0
- data/spec/integration/mailgun_spec.rb +26 -550
- data/spec/integration/routes_spec.rb +74 -0
- data/spec/integration/stats_spec.rb +15 -0
- data/spec/integration/unsubscribes_spec.rb +42 -0
- data/spec/integration/webhook_spec.rb +54 -0
- data/spec/spec_helper.rb +37 -7
- data/spec/unit/connection/test_client.rb +15 -95
- data/spec/unit/events/events_spec.rb +9 -6
- data/spec/unit/lists/opt_in_handler_spec.rb +6 -4
- data/spec/unit/mailgun_spec.rb +25 -19
- data/spec/unit/messages/batch_message_spec.rb +47 -38
- data/spec/unit/messages/message_builder_spec.rb +282 -111
- data/vcr_cassettes/bounces.yml +175 -0
- data/vcr_cassettes/complaints.yml +175 -0
- data/vcr_cassettes/domains.todo.yml +42 -0
- data/vcr_cassettes/domains.yml +360 -0
- data/vcr_cassettes/email_validation.yml +104 -0
- data/vcr_cassettes/events.yml +61 -0
- data/vcr_cassettes/list_members.yml +320 -0
- data/vcr_cassettes/mailing_list.todo.yml +43 -0
- data/vcr_cassettes/mailing_list.yml +390 -0
- data/vcr_cassettes/routes.yml +359 -0
- data/vcr_cassettes/send_message.yml +107 -0
- data/vcr_cassettes/stats.yml +44 -0
- data/vcr_cassettes/unsubscribes.yml +191 -0
- data/vcr_cassettes/webhooks.yml +276 -0
- metadata +114 -10
data/spec/unit/mailgun_spec.rb
CHANGED
@@ -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
|
-
|
35
|
-
result.body.
|
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
|
-
|
45
|
-
result.body.
|
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
|
-
|
63
|
-
result.body.
|
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
|
-
|
82
|
-
result.body
|
83
|
-
result.body["member"]
|
84
|
-
result.body["member"].
|
85
|
-
result.body["member"].
|
86
|
-
result.body["member"].
|
87
|
-
result.body.
|
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
|
-
|
104
|
-
result.body.
|
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
|
-
|
119
|
-
result.body.
|
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
|
-
|
8
|
-
@mb_obj.
|
9
|
-
@mb_obj.
|
10
|
-
@mb_obj.
|
11
|
-
@mb_obj.
|
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.
|
24
|
-
@mb_obj.message.length.
|
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.
|
29
|
-
@mb_obj.recipient_variables.length.
|
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.
|
34
|
-
@mb_obj.domain.
|
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.
|
39
|
-
@mb_obj.message_ids.length.
|
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.
|
44
|
-
@mb_obj.counters.
|
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.
|
49
|
-
|
50
|
-
@mb_obj.counters.
|
51
|
-
@mb_obj.counters[:recipients].
|
52
|
-
@mb_obj.counters[:recipients].
|
53
|
-
@mb_obj.counters[:recipients].
|
54
|
-
|
55
|
-
@mb_obj.counters.
|
56
|
-
@mb_obj.counters[:attributes].
|
57
|
-
@mb_obj.counters[:attributes].
|
58
|
-
@mb_obj.counters[:attributes].
|
59
|
-
@mb_obj.counters[:attributes].
|
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
|
-
|
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
|
-
|
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
|
-
|
94
|
+
|
95
|
+
expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
|
91
96
|
@mb_obj.finalize
|
92
|
-
|
93
|
-
@mb_obj.message[
|
94
|
-
@mb_obj.
|
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
|
-
|
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
|
-
|
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
|
-
|
118
|
-
@mb_obj.recipient_variables[@
|
119
|
-
@mb_obj.recipient_variables[@
|
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
|
-
|
7
|
-
@mb_obj.
|
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.
|
19
|
-
@mb_obj.counters.
|
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.
|
24
|
-
@mb_obj.counters.
|
25
|
-
@mb_obj.counters[:recipients].
|
26
|
-
@mb_obj.counters[:recipients].
|
27
|
-
@mb_obj.counters[:recipients].
|
28
|
-
@mb_obj.counters.
|
29
|
-
@mb_obj.counters[:attributes].
|
30
|
-
@mb_obj.counters[:attributes].
|
31
|
-
@mb_obj.counters[:attributes].
|
32
|
-
@mb_obj.counters[:attributes].
|
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
|
-
|
47
|
-
@mb_obj.
|
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
|
-
|
54
|
-
@mb_obj.
|
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
|
-
|
61
|
-
@mb_obj.
|
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
|
-
|
68
|
-
@mb_obj.
|
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
|
-
|
75
|
-
@mb_obj.
|
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.
|
106
|
-
|
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.
|
111
|
-
|
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.
|
117
|
-
@mb_obj.
|
118
|
-
|
119
|
-
@mb_obj.message[: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
|
128
|
-
@mb_obj.
|
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
|
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.
|
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.
|
140
|
-
@mb_obj.
|
141
|
-
|
142
|
-
@mb_obj.message[: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
|
-
|
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
|
-
|
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.
|
183
|
-
|
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.
|
187
|
-
|
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.
|
191
|
-
|
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.
|
195
|
-
|
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.
|
199
|
-
@mb_obj.
|
200
|
-
@mb_obj.
|
201
|
-
|
202
|
-
@mb_obj.message["o:testmode"]
|
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.
|
212
|
-
|
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.
|
216
|
-
|
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.
|
220
|
-
|
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.
|
224
|
-
|
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.
|
228
|
-
@mb_obj.
|
229
|
-
@mb_obj.
|
230
|
-
|
231
|
-
@mb_obj.message["o:dkim"]
|
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
|
-
|
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
|
-
|
248
|
-
@mb_obj.message["o:campaign"][
|
249
|
-
@mb_obj.message["o:campaign"][
|
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
|
-
|
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
|
-
|
272
|
-
@mb_obj.message["o:tag"][
|
273
|
-
@mb_obj.message["o:tag"][
|
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.
|
289
|
-
|
290
|
-
@mb_obj.
|
291
|
-
|
292
|
-
@mb_obj.
|
293
|
-
|
294
|
-
@mb_obj.
|
295
|
-
|
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.
|
305
|
-
|
306
|
-
@mb_obj.
|
307
|
-
|
308
|
-
@mb_obj.
|
309
|
-
|
310
|
-
@mb_obj.
|
311
|
-
|
312
|
-
@mb_obj.
|
313
|
-
|
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.
|
323
|
-
|
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.
|
333
|
-
|
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.
|
338
|
-
|
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.
|
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
|
-
|
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.
|
363
|
-
|
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.
|
367
|
-
|
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.
|
371
|
-
|
372
|
-
@mb_obj.
|
373
|
-
|
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
|