From 4a6182195f548f71ad2e9cce561ef69204f76ccc Mon Sep 17 00:00:00 2001 From: DagonWat Date: Mon, 5 Jan 2026 13:25:23 +0100 Subject: [PATCH 1/5] Add Accounts API --- lib/mailtrap.rb | 1 + lib/mailtrap/account.rb | 21 +++++ lib/mailtrap/accounts_api.rb | 25 ++++++ .../maps_response_data_to_Account_objects.yml | 79 +++++++++++++++++++ .../raises_authorization_error.yml | 79 +++++++++++++++++++ spec/mailtrap/account_spec.rb | 61 ++++++++++++++ spec/mailtrap/accounts_api_spec.rb | 29 +++++++ 7 files changed, 295 insertions(+) create mode 100644 lib/mailtrap/account.rb create mode 100644 lib/mailtrap/accounts_api.rb create mode 100644 spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml create mode 100644 spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/when_api_key_is_incorrect/raises_authorization_error.yml create mode 100644 spec/mailtrap/account_spec.rb create mode 100644 spec/mailtrap/accounts_api_spec.rb diff --git a/lib/mailtrap.rb b/lib/mailtrap.rb index 3b6af23..e5d76d1 100644 --- a/lib/mailtrap.rb +++ b/lib/mailtrap.rb @@ -11,6 +11,7 @@ require_relative 'mailtrap/contact_imports_api' require_relative 'mailtrap/suppressions_api' require_relative 'mailtrap/projects_api' +require_relative 'mailtrap/accounts_api' module Mailtrap # @!macro api_errors diff --git a/lib/mailtrap/account.rb b/lib/mailtrap/account.rb new file mode 100644 index 0000000..ce168d6 --- /dev/null +++ b/lib/mailtrap/account.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Mailtrap + # Data Transfer Object for Project + # @see https://docs.mailtrap.io/developers/account-management/general-api/accounts + # @attr_reader id [Integer] The account ID + # @attr_reader name [String] The account name + # @attr_reader access_levels [Array] The account access levels + # + Account = Struct.new( + :id, + :name, + :access_levels, + keyword_init: true + ) do + # @return [Hash] The Project attributes as a hash + def to_h + super.compact + end + end +end diff --git a/lib/mailtrap/accounts_api.rb b/lib/mailtrap/accounts_api.rb new file mode 100644 index 0000000..056e4b6 --- /dev/null +++ b/lib/mailtrap/accounts_api.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative 'base_api' +require_relative 'account' + +module Mailtrap + class AccountsAPI + include BaseAPI + + self.response_class = Account + + # Lists all accounts + # @return [Array] Array of accounts + # @!macro api_errors + def list + base_list + end + + private + + def base_path + '/api/accounts' + end + end +end diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml b/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml new file mode 100644 index 0000000..6fc59c4 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml @@ -0,0 +1,79 @@ +--- +http_interactions: +- request: + method: get + uri: https://mailtrap.io/api/accounts + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - mailtrap-ruby (https://github.com/mailtrap/mailtrap-ruby) + Host: + - mailtrap.io + Authorization: + - Bearer + Content-Type: + - application/json + response: + status: + code: 200 + message: OK + headers: + Date: + - Mon, 05 Jan 2026 12:24:36 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Server: + - cloudflare + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + Vary: + - Accept + X-Mailtrap-Version: + - v2 + X-Ratelimit-Limit: + - '150' + X-Ratelimit-Remaining: + - '149' + Etag: + - W/"42e73e96e1ed3c973620c8c09d2f244c" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 10a9a535-2aa9-4004-baa2-6efbf6c7e701 + X-Runtime: + - '0.042150' + X-Cloud-Trace-Context: + - 26c79d159ce04474883f150f101a74b1;o=0 + Strict-Transport-Security: + - max-age=0 + Cf-Cache-Status: + - DYNAMIC + Cf-Ray: + - 9b92f41c6ab0d2eb-FRA + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: ASCII-8BIT + string: '[{"id":2326475,"name":"Yahor Vaitsiakhouski","access_levels":[100]}]' + recorded_at: Mon, 05 Jan 2026 12:24:36 GMT +recorded_with: VCR 6.2.0 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/when_api_key_is_incorrect/raises_authorization_error.yml b/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/when_api_key_is_incorrect/raises_authorization_error.yml new file mode 100644 index 0000000..c231550 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/when_api_key_is_incorrect/raises_authorization_error.yml @@ -0,0 +1,79 @@ +--- +http_interactions: +- request: + method: get + uri: https://mailtrap.io/api/accounts + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - mailtrap-ruby (https://github.com/mailtrap/mailtrap-ruby) + Host: + - mailtrap.io + Authorization: + - Bearer + Content-Type: + - application/json + response: + status: + code: 401 + message: Unauthorized + headers: + Date: + - Mon, 05 Jan 2026 12:24:37 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '31' + Connection: + - keep-alive + Server: + - cloudflare + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + Www-Authenticate: + - Token realm="Application" + Vary: + - Accept + X-Mailtrap-Version: + - v2 + X-Ratelimit-Limit: + - '150' + X-Ratelimit-Remaining: + - '149' + Cache-Control: + - no-cache + X-Request-Id: + - bb687a17-5205-409a-84fc-9a7efca5378e + X-Runtime: + - '0.007537' + X-Cloud-Trace-Context: + - 3810f5b5e5114fb3cd66b23e840b3a23;o=0 + Strict-Transport-Security: + - max-age=0 + Cf-Cache-Status: + - DYNAMIC + Cf-Ray: + - 9b92f41f8c68f65d-FRA + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: UTF-8 + string: '{"error":"Incorrect API token"}' + recorded_at: Mon, 05 Jan 2026 12:24:37 GMT +recorded_with: VCR 6.2.0 diff --git a/spec/mailtrap/account_spec.rb b/spec/mailtrap/account_spec.rb new file mode 100644 index 0000000..fca8e52 --- /dev/null +++ b/spec/mailtrap/account_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +RSpec.describe Mailtrap::Account do + describe '#initialize' do + subject(:account) { described_class.new(attributes) } + + let(:attributes) do + { + id: '123456', + name: 'Account 1', + access_levels: [ + 1000 + ] + } + end + + it 'creates a project with all attributes' do + expect(account).to match_struct( + id: '123456', + name: 'Account 1', + access_levels: [1000] + ) + end + end + + describe '#to_h' do + subject(:hash) { account.to_h } + + let(:account) do + described_class.new( + id: '123456', + name: 'Account 1', + access_levels: [ + 1000 + ] + ) + end + + it 'returns a hash with all attributes' do + expect(hash).to eq( + id: '123456', + name: 'Account 1', + access_levels: [1000] + ) + end + + context 'when some attributes are nil' do + let(:account) do + described_class.new( + id: '123456' + ) + end + + it 'returns a hash with only non-nil attributes' do + expect(hash).to eq( + id: '123456' + ) + end + end + end +end diff --git a/spec/mailtrap/accounts_api_spec.rb b/spec/mailtrap/accounts_api_spec.rb new file mode 100644 index 0000000..f9bf109 --- /dev/null +++ b/spec/mailtrap/accounts_api_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +RSpec.describe Mailtrap::AccountsAPI, :vcr do + subject(:accounts_api) { described_class.new(account_id, client) } + + let(:account_id) { ENV.fetch('MAILTRAP_ACCOUNT_ID', 1_111_111) } + let(:client) { Mailtrap::Client.new(api_key: ENV.fetch('MAILTRAP_API_KEY', 'local-api-key')) } + + describe '#list' do + subject(:list) { accounts_api.list } + + it 'maps response data to Account objects' do + expect(list).to all(be_a(Mailtrap::Account)) + expect(list.size).to eq(1) + end + + context 'when api key is incorrect' do + let(:client) { Mailtrap::Client.new(api_key: 'incorrect-api-key') } + + it 'raises authorization error' do + expect { list }.to raise_error do |error| + expect(error).to be_a(Mailtrap::AuthorizationError) + expect(error.message).to include('Incorrect API token') + expect(error.messages.any? { |msg| msg.include?('Incorrect API token') }).to be true + end + end + end + end +end From 318cdce0d3738399683b27f60ae4abf71eddfb7c Mon Sep 17 00:00:00 2001 From: DagonWat Date: Fri, 20 Feb 2026 12:40:22 +0100 Subject: [PATCH 2/5] Apply fixes for Accounts API --- CHANGELOG.md | 1 + README.md | 3 ++- examples/accounts_api.rb | 15 +++++++++++++++ lib/mailtrap/account.rb | 11 +++-------- spec/mailtrap/account_spec.rb | 36 ----------------------------------- 5 files changed, 21 insertions(+), 45 deletions(-) create mode 100644 examples/accounts_api.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index db6bf9d..f87545c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ - Add Sandbox Messages API - Add Sending Domains API - Add Sandbox Attachments API +- Add Accounts API ## [2.6.0] - 2026-01-27 - Add Inboxes API diff --git a/README.md b/README.md index 88587db..84cea42 100644 --- a/README.md +++ b/README.md @@ -189,10 +189,11 @@ Contact management: - Contacts CRUD & Listing – [`contacts_api.rb`](examples/contacts_api.rb) -General API: +General: - Templates CRUD – [`email_templates_api.rb`](examples/email_templates_api.rb) - Action Mailer – [`action_mailer.rb`](examples/action_mailer.rb) +- Accounts API – [`accounts_api.rb`](examples/accounts_api.rb) ## Migration guide v1 → v2 diff --git a/examples/accounts_api.rb b/examples/accounts_api.rb new file mode 100644 index 0000000..a0abbb2 --- /dev/null +++ b/examples/accounts_api.rb @@ -0,0 +1,15 @@ +require 'mailtrap' + +client = Mailtrap::Client.new(api_key: 'your-api-key') +account_id = 3229 +accounts = Mailtrap::AccountsAPI.new(account_id, client) + +# Set your API credentials as environment variables +# export MAILTRAP_API_KEY='your-api-key' +# export MAILTRAP_ACCOUNT_ID=your-account-id + +# accounts = Mailtrap::AccountsAPI.new + +# Get all accounts +accounts.list +# => [#] diff --git a/lib/mailtrap/account.rb b/lib/mailtrap/account.rb index ce168d6..d65cdd7 100644 --- a/lib/mailtrap/account.rb +++ b/lib/mailtrap/account.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true module Mailtrap - # Data Transfer Object for Project - # @see https://docs.mailtrap.io/developers/account-management/general-api/accounts + # Data Transfer Object for Фссщгте + # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/d26921ca2a48f-get-all-accounts # @attr_reader id [Integer] The account ID # @attr_reader name [String] The account name # @attr_reader access_levels [Array] The account access levels @@ -12,10 +12,5 @@ module Mailtrap :name, :access_levels, keyword_init: true - ) do - # @return [Hash] The Project attributes as a hash - def to_h - super.compact - end - end + ) end diff --git a/spec/mailtrap/account_spec.rb b/spec/mailtrap/account_spec.rb index fca8e52..35ddc2b 100644 --- a/spec/mailtrap/account_spec.rb +++ b/spec/mailtrap/account_spec.rb @@ -22,40 +22,4 @@ ) end end - - describe '#to_h' do - subject(:hash) { account.to_h } - - let(:account) do - described_class.new( - id: '123456', - name: 'Account 1', - access_levels: [ - 1000 - ] - ) - end - - it 'returns a hash with all attributes' do - expect(hash).to eq( - id: '123456', - name: 'Account 1', - access_levels: [1000] - ) - end - - context 'when some attributes are nil' do - let(:account) do - described_class.new( - id: '123456' - ) - end - - it 'returns a hash with only non-nil attributes' do - expect(hash).to eq( - id: '123456' - ) - end - end - end end From e84d0043be34dae69fee9b1fe84d1a98686a8d28 Mon Sep 17 00:00:00 2001 From: DagonWat Date: Fri, 20 Feb 2026 13:30:03 +0100 Subject: [PATCH 3/5] Fix typos --- lib/mailtrap/account.rb | 2 +- .../_list/maps_response_data_to_Account_objects.yml | 2 +- spec/mailtrap/account_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mailtrap/account.rb b/lib/mailtrap/account.rb index d65cdd7..c16dc1a 100644 --- a/lib/mailtrap/account.rb +++ b/lib/mailtrap/account.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Mailtrap - # Data Transfer Object for Фссщгте + # Data Transfer Object for Account # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/d26921ca2a48f-get-all-accounts # @attr_reader id [Integer] The account ID # @attr_reader name [String] The account name diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml b/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml index 6fc59c4..05d19a6 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_AccountsAPI/_list/maps_response_data_to_Account_objects.yml @@ -74,6 +74,6 @@ http_interactions: - h3=":443"; ma=86400 body: encoding: ASCII-8BIT - string: '[{"id":2326475,"name":"Yahor Vaitsiakhouski","access_levels":[100]}]' + string: '[{"id":2326475,"name":"John Doe","access_levels":[100]}]' recorded_at: Mon, 05 Jan 2026 12:24:36 GMT recorded_with: VCR 6.2.0 diff --git a/spec/mailtrap/account_spec.rb b/spec/mailtrap/account_spec.rb index 35ddc2b..6e40608 100644 --- a/spec/mailtrap/account_spec.rb +++ b/spec/mailtrap/account_spec.rb @@ -14,7 +14,7 @@ } end - it 'creates a project with all attributes' do + it 'creates an account with all attributes' do expect(account).to match_struct( id: '123456', name: 'Account 1', From e9a617ab058ef537dbe296b5f51e6e9b25c3c9c7 Mon Sep 17 00:00:00 2001 From: DagonWat Date: Mon, 23 Feb 2026 13:49:06 +0100 Subject: [PATCH 4/5] Remove account_id parameter --- examples/accounts_api.rb | 4 +--- lib/mailtrap/accounts_api.rb | 8 ++++++++ spec/mailtrap/accounts_api_spec.rb | 3 +-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/accounts_api.rb b/examples/accounts_api.rb index a0abbb2..80ce933 100644 --- a/examples/accounts_api.rb +++ b/examples/accounts_api.rb @@ -1,12 +1,10 @@ require 'mailtrap' client = Mailtrap::Client.new(api_key: 'your-api-key') -account_id = 3229 -accounts = Mailtrap::AccountsAPI.new(account_id, client) +accounts = Mailtrap::AccountsAPI.new(client) # Set your API credentials as environment variables # export MAILTRAP_API_KEY='your-api-key' -# export MAILTRAP_ACCOUNT_ID=your-account-id # accounts = Mailtrap::AccountsAPI.new diff --git a/lib/mailtrap/accounts_api.rb b/lib/mailtrap/accounts_api.rb index 056e4b6..07a48d5 100644 --- a/lib/mailtrap/accounts_api.rb +++ b/lib/mailtrap/accounts_api.rb @@ -9,6 +9,14 @@ class AccountsAPI self.response_class = Account + attr_reader :client + + # @param client [Mailtrap::Client] The client instance + # @raise [ArgumentError] If account_id is nil + def initialize(client = Mailtrap::Client.new) + @client = client + end + # Lists all accounts # @return [Array] Array of accounts # @!macro api_errors diff --git a/spec/mailtrap/accounts_api_spec.rb b/spec/mailtrap/accounts_api_spec.rb index f9bf109..eddc79b 100644 --- a/spec/mailtrap/accounts_api_spec.rb +++ b/spec/mailtrap/accounts_api_spec.rb @@ -1,9 +1,8 @@ # frozen_string_literal: true RSpec.describe Mailtrap::AccountsAPI, :vcr do - subject(:accounts_api) { described_class.new(account_id, client) } + subject(:accounts_api) { described_class.new(client) } - let(:account_id) { ENV.fetch('MAILTRAP_ACCOUNT_ID', 1_111_111) } let(:client) { Mailtrap::Client.new(api_key: ENV.fetch('MAILTRAP_API_KEY', 'local-api-key')) } describe '#list' do From 691d1f47d6c624eead118da9574993d9201914c2 Mon Sep 17 00:00:00 2001 From: DagonWat Date: Mon, 23 Feb 2026 16:36:48 +0100 Subject: [PATCH 5/5] Fix mailtrap imports --- lib/mailtrap.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/mailtrap.rb b/lib/mailtrap.rb index 9235686..134ed05 100644 --- a/lib/mailtrap.rb +++ b/lib/mailtrap.rb @@ -5,17 +5,17 @@ require_relative 'mailtrap/errors' require_relative 'mailtrap/version' require_relative 'mailtrap/accounts_api' +require_relative 'mailtrap/email_templates_api' require_relative 'mailtrap/contacts_api' require_relative 'mailtrap/contact_lists_api' require_relative 'mailtrap/contact_fields_api' require_relative 'mailtrap/contact_imports_api' -require_relative 'mailtrap/email_templates_api' -require_relative 'mailtrap/inboxes_api' +require_relative 'mailtrap/suppressions_api' +require_relative 'mailtrap/sending_domains_api' require_relative 'mailtrap/projects_api' +require_relative 'mailtrap/inboxes_api' require_relative 'mailtrap/sandbox_messages_api' require_relative 'mailtrap/sandbox_attachments_api' -require_relative 'mailtrap/suppressions_api' -require_relative 'mailtrap/sending_domains_api' module Mailtrap # @!macro api_errors