Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mailtrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/mailtrap/account.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions lib/mailtrap/accounts_api.rb
Original file line number Diff line number Diff line change
@@ -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<Account>] Array of accounts
# @!macro api_errors
def list
base_list
end

private

def base_path
'/api/accounts'
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions spec/mailtrap/account_spec.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions spec/mailtrap/accounts_api_spec.rb
Original file line number Diff line number Diff line change
@@ -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