wj-mailgun-ruby 1.1.7

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