mailgun-ruby 1.4.1 → 1.4.3

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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +30 -8
  3. data/.rubocop.yml +64 -4
  4. data/Gemfile +3 -1
  5. data/README.md +1 -1
  6. data/Rakefile +5 -8
  7. data/docs/AnalyticsTags.md +63 -0
  8. data/lib/mailgun/address.rb +5 -5
  9. data/lib/mailgun/chains.rb +2 -3
  10. data/lib/mailgun/client.rb +56 -56
  11. data/lib/mailgun/domains/domains.rb +11 -10
  12. data/lib/mailgun/events/events.rb +4 -3
  13. data/lib/mailgun/exceptions/exceptions.rb +12 -15
  14. data/lib/mailgun/helpers/api_version_checker.rb +6 -1
  15. data/lib/mailgun/lists/opt_in_handler.rb +6 -10
  16. data/lib/mailgun/logs/logs.rb +4 -2
  17. data/lib/mailgun/messages/batch_message.rb +10 -10
  18. data/lib/mailgun/messages/message_builder.rb +40 -56
  19. data/lib/mailgun/metrics/metrics.rb +12 -6
  20. data/lib/mailgun/response.rb +12 -10
  21. data/lib/mailgun/subaccounts/subaccounts.rb +13 -8
  22. data/lib/mailgun/suppressions.rb +36 -43
  23. data/lib/mailgun/tags/analytics_tags.rb +37 -2
  24. data/lib/mailgun/tags/tags.rb +29 -19
  25. data/lib/mailgun/templates/templates.rb +40 -29
  26. data/lib/mailgun/version.rb +3 -1
  27. data/lib/mailgun/webhooks/webhooks.rb +22 -19
  28. data/lib/mailgun-ruby.rb +2 -0
  29. data/lib/mailgun.rb +4 -4
  30. data/lib/railgun/attachment.rb +12 -19
  31. data/lib/railgun/errors.rb +2 -3
  32. data/lib/railgun/mailer.rb +37 -41
  33. data/lib/railgun/railtie.rb +2 -0
  34. data/lib/railgun.rb +2 -0
  35. data/mailgun.gemspec +15 -11
  36. data/spec/integration/analytics_tags_spec.rb +54 -0
  37. data/spec/integration/bounces_spec.rb +12 -11
  38. data/spec/integration/campaign_spec.rb +20 -18
  39. data/spec/integration/complaints_spec.rb +8 -6
  40. data/spec/integration/domains_spec.rb +12 -18
  41. data/spec/integration/email_validation_spec.rb +35 -34
  42. data/spec/integration/events_spec.rb +8 -8
  43. data/spec/integration/list_members_spec.rb +27 -26
  44. data/spec/integration/list_spec.rb +22 -21
  45. data/spec/integration/logs_spec.rb +49 -47
  46. data/spec/integration/mailer_spec.rb +7 -3
  47. data/spec/integration/mailgun_spec.rb +85 -92
  48. data/spec/integration/metrics_spec.rb +137 -131
  49. data/spec/integration/routes_spec.rb +41 -40
  50. data/spec/integration/stats_spec.rb +4 -2
  51. data/spec/integration/subaccounts_spec.rb +9 -10
  52. data/spec/integration/suppressions_spec.rb +222 -44
  53. data/spec/integration/templates_spec.rb +14 -12
  54. data/spec/integration/unsubscribes_spec.rb +8 -6
  55. data/spec/integration/webhook_spec.rb +18 -12
  56. data/spec/spec_helper.rb +15 -8
  57. data/spec/unit/client_spec.rb +424 -0
  58. data/spec/unit/connection/test_client.rb +108 -55
  59. data/spec/unit/events/events_spec.rb +48 -29
  60. data/spec/unit/exceptions/exceptions_spec.rb +8 -7
  61. data/spec/unit/helpers/api_version_checker_spec.rb +206 -0
  62. data/spec/unit/lists/opt_in_handler_spec.rb +11 -7
  63. data/spec/unit/mailgun_spec.rb +71 -68
  64. data/spec/unit/messages/batch_message_spec.rb +37 -36
  65. data/spec/unit/messages/message_builder_spec.rb +170 -169
  66. data/spec/unit/railgun/content_type_spec.rb +31 -30
  67. data/spec/unit/railgun/mailer_spec.rb +62 -59
  68. data/spec/unit/response_spec.rb +225 -0
  69. data/vcr_cassettes/For_the_suppressions_handling_class/creates_a_single_bounce.yml +55 -0
  70. data/vcr_cassettes/analytics_tags.yml +187 -0
  71. data/vcr_cassettes/suppressions.yml +1053 -170
  72. metadata +95 -29
  73. data/.rubocop_todo.yml +0 -22
@@ -1,26 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe 'Mailgun instantiation' do
4
6
  it 'instantiates an HttpClient object' do
5
- expect {@mg_obj = Mailgun::UnitClient.new("messages")}.not_to raise_error
7
+ expect { @mg_obj = Mailgun::UnitClient.new('messages') }.not_to raise_error
6
8
  end
7
9
  end
8
10
 
9
11
  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"
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'
15
17
  end
16
18
 
17
19
  it 'accepts only specific data types' do
18
- @mb_obj = Mailgun::MessageBuilder.new()
20
+ @mb_obj = Mailgun::MessageBuilder.new
19
21
  @mh_obj = {}
20
22
 
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
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
24
26
  end
25
27
 
26
28
  it 'sends a message' do
@@ -28,12 +30,12 @@ describe 'The method send_message()' do
28
30
  'to' => 'bob@example.com',
29
31
  'subject' => 'Test',
30
32
  'text' => 'Test Data' }
31
- result = @mg_obj.send_message("testdomain.com", data)
33
+ result = @mg_obj.send_message('testdomain.com', data)
32
34
 
33
35
  result.to_h!
34
36
 
35
- expect(result.body).to include("message")
36
- expect(result.body).to include("id")
37
+ expect(result.body).to include('message')
38
+ expect(result.body).to include('id')
37
39
  end
38
40
 
39
41
  it 'opens the message MIME and sends the MIME message.' do
@@ -42,119 +44,120 @@ describe 'The method send_message()' do
42
44
  'message' => 'Sample Data/mime.txt',
43
45
  'from' => 'joe@test.com'
44
46
  }
45
- result = @mg_obj.send_message("testdomain.com", data)
47
+ result = @mg_obj.send_message('testdomain.com', data)
46
48
 
47
49
  result.to_h!
48
50
 
49
- expect(result.body).to include("message")
50
- expect(result.body).to include("id")
51
+ expect(result.body).to include('message')
52
+ expect(result.body).to include('id')
51
53
  end
52
54
 
53
55
  context 'when domain is missing' do
54
56
  it 'shows failure message' do
55
- expect(@mg_obj).to receive(:fail)
57
+ expect(@mg_obj).to receive(:raise)
56
58
  @mg_obj.send_message(nil, {})
57
59
  end
58
60
  end
59
61
 
60
62
  context 'when to is missing' do
61
63
  it 'shows failure message' do
62
- data = {
63
- 'to' => '',
64
- 'message' => 'Sample Data/mime.txt',
65
- 'from' => 'joe@test.com'
66
- }
67
- expect(@mg_obj).to receive(:fail)
68
- @mg_obj.send_message("testdomain.com", data)
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)
69
71
  end
70
72
  end
71
73
  end
72
74
 
73
75
  describe 'The method post()' do
74
- before(:each) do
75
- @mg_obj = Mailgun::UnitClient.new("messages")
76
- @domain = "test.com"
76
+ before do
77
+ @mg_obj = Mailgun::UnitClient.new('messages')
78
+ @domain = 'test.com'
77
79
  end
80
+
78
81
  it 'in this case, sends a simple message.' do
79
- data = {'from' => 'joe@test.com',
80
- 'to' => 'bob@example.com',
81
- 'subject' => 'Test',
82
- 'text' => 'Test Data'}
82
+ data = { 'from' => 'joe@test.com',
83
+ 'to' => 'bob@example.com',
84
+ 'subject' => 'Test',
85
+ 'text' => 'Test Data' }
83
86
  result = @mg_obj.post("#{@domain}/messages", data)
84
87
 
85
88
  result.to_h!
86
89
 
87
- expect(result.body).to include("message")
88
- expect(result.body).to include("id")
90
+ expect(result.body).to include('message')
91
+ expect(result.body).to include('id')
89
92
  end
90
93
 
91
94
  context 'when Unknown API error is raised' do
92
95
  before do
93
- allow(Mailgun::Response).to receive(:new).and_raise(StandardError, "message")
96
+ allow(Mailgun::Response).to receive(:new).and_raise(StandardError, 'message')
94
97
  allow(JSON).to receive(:parse).and_raise('Unknown')
95
98
  end
96
99
 
97
100
  it 'adds Unknown API error to message' do
98
- data = {'from' => 'joe@test.com',
99
- 'to' => 'bob@example.com',
100
- 'subject' => 'Test',
101
- 'text' => 'Test Data'}
101
+ data = { 'from' => 'joe@test.com',
102
+ 'to' => 'bob@example.com',
103
+ 'subject' => 'Test',
104
+ 'text' => 'Test Data' }
102
105
  @mg_obj.post("#{@domain}/messages", data)
103
- rescue Mailgun::CommunicationError => err
104
- expect(err.message).to eq('message: Unknown API error')
106
+ rescue Mailgun::CommunicationError => e
107
+ expect(e.message).to eq('message: Unknown API error')
105
108
  else
106
- fail
109
+ raise
107
110
  end
108
111
  end
109
112
  end
110
113
 
111
114
  describe 'The method put()' do
112
- before(:each) do
113
- @mg_obj = Mailgun::UnitClient.new("lists")
114
- @domain = "test.com"
115
- @list_address = "mylist@test.com"
116
- @member_address = "subscribee@test.com"
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'
117
120
  end
118
121
 
119
122
  it 'in this case, updates a member with the attributes in data.' do
120
- data = {'subscribed' => false,
121
- 'name' => 'Foo Bar'}
123
+ data = { 'subscribed' => false,
124
+ 'name' => 'Foo Bar' }
122
125
  result = @mg_obj.put("lists/#{@list_address}/members#{@member_address}", data)
123
126
 
124
127
  result.to_h!
125
128
 
126
- expect(result.body).to include("member")
127
- expect(result.body["member"]).to include("vars")
128
- expect(result.body["member"]["vars"]).to include("age")
129
- expect(result.body["member"]).to include("name")
130
- expect(result.body["member"]).to include("subscribed")
131
- expect(result.body["member"]).to include("address")
132
- expect(result.body).to include("message")
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')
133
136
  end
134
-
135
137
  end
136
138
 
137
139
  describe 'The method get()' do
138
- before(:each) do
139
- @mg_obj = Mailgun::UnitClient.new("bounces")
140
- @domain = "test.com"
140
+ before do
141
+ @mg_obj = Mailgun::UnitClient.new('bounces')
142
+ @domain = 'test.com'
141
143
  end
144
+
142
145
  it 'in this case, obtains a list of bounces for the domain, limit of 5, skipping the first 10.' do
143
- query_string = {'skip' => '10',
144
- 'limit' => '5'}
146
+ query_string = { 'skip' => '10',
147
+ 'limit' => '5' }
145
148
  result = @mg_obj.get("#{@domain}/bounces", query_string)
146
149
 
147
150
  result.to_h!
148
151
 
149
- expect(result.body).to include("total_count")
150
- expect(result.body).to include("items")
152
+ expect(result.body).to include('total_count')
153
+ expect(result.body).to include('items')
151
154
  end
152
155
  end
153
156
 
154
157
  describe 'The method delete()' do
155
- before(:each) do
156
- @mg_obj = Mailgun::UnitClient.new("campaigns")
157
- @domain = "test.com"
158
+ before do
159
+ @mg_obj = Mailgun::UnitClient.new('campaigns')
160
+ @domain = 'test.com'
158
161
  end
159
162
 
160
163
  it 'issues a generic delete request.' do
@@ -162,7 +165,7 @@ describe 'The method delete()' do
162
165
 
163
166
  result.to_h!
164
167
 
165
- expect(result.body).to include("message")
166
- expect(result.body).to include("id")
168
+ expect(result.body).to include('message')
169
+ expect(result.body).to include('id')
167
170
  end
168
171
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  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")
6
+ it 'is readable' do
7
+ @mb_client = Mailgun::UnitClient.new('messages')
8
+ @mb_obj = Mailgun::BatchMessage.new(@mb_client, 'example.com')
7
9
 
8
10
  expect(@mb_obj).to respond_to(:message_ids)
9
11
  expect(@mb_obj).to respond_to(:message)
@@ -14,10 +16,9 @@ describe 'BatchMessage attribute readers' do
14
16
  end
15
17
 
16
18
  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")
19
+ before do
20
+ @mb_client = Mailgun::UnitClient.new('messages')
21
+ @mb_obj = Mailgun::BatchMessage.new(@mb_client, 'example.com')
21
22
  end
22
23
 
23
24
  it 'contains Message, which should be of type Hash and empty' do
@@ -62,38 +63,39 @@ describe 'The instantiation of Batch Message' do
62
63
  end
63
64
 
64
65
  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'}
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' }
74
75
  end
76
+
75
77
  context 'when from is present' do
76
- before(:each) do
78
+ before do
77
79
  @mb_obj.from('example@email.com')
78
80
  end
79
81
 
80
82
  it 'adds 1,000 recipients to the message body and validates counter is incremented then reset' do
81
83
  recipient_type = :to
82
84
  1000.times do
83
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
85
+ @mb_obj.add_recipient(recipient_type, @address1, @variables1)
84
86
  end
85
87
 
86
88
  expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
87
-
88
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
89
-
89
+
90
+ @mb_obj.add_recipient(recipient_type, @address1, @variables1)
91
+
90
92
  expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
91
93
  end
92
94
 
93
95
  it 'adds recipients to the message, calls finalize, and cleans up' do
94
96
  recipient_type = :to
95
97
  1000.times do
96
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
98
+ @mb_obj.add_recipient(recipient_type, @address1, @variables1)
97
99
  end
98
100
 
99
101
  expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
@@ -108,7 +110,7 @@ describe 'The method add_recipient' do
108
110
  it 'adds 5,005 recipients to the message body and validates we receive message_ids back' do
109
111
  recipient_type = :to
110
112
  5005.times do
111
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
113
+ @mb_obj.add_recipient(recipient_type, @address1, @variables1)
112
114
  end
113
115
  @mb_obj.finalize
114
116
 
@@ -117,31 +119,30 @@ describe 'The method add_recipient' do
117
119
 
118
120
  it 'sets recipient-variables, for batch expansion' do
119
121
  recipient_type = :to
120
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
122
+ @mb_obj.add_recipient(recipient_type, @address1, @variables1)
121
123
 
122
- expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
124
+ expect(@mb_obj.recipient_variables[@address1]).to eq(@variables1)
123
125
  end
124
126
 
125
127
  it 'sets multiple recipient-variables, for batch expansion' do
126
128
  recipient_type = :to
127
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
128
- @mb_obj.add_recipient(recipient_type, @address_2, @variables_2)
129
- @mb_obj.add_recipient(recipient_type, @address_3, @variables_3)
130
-
131
- expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
132
- expect(@mb_obj.recipient_variables[@address_2]).to eq(@variables_2)
133
- expect(@mb_obj.recipient_variables[@address_3]).to eq(@variables_3)
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)
134
136
  end
135
137
  end
136
138
 
137
139
  context 'when from is empty' do
138
140
  it 'shows error message' do
139
141
  recipient_type = :to
140
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
141
- @mb_obj.add_recipient(recipient_type, @address_2, @variables_2)
142
- expect(@mb_client).to receive(:fail)
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)
143
145
  @mb_obj.finalize
144
146
  end
145
147
  end
146
-
147
148
  end