mailgun-ruby 1.4.5 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -1
  3. data/README.md +3 -1
  4. data/lib/mailgun/api_keys/api_keys.rb +2 -2
  5. data/lib/mailgun/client.rb +1 -1
  6. data/lib/mailgun/domains/domain_keys.rb +5 -5
  7. data/lib/mailgun/domains/domains.rb +12 -9
  8. data/lib/mailgun/events/events.rb +1 -1
  9. data/lib/mailgun/helpers/api_version_checker.rb +23 -12
  10. data/lib/mailgun/helpers/deprecation.rb +32 -0
  11. data/lib/mailgun/lists/mailing_lists.rb +250 -0
  12. data/lib/mailgun/messages/message_builder.rb +17 -7
  13. data/lib/mailgun/response.rb +1 -1
  14. data/lib/mailgun/routes/routes.rb +98 -0
  15. data/lib/mailgun/tags/analytics_tags.rb +2 -2
  16. data/lib/mailgun/tags/tags.rb +16 -8
  17. data/lib/mailgun/version.rb +1 -1
  18. data/lib/mailgun/webhooks/account_webhooks.rb +2 -2
  19. data/lib/mailgun/webhooks/webhooks.rb +17 -9
  20. data/lib/mailgun.rb +1 -0
  21. metadata +6 -326
  22. data/.github/workflows/ci.yml +0 -49
  23. data/.github_changelog_generator +0 -1
  24. data/.gitignore +0 -24
  25. data/.rubocop.yml +0 -68
  26. data/.ruby-env.yml.example +0 -13
  27. data/Gemfile +0 -9
  28. data/Rakefile +0 -32
  29. data/docs/AccountWebhooks.md +0 -55
  30. data/docs/AnalyticsTags.md +0 -63
  31. data/docs/ApiKeys.md +0 -65
  32. data/docs/DomainKeys.md +0 -49
  33. data/docs/Domains.md +0 -82
  34. data/docs/EmailValidation.md +0 -34
  35. data/docs/Events.md +0 -48
  36. data/docs/Logs.md +0 -67
  37. data/docs/MessageBuilder.md +0 -120
  38. data/docs/Metrics.md +0 -108
  39. data/docs/OptInHandler.md +0 -103
  40. data/docs/Snippets.md +0 -519
  41. data/docs/Subaccounts.md +0 -68
  42. data/docs/Suppressions.md +0 -92
  43. data/docs/Templates.md +0 -92
  44. data/docs/Webhooks.md +0 -65
  45. data/docs/railgun/EmailValidation.md +0 -34
  46. data/docs/railgun/Overview.md +0 -11
  47. data/docs/railgun/Parameters.md +0 -83
  48. data/mailgun.gemspec +0 -47
  49. data/spec/integration/account_webhooks_spec.rb +0 -57
  50. data/spec/integration/analytics_tags_spec.rb +0 -54
  51. data/spec/integration/api_keys_spec.rb +0 -50
  52. data/spec/integration/bounces_spec.rb +0 -45
  53. data/spec/integration/campaign_spec.rb +0 -62
  54. data/spec/integration/complaints_spec.rb +0 -40
  55. data/spec/integration/domain_keys_spec.rb +0 -124
  56. data/spec/integration/domains_spec.rb +0 -201
  57. data/spec/integration/email_validation_spec.rb +0 -73
  58. data/spec/integration/events_spec.rb +0 -28
  59. data/spec/integration/list_members_spec.rb +0 -64
  60. data/spec/integration/list_spec.rb +0 -59
  61. data/spec/integration/logs_spec.rb +0 -105
  62. data/spec/integration/mailer_spec.rb +0 -65
  63. data/spec/integration/mailgun_spec.rb +0 -198
  64. data/spec/integration/messages/sample_data/mime.txt +0 -38
  65. data/spec/integration/metrics_spec.rb +0 -224
  66. data/spec/integration/routes_spec.rb +0 -91
  67. data/spec/integration/stats_spec.rb +0 -17
  68. data/spec/integration/subaccounts_spec.rb +0 -57
  69. data/spec/integration/suppressions_spec.rb +0 -320
  70. data/spec/integration/templates_spec.rb +0 -137
  71. data/spec/integration/unsubscribes_spec.rb +0 -44
  72. data/spec/integration/webhook_spec.rb +0 -79
  73. data/spec/spec_helper.rb +0 -53
  74. data/spec/unit/client_spec.rb +0 -448
  75. data/spec/unit/connection/test_client.rb +0 -169
  76. data/spec/unit/events/events_spec.rb +0 -88
  77. data/spec/unit/exceptions/exceptions_spec.rb +0 -31
  78. data/spec/unit/helpers/api_version_checker_spec.rb +0 -206
  79. data/spec/unit/lists/opt_in_handler_spec.rb +0 -28
  80. data/spec/unit/mailgun_spec.rb +0 -171
  81. data/spec/unit/messages/batch_message_spec.rb +0 -148
  82. data/spec/unit/messages/message_builder_spec.rb +0 -745
  83. data/spec/unit/messages/sample_data/mailgun_icon.png +0 -0
  84. data/spec/unit/messages/sample_data/mime.txt +0 -38
  85. data/spec/unit/messages/sample_data/rackspace_logo.jpg +0 -0
  86. data/spec/unit/messages/sample_data/unknown.type +0 -0
  87. data/spec/unit/railgun/content_type_spec.rb +0 -89
  88. data/spec/unit/railgun/mailer_spec.rb +0 -391
  89. data/spec/unit/response_spec.rb +0 -225
  90. data/spec/unit/webhooks_spec.rb +0 -121
  91. data/vcr_cassettes/For_the_suppressions_handling_class/creates_a_single_bounce.yml +0 -55
  92. data/vcr_cassettes/account_webhooks.yml +0 -269
  93. data/vcr_cassettes/analytics_tags.yml +0 -187
  94. data/vcr_cassettes/api_keys.yml +0 -212
  95. data/vcr_cassettes/bounces.yml +0 -136
  96. data/vcr_cassettes/complaints.yml +0 -315
  97. data/vcr_cassettes/domains.todo.yml +0 -42
  98. data/vcr_cassettes/domains.yml +0 -1243
  99. data/vcr_cassettes/email_validation.yml +0 -152
  100. data/vcr_cassettes/events.yml +0 -69
  101. data/vcr_cassettes/exceptions-invalid-api-key.yml +0 -52
  102. data/vcr_cassettes/exceptions-invalid-data.yml +0 -52
  103. data/vcr_cassettes/exceptions-not-allowed.yml +0 -54
  104. data/vcr_cassettes/exceptions.yml +0 -92
  105. data/vcr_cassettes/list_members.yml +0 -328
  106. data/vcr_cassettes/logs.yml +0 -53
  107. data/vcr_cassettes/mailer_invalid_domain.yml +0 -109
  108. data/vcr_cassettes/mailing_list.todo.yml +0 -43
  109. data/vcr_cassettes/mailing_list.yml +0 -682
  110. data/vcr_cassettes/message_deliver.yml +0 -149
  111. data/vcr_cassettes/metrics.yml +0 -116
  112. data/vcr_cassettes/routes.yml +0 -359
  113. data/vcr_cassettes/send_message.yml +0 -158
  114. data/vcr_cassettes/stats.yml +0 -48
  115. data/vcr_cassettes/subaccounts.yml +0 -270
  116. data/vcr_cassettes/suppressions.yml +0 -1392
  117. data/vcr_cassettes/templates.yml +0 -1065
  118. data/vcr_cassettes/unsubscribes.yml +0 -401
  119. data/vcr_cassettes/webhooks.yml +0 -246
  120. data/vcr_cassettes/webhooks_v4.yml +0 -275
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Mailgun::CommunicationError do
6
- describe '.new' do
7
- context "when the Response body doesn't have a `message` property" do
8
- it "doesn't raise an error" do
9
- expect do
10
- described_class.new('Boom!', { status: 401, body: '{}' })
11
- end.not_to raise_error
12
- end
13
-
14
- context 'when the Response body has an `Error` property' do
15
- it 'uses the `Error` property as the API message' do
16
- subject = described_class.new('Boom!', { status: 401, body: '{"Error":"unauthorized"}' })
17
-
18
- expect(subject.message).to eq('Boom!: unauthorized')
19
- end
20
- end
21
-
22
- context 'when the Response body has an `error` property' do
23
- it 'uses the `Error` property as the API message' do
24
- subject = described_class.new('Boom!', { status: 401, body: '{"error":"not found"}' })
25
-
26
- expect(subject.message).to eq('Boom!: not found')
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,206 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- require 'mailgun'
6
- require 'mailgun/helpers/api_version_checker'
7
-
8
- describe Mailgun::ApiVersionChecker do
9
- # Build a minimal host class that includes the module, with a controllable
10
- # @client so we can set api_version per example.
11
- subject(:instance) { host_class.new(client) }
12
-
13
- let(:client) { double(:client) }
14
-
15
- let(:host_class) do
16
- Class.new do
17
- include Mailgun::ApiVersionChecker
18
-
19
- attr_reader :result
20
-
21
- def initialize(client)
22
- @client = client
23
- end
24
-
25
- def do_something(arg = nil)
26
- @result = arg || :called
27
- end
28
- end
29
- end
30
-
31
- # ──────────────────────────────────────────────────────────────────────────
32
- # .included / module structure
33
- # ──────────────────────────────────────────────────────────────────────────
34
-
35
- describe '.included' do
36
- it 'extends the including class with ClassMethods' do
37
- expect(host_class).to respond_to(:requires_api_version)
38
- expect(host_class).to respond_to(:enforces_api_version)
39
- end
40
-
41
- it 'does not expose ClassMethods as instance methods' do
42
- expect(instance).not_to respond_to(:requires_api_version)
43
- expect(instance).not_to respond_to(:enforces_api_version)
44
- end
45
- end
46
-
47
- # ──────────────────────────────────────────────────────────────────────────
48
- # .requires_api_version (warns but still calls through)
49
- # ──────────────────────────────────────────────────────────────────────────
50
-
51
- describe '.requires_api_version' do
52
- before { host_class.requires_api_version('v3', :do_something) }
53
-
54
- context 'when the client is on the expected version' do
55
- before { allow(client).to receive(:api_version).and_return('v3') }
56
-
57
- it 'does not emit a warning' do
58
- expect { instance.do_something }.not_to output.to_stderr
59
- end
60
-
61
- it 'still calls the original method' do
62
- instance.do_something(:payload)
63
- expect(instance.result).to eq(:payload)
64
- end
65
-
66
- it 'forwards all arguments to the original method' do
67
- instance.do_something(:forwarded_arg)
68
- expect(instance.result).to eq(:forwarded_arg)
69
- end
70
- end
71
-
72
- context 'when the client is on a different version' do
73
- before { allow(client).to receive(:api_version).and_return('v4') }
74
-
75
- it 'emits a warning to stderr' do
76
- expect { instance.do_something }
77
- .to output(/WARN: Client api version must be v3/).to_stderr
78
- end
79
-
80
- it 'still calls the original method despite the warning' do
81
- instance.do_something(:payload)
82
- expect(instance.result).to eq(:payload)
83
- end
84
-
85
- it 'warning message includes the expected version' do
86
- expect { instance.do_something }
87
- .to output(/v3/).to_stderr
88
- end
89
- end
90
-
91
- it 'wraps multiple methods when given a list' do
92
- host_class.class_eval { def do_other = @result = :other }
93
- host_class.requires_api_version('v3', :do_something, :do_other)
94
-
95
- allow(client).to receive(:api_version).and_return('v4')
96
-
97
- expect { instance.do_something }.to output(/v3/).to_stderr
98
- expect { instance.do_other }.to output(/v3/).to_stderr
99
- end
100
-
101
- it 'wraps only the listed methods, leaving others unwrapped' do
102
- host_class.class_eval { def unwrapped = @result = :unwrapped }
103
- host_class.requires_api_version('v3', :do_something)
104
-
105
- allow(client).to receive(:api_version).and_return('v4')
106
-
107
- # unwrapped should not warn
108
- expect { instance.unwrapped }.not_to output.to_stderr
109
- end
110
- end
111
-
112
- # ──────────────────────────────────────────────────────────────────────────
113
- # .enforces_api_version (raises on version mismatch)
114
- # ──────────────────────────────────────────────────────────────────────────
115
-
116
- describe '.enforces_api_version' do
117
- before { host_class.enforces_api_version('v3', :do_something) }
118
-
119
- context 'when the client is on the expected version' do
120
- before { allow(client).to receive(:api_version).and_return('v3') }
121
-
122
- it 'does not raise' do
123
- expect { instance.do_something }.not_to raise_error
124
- end
125
-
126
- it 'calls the original method' do
127
- instance.do_something(:payload)
128
- expect(instance.result).to eq(:payload)
129
- end
130
-
131
- it 'forwards all arguments to the original method' do
132
- instance.do_something(:forwarded_arg)
133
- expect(instance.result).to eq(:forwarded_arg)
134
- end
135
- end
136
-
137
- context 'when the client is on a different version' do
138
- before { allow(client).to receive(:api_version).and_return('v4') }
139
-
140
- it 'raises Mailgun::ParameterError' do
141
- expect { instance.do_something }
142
- .to raise_error(Mailgun::ParameterError)
143
- end
144
-
145
- it 'raises with a message including the expected version' do
146
- expect { instance.do_something }
147
- .to raise_error(Mailgun::ParameterError, /v3/)
148
- end
149
-
150
- it 'does not call the original method' do
151
- begin
152
- instance.do_something
153
- rescue StandardError
154
- nil
155
- end
156
- expect(instance.result).to be_nil
157
- end
158
- end
159
-
160
- it 'wraps multiple methods when given a list' do
161
- host_class.class_eval { def do_other = @result = :other }
162
- host_class.enforces_api_version('v3', :do_something, :do_other)
163
-
164
- allow(client).to receive(:api_version).and_return('v4')
165
-
166
- expect { instance.do_something }.to raise_error(Mailgun::ParameterError)
167
- expect { instance.do_other }.to raise_error(Mailgun::ParameterError)
168
- end
169
-
170
- it 'wraps only the listed methods, leaving others unwrapped' do
171
- host_class.class_eval { def unwrapped = @result = :unwrapped }
172
- host_class.enforces_api_version('v3', :do_something)
173
-
174
- allow(client).to receive(:api_version).and_return('v4')
175
-
176
- expect { instance.unwrapped }.not_to raise_error
177
- end
178
- end
179
-
180
- # ──────────────────────────────────────────────────────────────────────────
181
- # requires vs enforces contrast
182
- # ──────────────────────────────────────────────────────────────────────────
183
-
184
- describe 'requires_api_version vs enforces_api_version' do
185
- before do
186
- host_class.class_eval do
187
- def soft_method = @result = :soft
188
- def hard_method = @result = :hard
189
- end
190
- host_class.requires_api_version('v3', :soft_method)
191
- host_class.enforces_api_version('v3', :hard_method)
192
-
193
- allow(client).to receive(:api_version).and_return('v4')
194
- end
195
-
196
- it 'requires_api_version warns but does not raise' do
197
- expect { instance.soft_method }.to output(/v3/).to_stderr
198
- expect(instance.result).to eq(:soft)
199
- end
200
-
201
- it 'enforces_api_version raises and does not call through' do
202
- expect { instance.hard_method }.to raise_error(Mailgun::ParameterError)
203
- expect(instance.result).not_to eq(:hard)
204
- end
205
- end
206
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe 'The method generate_hash' do
6
- before do
7
- @mailing_list = 'mylist@example.com'
8
- @secret_app_id = 'mysupersecretpassword'
9
- @recipient_address = 'bob@example.com'
10
- @precalculated_hash =
11
- "eyJoIjoiMmY3ZmY1MzFlOGJmMjA0OWNhMTI3ZmU4ZTQyNjZkOTljYzhkMTdk%0AMiIsInAiOiJleUpzSWpv\
12
- aWJYbHNhWE4wUUdWNFlXMXdiR1V1WTI5dElpd2lj%0AaUk2SW1KdllrQmxlR0Z0Y0d4bExtTnZcbmJTSjlcbiJ9%0A"
13
- end
14
-
15
- it 'generates a web safe hash for the recipient wishing to subscribe' do
16
- my_hash = Mailgun::OptInHandler.generate_hash(@mailing_list, @secret_app_id, @recipient_address)
17
-
18
- expect(my_hash).to eq(@precalculated_hash)
19
- end
20
-
21
- it 'generates a web safe hash for the recipient wishing to subscribe' do
22
- validate_result = Mailgun::OptInHandler.validate_hash(@secret_app_id, @precalculated_hash)
23
-
24
- expect(validate_result.length).to eq(2)
25
- expect(validate_result['recipient_address']).to eq(@recipient_address)
26
- expect(validate_result['mailing_list']).to eq(@mailing_list)
27
- end
28
- end
@@ -1,171 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe 'Mailgun instantiation' do
6
- it 'instantiates an HttpClient object' do
7
- expect { @mg_obj = Mailgun::UnitClient.new('messages') }.not_to raise_error
8
- end
9
- end
10
-
11
- describe 'The method send_message()' do
12
- before do
13
- @mg_obj = Mailgun::UnitClient.new('messages')
14
- @domain = 'test.com'
15
- @list_address = 'mylist@test.com'
16
- @member_address = 'subscribee@test.com'
17
- end
18
-
19
- it 'accepts only specific data types' do
20
- @mb_obj = Mailgun::MessageBuilder.new
21
- @mh_obj = {}
22
-
23
- expect { @mg_obj.send_message('test.com', 'incorrect data') }.to raise_error Mailgun::ParameterError
24
- expect { @mg_obj.send_message('test.com', @mb_obj) }.not_to raise_error
25
- expect { @mg_obj.send_message('test.com', @mh_obj) }.not_to raise_error
26
- end
27
-
28
- it 'sends a message' do
29
- data = { 'from' => 'joe@test.com',
30
- 'to' => 'bob@example.com',
31
- 'subject' => 'Test',
32
- 'text' => 'Test Data' }
33
- result = @mg_obj.send_message('testdomain.com', data)
34
-
35
- result.to_h!
36
-
37
- expect(result.body).to include('message')
38
- expect(result.body).to include('id')
39
- end
40
-
41
- it 'opens the message MIME and sends the MIME message.' do
42
- data = {
43
- 'to' => 'joe@test.com',
44
- 'message' => 'Sample Data/mime.txt',
45
- 'from' => 'joe@test.com'
46
- }
47
- result = @mg_obj.send_message('testdomain.com', data)
48
-
49
- result.to_h!
50
-
51
- expect(result.body).to include('message')
52
- expect(result.body).to include('id')
53
- end
54
-
55
- context 'when domain is missing' do
56
- it 'shows failure message' do
57
- expect(@mg_obj).to receive(:raise)
58
- @mg_obj.send_message(nil, {})
59
- end
60
- end
61
-
62
- context 'when to is missing' do
63
- it 'shows failure message' do
64
- data = {
65
- 'to' => '',
66
- 'message' => 'Sample Data/mime.txt',
67
- 'from' => 'joe@test.com'
68
- }
69
- expect(@mg_obj).to receive(:raise)
70
- @mg_obj.send_message('testdomain.com', data)
71
- end
72
- end
73
- end
74
-
75
- describe 'The method post()' do
76
- before do
77
- @mg_obj = Mailgun::UnitClient.new('messages')
78
- @domain = 'test.com'
79
- end
80
-
81
- it 'in this case, sends a simple message.' do
82
- data = { 'from' => 'joe@test.com',
83
- 'to' => 'bob@example.com',
84
- 'subject' => 'Test',
85
- 'text' => 'Test Data' }
86
- result = @mg_obj.post("#{@domain}/messages", data)
87
-
88
- result.to_h!
89
-
90
- expect(result.body).to include('message')
91
- expect(result.body).to include('id')
92
- end
93
-
94
- context 'when Unknown API error is raised' do
95
- before do
96
- allow(Mailgun::Response).to receive(:new).and_raise(StandardError, 'message')
97
- allow(JSON).to receive(:parse).and_raise('Unknown')
98
- end
99
-
100
- it 'adds Unknown API error to message' do
101
- data = { 'from' => 'joe@test.com',
102
- 'to' => 'bob@example.com',
103
- 'subject' => 'Test',
104
- 'text' => 'Test Data' }
105
- @mg_obj.post("#{@domain}/messages", data)
106
- rescue Mailgun::CommunicationError => e
107
- expect(e.message).to eq('message: Unknown API error')
108
- else
109
- raise
110
- end
111
- end
112
- end
113
-
114
- describe 'The method put()' do
115
- before do
116
- @mg_obj = Mailgun::UnitClient.new('lists')
117
- @domain = 'test.com'
118
- @list_address = 'mylist@test.com'
119
- @member_address = 'subscribee@test.com'
120
- end
121
-
122
- it 'in this case, updates a member with the attributes in data.' do
123
- data = { 'subscribed' => false,
124
- 'name' => 'Foo Bar' }
125
- result = @mg_obj.put("lists/#{@list_address}/members#{@member_address}", data)
126
-
127
- result.to_h!
128
-
129
- expect(result.body).to include('member')
130
- expect(result.body['member']).to include('vars')
131
- expect(result.body['member']['vars']).to include('age')
132
- expect(result.body['member']).to include('name')
133
- expect(result.body['member']).to include('subscribed')
134
- expect(result.body['member']).to include('address')
135
- expect(result.body).to include('message')
136
- end
137
- end
138
-
139
- describe 'The method get()' do
140
- before do
141
- @mg_obj = Mailgun::UnitClient.new('bounces')
142
- @domain = 'test.com'
143
- end
144
-
145
- it 'in this case, obtains a list of bounces for the domain, limit of 5, skipping the first 10.' do
146
- query_string = { 'skip' => '10',
147
- 'limit' => '5' }
148
- result = @mg_obj.get("#{@domain}/bounces", query_string)
149
-
150
- result.to_h!
151
-
152
- expect(result.body).to include('total_count')
153
- expect(result.body).to include('items')
154
- end
155
- end
156
-
157
- describe 'The method delete()' do
158
- before do
159
- @mg_obj = Mailgun::UnitClient.new('campaigns')
160
- @domain = 'test.com'
161
- end
162
-
163
- it 'issues a generic delete request.' do
164
- result = @mg_obj.delete("#{@domain}/campaigns/ABC123")
165
-
166
- result.to_h!
167
-
168
- expect(result.body).to include('message')
169
- expect(result.body).to include('id')
170
- end
171
- end
@@ -1,148 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe 'BatchMessage attribute readers' do
6
- it 'is readable' do
7
- @mb_client = Mailgun::UnitClient.new('messages')
8
- @mb_obj = Mailgun::BatchMessage.new(@mb_client, 'example.com')
9
-
10
- expect(@mb_obj).to respond_to(:message_ids)
11
- expect(@mb_obj).to respond_to(:message)
12
- expect(@mb_obj).to respond_to(:counters)
13
- expect(@mb_obj).to respond_to(:recipient_variables)
14
- expect(@mb_obj).to respond_to(:domain)
15
- end
16
- end
17
-
18
- describe 'The instantiation of Batch Message' do
19
- before do
20
- @mb_client = Mailgun::UnitClient.new('messages')
21
- @mb_obj = Mailgun::BatchMessage.new(@mb_client, 'example.com')
22
- end
23
-
24
- it 'contains Message, which should be of type Hash and empty' do
25
- expect(@mb_obj.message).to be_a(Hash)
26
- expect(@mb_obj.message.length).to eq(0)
27
- end
28
-
29
- it 'contains recipient_variables, which should be of type Hash and empty' do
30
- expect(@mb_obj.recipient_variables).to be_a(Hash)
31
- expect(@mb_obj.recipient_variables.length).to eq(0)
32
- end
33
-
34
- it 'contains domain, which should be of type string and contain example.com' do
35
- expect(@mb_obj.domain).to be_a(String)
36
- expect(@mb_obj.domain).to eq('example.com')
37
- end
38
-
39
- it 'contains message_ids, which should be of type hash and empty' do
40
- expect(@mb_obj.message_ids).to be_a(Hash)
41
- expect(@mb_obj.message_ids.length).to eq(0)
42
- end
43
-
44
- it 'contains counters, which should be of type hash and contain several important counters' do
45
- expect(@mb_obj.counters).to be_a(Hash)
46
- expect(@mb_obj.counters).to include(:recipients)
47
- end
48
-
49
- it 'contains counters, which should be of type hash and contain several important counters' do
50
- expect(@mb_obj.counters).to be_a(Hash)
51
-
52
- expect(@mb_obj.counters).to include(:recipients)
53
- expect(@mb_obj.counters[:recipients]).to include(:to)
54
- expect(@mb_obj.counters[:recipients]).to include(:cc)
55
- expect(@mb_obj.counters[:recipients]).to include(:bcc)
56
-
57
- expect(@mb_obj.counters).to include(:attributes)
58
- expect(@mb_obj.counters[:attributes]).to include(:attachment)
59
- expect(@mb_obj.counters[:attributes]).to include(:campaign_id)
60
- expect(@mb_obj.counters[:attributes]).to include(:custom_option)
61
- expect(@mb_obj.counters[:attributes]).to include(:tag)
62
- end
63
- end
64
-
65
- describe 'The method add_recipient' do
66
- before do
67
- @mb_client = Mailgun::UnitClient.new('messages')
68
- @mb_obj = Mailgun::BatchMessage.new(@mb_client, 'example.com')
69
- @address1 = 'jane@example.com'
70
- @variables1 = { 'first' => 'Jane', 'last' => 'Doe', 'tracking' => 'ABC123' }
71
- @address2 = 'bob@example.com'
72
- @variables2 = { 'first' => 'Bob', 'last' => 'Doe', 'tracking' => 'DEF123' }
73
- @address3 = 'sam@example.com'
74
- @variables3 = { 'first' => 'Sam', 'last' => 'Doe', 'tracking' => 'GHI123' }
75
- end
76
-
77
- context 'when from is present' do
78
- before do
79
- @mb_obj.from('example@email.com')
80
- end
81
-
82
- it 'adds 1,000 recipients to the message body and validates counter is incremented then reset' do
83
- recipient_type = :to
84
- 1000.times do
85
- @mb_obj.add_recipient(recipient_type, @address1, @variables1)
86
- end
87
-
88
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
89
-
90
- @mb_obj.add_recipient(recipient_type, @address1, @variables1)
91
-
92
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
93
- end
94
-
95
- it 'adds recipients to the message, calls finalize, and cleans up' do
96
- recipient_type = :to
97
- 1000.times do
98
- @mb_obj.add_recipient(recipient_type, @address1, @variables1)
99
- end
100
-
101
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
102
- @mb_obj.finalize
103
-
104
- expect(@mb_obj.recipient_variables).to eq({})
105
- expect(@mb_obj.message['recipient-variables'].length).to eq(0)
106
- expect(@mb_obj.message[:to].length).to eq(0)
107
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(0)
108
- end
109
-
110
- it 'adds 5,005 recipients to the message body and validates we receive message_ids back' do
111
- recipient_type = :to
112
- 5005.times do
113
- @mb_obj.add_recipient(recipient_type, @address1, @variables1)
114
- end
115
- @mb_obj.finalize
116
-
117
- expect(@mb_obj.message_ids.length).to eq(6)
118
- end
119
-
120
- it 'sets recipient-variables, for batch expansion' do
121
- recipient_type = :to
122
- @mb_obj.add_recipient(recipient_type, @address1, @variables1)
123
-
124
- expect(@mb_obj.recipient_variables[@address1]).to eq(@variables1)
125
- end
126
-
127
- it 'sets multiple recipient-variables, for batch expansion' do
128
- recipient_type = :to
129
- @mb_obj.add_recipient(recipient_type, @address1, @variables1)
130
- @mb_obj.add_recipient(recipient_type, @address2, @variables2)
131
- @mb_obj.add_recipient(recipient_type, @address3, @variables3)
132
-
133
- expect(@mb_obj.recipient_variables[@address1]).to eq(@variables1)
134
- expect(@mb_obj.recipient_variables[@address2]).to eq(@variables2)
135
- expect(@mb_obj.recipient_variables[@address3]).to eq(@variables3)
136
- end
137
- end
138
-
139
- context 'when from is empty' do
140
- it 'shows error message' do
141
- recipient_type = :to
142
- @mb_obj.add_recipient(recipient_type, @address1, @variables1)
143
- @mb_obj.add_recipient(recipient_type, @address2, @variables2)
144
- expect(@mb_client).to receive(:raise)
145
- @mb_obj.finalize
146
- end
147
- end
148
- end