mailtrap 2.5.0 → 2.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cfe51fc46a03cac98509b38611a60c5c7280c96055346d557590e3a59a17de9
4
- data.tar.gz: 5a8acfa518abf2903105f4a2a2df8d850532419e961372078ea0bd691ea898e2
3
+ metadata.gz: a56e52ebde096ddaa019f4c16e15f63fe37ace69d91892f4c92044c0727b3c3f
4
+ data.tar.gz: 79a7aca8251526389c4faa2f643ca8f7e58182de4dcbada0af2d8f9a5afe6464
5
5
  SHA512:
6
- metadata.gz: f0381884cc8e03c59523f87b2e4ec35872ddf514b135e9e5f72ce6d6752d37ae531dc8a91b40c8ed3b2a66f4b4e1cc1dbac95638dd94382a625145b24271d0d9
7
- data.tar.gz: 54da492c1a517820087101305e0efbfa07b5e4a6a297aba561bfe778504984cca563e45da710dc0df520d0890e80e8c58446408eac4e2c1eae6d035ed9b05083
6
+ metadata.gz: df5e86fd7a0d31a5b717e189ae9637f6e0f413dad2d4fd2c9adbc56e7be5197ef5a59a759dc6eee9ff19a98c9196c7d5f1a984b3ee5f75ef69a5ca0f376a5519
7
+ data.tar.gz: 516e66780366eaf9f76b07f0957092b345fdd0262cd15dd997dcde5887276b7b37ba9c5c29971c6e7e53082e74db4deb5575ba1b4e0ebab4341fae05d7accc98
data/.coderabbit.yaml ADDED
@@ -0,0 +1,10 @@
1
+ # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
2
+ language: en
3
+ reviews:
4
+ path_instructions:
5
+ - path: "spec/fixtures/vcr_cassettes/**/*.yml"
6
+ instructions: |
7
+ Act as a data privacy officer. Carefully read all the vcr cassettes
8
+ with recorded HTTP interactions and try to identify sensitive data that
9
+ could potentially be recorded. It can be anything from PII to
10
+ credentials. Ignore obvious placeholder values and real IDs in URLs.
data/.rubocop.yml CHANGED
@@ -1,4 +1,4 @@
1
- require:
1
+ plugins:
2
2
  - rubocop-rake
3
3
  - rubocop-rspec
4
4
 
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## [2.7.0] - 2026-02-24
2
+ - Add Sandbox Messages API
3
+ - Add Sending Domains API
4
+ - Add Sandbox Attachments API
5
+ - Add Accounts API
6
+
7
+ ## [2.6.0] - 2026-01-27
8
+ - Add Inboxes API
9
+ - Add Projects API
10
+ - Models' `to_h` now returns all fields without compacting
11
+
1
12
  ## [2.5.0] - 2025-11-10
2
13
  - Add Contact Imports API
3
14
  - Add Suppressions API
data/Gemfile CHANGED
@@ -8,13 +8,16 @@ gem 'appraisal'
8
8
  gem 'irb'
9
9
  gem 'mail'
10
10
  gem 'net-smtp'
11
- gem 'rake', '~> 13.0'
12
- gem 'rdoc', '~> 6.13.0'
13
- gem 'rspec', '~> 3.0'
11
+ gem 'rack'
12
+ gem 'rackup'
13
+ gem 'rake', '~> 13.3'
14
+ gem 'rdoc', '~> 7.1.0'
15
+ gem 'rspec', '~> 3'
14
16
  gem 'rspec-its'
15
- gem 'rubocop', '~> 1.21'
17
+ gem 'rubocop', '~> 1.82'
16
18
  gem 'rubocop-rake', require: false
17
19
  gem 'rubocop-rspec', require: false
18
20
  gem 'vcr'
19
21
  gem 'webmock'
22
+ gem 'webrick'
20
23
  gem 'yard', '~> 0.9'
data/Gemfile.lock CHANGED
@@ -1,88 +1,96 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mailtrap (2.5.0)
4
+ mailtrap (2.7.0)
5
5
  base64
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- addressable (2.8.7)
11
- public_suffix (>= 2.0.2, < 7.0)
10
+ addressable (2.8.8)
11
+ public_suffix (>= 2.0.2, < 8.0)
12
12
  appraisal (2.5.0)
13
13
  bundler
14
14
  rake
15
15
  thor (>= 0.14.0)
16
16
  ast (2.4.3)
17
17
  base64 (0.3.0)
18
- bigdecimal (3.1.8)
19
- crack (1.0.0)
18
+ bigdecimal (4.0.1)
19
+ crack (1.0.1)
20
20
  bigdecimal
21
21
  rexml
22
- date (3.4.1)
23
- diff-lcs (1.5.1)
24
- hashdiff (1.1.0)
25
- io-console (0.8.0)
26
- irb (1.15.2)
22
+ date (3.5.1)
23
+ diff-lcs (1.6.2)
24
+ erb (6.0.1)
25
+ hashdiff (1.2.1)
26
+ io-console (0.8.2)
27
+ irb (1.16.0)
27
28
  pp (>= 0.6.0)
28
29
  rdoc (>= 4.0.0)
29
30
  reline (>= 0.4.2)
30
- json (2.12.2)
31
+ json (2.18.0)
31
32
  language_server-protocol (3.17.0.5)
32
33
  lint_roller (1.1.0)
33
- mail (2.8.1)
34
+ logger (1.7.0)
35
+ mail (2.9.0)
36
+ logger
34
37
  mini_mime (>= 0.1.1)
35
38
  net-imap
36
39
  net-pop
37
40
  net-smtp
38
41
  mini_mime (1.1.5)
39
- net-imap (0.5.7)
42
+ net-imap (0.6.2)
40
43
  date
41
44
  net-protocol
42
45
  net-pop (0.1.2)
43
46
  net-protocol
44
47
  net-protocol (0.2.2)
45
48
  timeout
46
- net-smtp (0.5.0)
49
+ net-smtp (0.5.1)
47
50
  net-protocol
48
51
  parallel (1.27.0)
49
- parser (3.3.8.0)
52
+ parser (3.3.10.1)
50
53
  ast (~> 2.4.1)
51
54
  racc
52
- pp (0.6.2)
55
+ pp (0.6.3)
53
56
  prettyprint
54
57
  prettyprint (0.2.0)
55
- prism (1.4.0)
56
- psych (5.2.6)
58
+ prism (1.9.0)
59
+ psych (5.3.1)
57
60
  date
58
61
  stringio
59
- public_suffix (5.1.1)
62
+ public_suffix (7.0.2)
60
63
  racc (1.8.1)
64
+ rack (3.2.4)
65
+ rackup (2.3.1)
66
+ rack (>= 3)
61
67
  rainbow (3.1.1)
62
- rake (13.2.1)
63
- rdoc (6.13.1)
68
+ rake (13.3.1)
69
+ rdoc (7.1.0)
70
+ erb
64
71
  psych (>= 4.0.0)
65
- regexp_parser (2.10.0)
66
- reline (0.6.1)
72
+ tsort
73
+ regexp_parser (2.11.3)
74
+ reline (0.6.3)
67
75
  io-console (~> 0.5)
68
- rexml (3.4.2)
69
- rspec (3.13.0)
76
+ rexml (3.4.4)
77
+ rspec (3.13.2)
70
78
  rspec-core (~> 3.13.0)
71
79
  rspec-expectations (~> 3.13.0)
72
80
  rspec-mocks (~> 3.13.0)
73
- rspec-core (3.13.0)
81
+ rspec-core (3.13.6)
74
82
  rspec-support (~> 3.13.0)
75
- rspec-expectations (3.13.1)
83
+ rspec-expectations (3.13.5)
76
84
  diff-lcs (>= 1.2.0, < 2.0)
77
85
  rspec-support (~> 3.13.0)
78
- rspec-its (1.3.0)
79
- rspec-core (>= 3.0.0)
80
- rspec-expectations (>= 3.0.0)
81
- rspec-mocks (3.13.1)
86
+ rspec-its (2.0.0)
87
+ rspec-core (>= 3.13.0)
88
+ rspec-expectations (>= 3.13.0)
89
+ rspec-mocks (3.13.7)
82
90
  diff-lcs (>= 1.2.0, < 2.0)
83
91
  rspec-support (~> 3.13.0)
84
- rspec-support (3.13.1)
85
- rubocop (1.78.0)
92
+ rspec-support (3.13.6)
93
+ rubocop (1.84.0)
86
94
  json (~> 2.3)
87
95
  language_server-protocol (~> 3.17.0.2)
88
96
  lint_roller (~> 1.1.0)
@@ -90,29 +98,33 @@ GEM
90
98
  parser (>= 3.3.0.2)
91
99
  rainbow (>= 2.2.2, < 4.0)
92
100
  regexp_parser (>= 2.9.3, < 3.0)
93
- rubocop-ast (>= 1.45.1, < 2.0)
101
+ rubocop-ast (>= 1.49.0, < 2.0)
94
102
  ruby-progressbar (~> 1.7)
95
103
  unicode-display_width (>= 2.4.0, < 4.0)
96
- rubocop-ast (1.45.1)
104
+ rubocop-ast (1.49.0)
97
105
  parser (>= 3.3.7.2)
98
- prism (~> 1.4)
99
- rubocop-rake (0.6.0)
100
- rubocop (~> 1.0)
101
- rubocop-rspec (3.0.2)
102
- rubocop (~> 1.61)
106
+ prism (~> 1.7)
107
+ rubocop-rake (0.7.1)
108
+ lint_roller (~> 1.1)
109
+ rubocop (>= 1.72.1)
110
+ rubocop-rspec (3.9.0)
111
+ lint_roller (~> 1.1)
112
+ rubocop (~> 1.81)
103
113
  ruby-progressbar (1.13.0)
104
- stringio (3.1.7)
105
- thor (1.4.0)
106
- timeout (0.4.3)
107
- unicode-display_width (3.1.4)
108
- unicode-emoji (~> 4.0, >= 4.0.4)
109
- unicode-emoji (4.0.4)
110
- vcr (6.2.0)
111
- webmock (3.23.1)
114
+ stringio (3.2.0)
115
+ thor (1.5.0)
116
+ timeout (0.6.0)
117
+ tsort (0.2.0)
118
+ unicode-display_width (3.2.0)
119
+ unicode-emoji (~> 4.1)
120
+ unicode-emoji (4.2.0)
121
+ vcr (6.4.0)
122
+ webmock (3.26.1)
112
123
  addressable (>= 2.8.0)
113
124
  crack (>= 0.3.2)
114
125
  hashdiff (>= 0.4.0, < 2.0.0)
115
- yard (0.9.37)
126
+ webrick (1.9.2)
127
+ yard (0.9.38)
116
128
 
117
129
  PLATFORMS
118
130
  ruby
@@ -123,15 +135,18 @@ DEPENDENCIES
123
135
  mail
124
136
  mailtrap!
125
137
  net-smtp
126
- rake (~> 13.0)
127
- rdoc (~> 6.13.0)
128
- rspec (~> 3.0)
138
+ rack
139
+ rackup
140
+ rake (~> 13.3)
141
+ rdoc (~> 7.1.0)
142
+ rspec (~> 3)
129
143
  rspec-its
130
- rubocop (~> 1.21)
144
+ rubocop (~> 1.82)
131
145
  rubocop-rake
132
146
  rubocop-rspec
133
147
  vcr
134
148
  webmock
149
+ webrick
135
150
  yard (~> 0.9)
136
151
 
137
152
  BUNDLED WITH
data/README.md CHANGED
@@ -1,17 +1,21 @@
1
+ # Mailtrap Ruby client - Official
2
+
3
+ ![Ruby](https://img.shields.io/badge/Ruby-CC342D)
1
4
  [![test](https://github.com/mailtrap/mailtrap-ruby/actions/workflows/main.yml/badge.svg)](https://github.com/mailtrap/mailtrap-ruby/actions/workflows/main.yml)
2
5
  [![docs](https://shields.io/badge/docs-rubydoc.info-blue)](https://rubydoc.info/gems/mailtrap)
3
6
  [![gem](https://shields.io/gem/v/mailtrap)](https://rubygems.org/gems/mailtrap)
4
7
  [![downloads](https://shields.io/gem/dt/mailtrap)](https://rubygems.org/gems/mailtrap)
5
8
 
9
+ This client uses API v2, for v1 refer to [this documentation](https://mailtrap.docs.apiary.io/)
6
10
 
7
11
 
8
- # Official Mailtrap Ruby client
12
+ ## Prerequisites
9
13
 
10
- This Ruby gem offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io).
14
+ To get the most out of this official Mailtrap.io Ruby SDK:
11
15
 
12
- Quickly add email sending functionality to your Ruby application with Mailtrap.
16
+ - [Create a Mailtrap account](https://mailtrap.io/signup)
13
17
 
14
- (This client uses API v2, for v1 refer to [this documentation](https://mailtrap.docs.apiary.io/))
18
+ - [Verify your domain](https://mailtrap.io/sending/domains)
15
19
 
16
20
  ## Installation
17
21
 
@@ -105,31 +109,27 @@ client.send_batch(
105
109
  )
106
110
  ```
107
111
 
108
- ### Email Templates API
112
+ ### Sandbox Sending
113
+
114
+ Send emails to your Sandbox for testing purposes:
109
115
 
110
116
  ```ruby
111
117
  require 'mailtrap'
112
118
 
113
- client = Mailtrap::Client.new(api_key: 'your-api-key')
114
- templates = Mailtrap::EmailTemplatesAPI.new 3229, client
115
-
116
- templates.create(
117
- name: 'Welcome Email',
118
- subject: 'Welcome to Mailtrap!',
119
- body_html: '<h1>Hello</h1>',
120
- body_text: 'Hello',
121
- category: 'welcome'
119
+ client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, inbox_id: YOUR_INBOX_ID)
120
+ client.send(mail)
121
+
122
+ # You can also pass the request parameters directly
123
+ client.send(
124
+ from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
125
+ to: [
126
+ { email: 'your@email.com' }
127
+ ],
128
+ subject: 'You are awesome!',
129
+ text: 'Congrats for sending test email with Mailtrap!'
122
130
  )
123
131
  ```
124
132
 
125
- Refer to the [`examples`](examples) folder for more examples:
126
-
127
- - [Full](examples/full.rb)
128
- - [Email template](examples/email_template.rb)
129
- - [Batch Sending](examples/batch.rb)
130
- - [ActionMailer](examples/action_mailer.rb)
131
- - [Email Templates API](examples/email_templates_api.rb)
132
-
133
133
  ### Content-Transfer-Encoding
134
134
 
135
135
  `mailtrap` gem uses Mailtrap API to send emails. Mailtrap API does not try to
@@ -148,7 +148,7 @@ configuration example.
148
148
 
149
149
  You can configure two Mailtrap clients to operate simultaneously. This setup is
150
150
  particularly useful when you need to send emails using both the transactional
151
- and bulk APIs. Refer to the configuration example below:
151
+ and bulk APIs. Refer to the configuration examples below.
152
152
 
153
153
  ```ruby
154
154
  # config/application.rb
@@ -168,26 +168,72 @@ config.action_mailer.mailtrap_bulk_settings = {
168
168
  mail(delivery_method: :mailtrap_bulk)
169
169
  ```
170
170
 
171
+ ## Supported functionality & Examples
172
+
173
+ Refer to the [`examples`](examples) folder for more examples:
174
+
175
+ Email API:
176
+
177
+ - Full Email Sending – [`full.rb`](examples/full.rb)
178
+ - Batch Sending – [`batch.rb`](examples/batch.rb)
179
+ - Sending Domains API – [`sending_domains_api.rb`](examples/sending_domains_api.rb)
180
+
181
+ Email Sandbox (Testing):
182
+
183
+ - Projects CRUD – [`projects_api.rb`](examples/projects_api.rb)
184
+ - Inboxes CRUD - [`inboxes_api.rb`](examples/inboxes_api.rb)
185
+ - Sandbox Messages CRUD - [`sandbox_messages_api.rb`](examples/sandbox_messages_api.rb)
186
+ - Sandbox Attachments API - [`sandbox_attachments_api.rb`](examples/sandbox_attachments_api.rb)
187
+
188
+ Contact management:
189
+
190
+ - Contacts CRUD & Listing – [`contacts_api.rb`](examples/contacts_api.rb)
191
+
192
+ General:
193
+
194
+ - Templates CRUD – [`email_templates_api.rb`](examples/email_templates_api.rb)
195
+ - Action Mailer – [`action_mailer.rb`](examples/action_mailer.rb)
196
+ - Accounts API – [`accounts_api.rb`](examples/accounts_api.rb)
197
+
171
198
  ## Migration guide v1 → v2
172
199
 
173
200
  Change `Mailtrap::Sending::Client` to `Mailtrap::Client`.
174
201
 
175
202
  If you use classes which have `Sending` namespace, remove the namespace like in the example above.
176
203
 
204
+ ## Contributing
205
+
206
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/railsware/mailtrap-ruby). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
207
+
177
208
  ## Development
178
209
 
179
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
210
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
211
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
212
+ prompt that will allow you to experiment.
180
213
 
181
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
214
+ To install this gem onto your local machine, run `bundle exec rake install`.
182
215
 
183
- ## Contributing
216
+ To release a new version, update the version number in `version.rb`, and then
217
+ run `bundle exec rake release`, which will create a git tag for the version,
218
+ push git commits and the created tag, and push the `.gem` file to
219
+ [rubygems.org](https://rubygems.org).
184
220
 
185
- Bug reports and pull requests are welcome on [GitHub](https://github.com/mailtrap/mailtrap-ruby). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
221
+ To run the documentation server, first generate the documentation with
222
+ `yard doc`, then run `yard server`.
223
+
224
+ All contributions are required to have rspec tests covering its functionality.
225
+
226
+ Please be sure to update [README](README.md) with new examples and features
227
+ when applicable.
186
228
 
187
229
  ## License
188
230
 
189
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
231
+ The package is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
190
232
 
191
233
  ## Code of Conduct
192
234
 
193
- Everyone interacting in the Mailtrap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
235
+ Everyone interacting in the Mailtrap project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
236
+
237
+ ## Compatibility with previous releases
238
+
239
+ Versions of this package up to 2.0.2 were an [unofficial client](https://github.com/vchin/mailtrap-client) developed by [@vchin](https://github.com/vchin). Package version 3 is a completely new package.
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailtrap
4
+ # Data Transfer Object for Account
5
+ # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/d26921ca2a48f-get-all-accounts
6
+ # @attr_reader id [Integer] The account ID
7
+ # @attr_reader name [String] The account name
8
+ # @attr_reader access_levels [Array] The account access levels
9
+ #
10
+ Account = Struct.new(
11
+ :id,
12
+ :name,
13
+ :access_levels,
14
+ keyword_init: true
15
+ )
16
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_api'
4
+ require_relative 'account'
5
+
6
+ module Mailtrap
7
+ class AccountsAPI
8
+ include BaseAPI
9
+
10
+ self.response_class = Account
11
+
12
+ attr_reader :client
13
+
14
+ # @param client [Mailtrap::Client] The client instance
15
+ # @raise [ArgumentError] If account_id is nil
16
+ def initialize(client = Mailtrap::Client.new)
17
+ @client = client
18
+ end
19
+
20
+ # Lists all accounts
21
+ # @return [Array<Account>] Array of accounts
22
+ # @!macro api_errors
23
+ def list
24
+ base_list
25
+ end
26
+
27
+ private
28
+
29
+ def base_path
30
+ '/api/accounts'
31
+ end
32
+ end
33
+ end
@@ -115,7 +115,7 @@ module Mailtrap
115
115
  # )
116
116
  # @param base [#to_json] The base email configuration for the batch.
117
117
  # @param requests [Array<#to_json>] Array of individual email requests.
118
- # @return [Hash] The JSON response from the API.
118
+ # @return [Hash, String, nil] JSON response or raw response body from the API.
119
119
  # @!macro api_errors
120
120
  # @raise [Mailtrap::MailSizeError] If the message is too large.
121
121
  def send_batch(base, requests)
@@ -153,7 +153,7 @@ module Mailtrap
153
153
  # text: 'Congrats for sending test email with Mailtrap!'
154
154
  # )
155
155
  # @param mail [#to_json] The email to send
156
- # @return [Hash] The JSON response
156
+ # @return [Hash, String, nil] JSON response or raw response body
157
157
  # @!macro api_errors
158
158
  # @raise [Mailtrap::MailSizeError] If the message is too large
159
159
  def send(mail)
@@ -168,7 +168,7 @@ module Mailtrap
168
168
  # Performs a GET request to the specified path
169
169
  # @param path [String] The request path
170
170
  # @param query_params [Hash] Query parameters to append to the URL (optional)
171
- # @return [Hash, nil] The JSON response
171
+ # @return [Hash, String, nil] JSON response or raw response body
172
172
  # @!macro api_errors
173
173
  def get(path, query_params = {})
174
174
  perform_request(
@@ -182,7 +182,7 @@ module Mailtrap
182
182
  # Performs a POST request to the specified path
183
183
  # @param path [String] The request path
184
184
  # @param body [Hash] The request body
185
- # @return [Hash, nil] The JSON response
185
+ # @return [Hash, String, nil] JSON response or raw response body
186
186
  # @!macro api_errors
187
187
  def post(path, body = nil)
188
188
  perform_request(
@@ -196,7 +196,7 @@ module Mailtrap
196
196
  # Performs a PATCH request to the specified path
197
197
  # @param path [String] The request path
198
198
  # @param body [Hash] The request body
199
- # @return [Hash, nil] The JSON response
199
+ # @return [Hash, String, nil] JSON response or raw response body
200
200
  # @!macro api_errors
201
201
  def patch(path, body = nil)
202
202
  perform_request(
@@ -209,7 +209,7 @@ module Mailtrap
209
209
 
210
210
  # Performs a DELETE request to the specified path
211
211
  # @param path [String] The request path
212
- # @return [Hash, nil] The JSON response
212
+ # @return [Hash, String, nil] JSON response or raw response body
213
213
  # @!macro api_errors
214
214
  def delete(path)
215
215
  perform_request(
@@ -221,8 +221,11 @@ module Mailtrap
221
221
 
222
222
  private
223
223
 
224
- def http_client_for(host)
225
- @http_clients[host] ||= Net::HTTP.new(host, api_port).tap { |client| client.use_ssl = true }
224
+ def validate_args!(api_key, api_port, bulk, sandbox, inbox_id)
225
+ raise ArgumentError, 'api_key is required' if api_key.nil?
226
+ raise ArgumentError, 'api_port is required' if api_port.nil?
227
+ raise ArgumentError, 'bulk stream is not applicable for sandbox API' if bulk && sandbox
228
+ raise ArgumentError, 'inbox_id is required for sandbox API' if sandbox && inbox_id.nil?
226
229
  end
227
230
 
228
231
  def select_api_host(bulk:, sandbox:)
@@ -235,14 +238,6 @@ module Mailtrap
235
238
  end
236
239
  end
237
240
 
238
- def send_path
239
- "/api/send#{"/#{inbox_id}" if sandbox}"
240
- end
241
-
242
- def batch_request_path
243
- "/api/batch#{"/#{inbox_id}" if sandbox}"
244
- end
245
-
246
241
  def perform_request(method:, host:, path:, query_params: {}, body: nil)
247
242
  http_client = http_client_for(host)
248
243
 
@@ -254,6 +249,10 @@ module Mailtrap
254
249
  handle_response(response)
255
250
  end
256
251
 
252
+ def http_client_for(host)
253
+ @http_clients[host] ||= Net::HTTP.new(host, api_port).tap { |client| client.use_ssl = true }
254
+ end
255
+
257
256
  def setup_request(method, uri_or_path, body = nil)
258
257
  request = case method
259
258
  when :get
@@ -279,17 +278,17 @@ module Mailtrap
279
278
  def handle_response(response) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
280
279
  case response
281
280
  when Net::HTTPOK, Net::HTTPCreated
282
- json_response(response.body)
281
+ parse_response(response)
283
282
  when Net::HTTPNoContent
284
283
  nil
285
284
  when Net::HTTPBadRequest
286
285
  raise Mailtrap::Error, ['bad request'] if response.body.empty?
287
286
 
288
- raise Mailtrap::Error, response_errors(response.body)
287
+ raise Mailtrap::Error, response_errors(response)
289
288
  when Net::HTTPUnauthorized
290
- raise Mailtrap::AuthorizationError, response_errors(response.body)
289
+ raise Mailtrap::AuthorizationError, response_errors(response)
291
290
  when Net::HTTPForbidden
292
- raise Mailtrap::RejectionError, response_errors(response.body)
291
+ raise Mailtrap::RejectionError, response_errors(response)
293
292
  when Net::HTTPPayloadTooLarge
294
293
  raise Mailtrap::MailSizeError, ['message too large']
295
294
  when Net::HTTPTooManyRequests
@@ -303,20 +302,37 @@ module Mailtrap
303
302
  end
304
303
  end
305
304
 
306
- def response_errors(body)
307
- parsed_body = json_response(body)
308
- Array(parsed_body[:errors] || parsed_body[:error])
305
+ def parse_response(response)
306
+ if json_response?(response)
307
+ json_response(response.body)
308
+ else
309
+ response.body
310
+ end
311
+ end
312
+
313
+ def response_errors(response)
314
+ if json_response?(response)
315
+ parsed_body = json_response(response.body)
316
+ Array(parsed_body[:errors] || parsed_body[:error])
317
+ else
318
+ [response.body]
319
+ end
320
+ end
321
+
322
+ def json_response?(response)
323
+ response.content_type == 'application/json'
309
324
  end
310
325
 
311
326
  def json_response(body)
312
327
  JSON.parse(body, symbolize_names: true)
313
328
  end
314
329
 
315
- def validate_args!(api_key, api_port, bulk, sandbox, inbox_id)
316
- raise ArgumentError, 'api_key is required' if api_key.nil?
317
- raise ArgumentError, 'api_port is required' if api_port.nil?
318
- raise ArgumentError, 'bulk stream is not applicable for sandbox API' if bulk && sandbox
319
- raise ArgumentError, 'inbox_id is required for sandbox API' if sandbox && inbox_id.nil?
330
+ def send_path
331
+ "/api/send#{"/#{inbox_id}" if sandbox}"
332
+ end
333
+
334
+ def batch_request_path
335
+ "/api/batch#{"/#{inbox_id}" if sandbox}"
320
336
  end
321
337
  end
322
338
  end
@@ -29,10 +29,5 @@ module Mailtrap
29
29
  def newly_created?
30
30
  @action != 'updated'
31
31
  end
32
-
33
- # @return [Hash] The contact attributes as a hash
34
- def to_h
35
- super.compact
36
- end
37
32
  end
38
33
  end
@@ -9,10 +9,5 @@ module Mailtrap
9
9
  # Allowed values: text, integer, float, boolean, date
10
10
  # @attr_reader merge_tag [String] Personalize your campaigns by adding a merge tag.
11
11
  # This field will be replaced with unique contact details for each recipient (max 80 characters)
12
- ContactField = Struct.new(:id, :name, :data_type, :merge_tag, keyword_init: true) do
13
- # @return [Hash] The contact field attributes as a hash
14
- def to_h
15
- super.compact
16
- end
17
- end
12
+ ContactField = Struct.new(:id, :name, :data_type, :merge_tag, keyword_init: true)
18
13
  end