Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions examples/api_tokens_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'mailtrap'

account_id = 3229
client = Mailtrap::Client.new(api_key: 'your-api-key')
api_tokens = Mailtrap::ApiTokensAPI.new(account_id, client)

# List API tokens
api_tokens.list
# => [#<struct Mailtrap::ApiToken id=12345, name="My API Token", last_4_digits="x7k9", ..., token=nil>, ...]
15 changes: 15 additions & 0 deletions examples/permissions_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'mailtrap'

account_id = 3229
client = Mailtrap::Client.new(api_key: 'your-api-key')
permissions = Mailtrap::PermissionsAPI.new(account_id, client)

# Bulk-update user/token permissions on an account access. Combine create/update with destroy:
permissions.bulk_update(
5142, # account_access_id
[
{ resource_id: '3281', resource_type: 'account', access_level: 'viewer' },
{ resource_id: '3809', resource_type: 'inbox', _destroy: true }
]
)
# => { message: "Permissions have been updated!" }
2 changes: 2 additions & 0 deletions lib/mailtrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require_relative 'mailtrap/version'
require_relative 'mailtrap/accounts_api'
require_relative 'mailtrap/account_accesses_api'
require_relative 'mailtrap/api_tokens_api'
require_relative 'mailtrap/permissions_api'
require_relative 'mailtrap/billing_api'
require_relative 'mailtrap/email_templates_api'
require_relative 'mailtrap/contacts_api'
Expand Down
22 changes: 22 additions & 0 deletions lib/mailtrap/api_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Mailtrap
# Data Transfer Object for API Token
# @attr_reader id [Integer] The API token ID
# @attr_reader name [String] Token display name
# @attr_reader last_4_digits [String] Last 4 characters of the token
# @attr_reader created_by [String] Name of the user or token that created this token
# @attr_reader expires_at [String, nil] When the token expires (ISO 8601); nil if it does not expire
# @attr_reader resources [Array<Hash>] Permissions granted to this token
# @attr_reader token [String, nil] Full token value — only populated by #create and #reset
ApiToken = Struct.new(
:id,
:name,
:last_4_digits,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it called digits, while its chars? Sounds not so good!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♂️

:created_by,
:expires_at,
:resources,
:token,
keyword_init: true
)
end
25 changes: 25 additions & 0 deletions lib/mailtrap/api_tokens_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 'api_token'

module Mailtrap
class ApiTokensAPI
include BaseAPI

self.response_class = ApiToken

# Lists API tokens visible to the current API token
# @return [Array<ApiToken>] Array of API tokens
# @!macro api_errors
def list
base_list
end

private

def base_path
"/api/accounts/#{account_id}/api_tokens"
end
end
end
16 changes: 16 additions & 0 deletions lib/mailtrap/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ def patch(path, body = nil)
)
end

# Performs a PUT request to the specified path
# @param path [String] The request path
# @param body [Hash] The request body
# @return [Hash, String, nil] JSON response or raw response body
# @!macro api_errors
def put(path, body = nil)
perform_request(
method: :put,
host: general_api_host,
path:,
body:
)
end

# Performs a DELETE request to the specified path
# @param path [String] The request path
# @return [Hash, String, nil] JSON response or raw response body
Expand Down Expand Up @@ -261,6 +275,8 @@ def setup_request(method, uri_or_path, body = nil)
Net::HTTP::Post.new(uri_or_path)
when :patch
Net::HTTP::Patch.new(uri_or_path)
when :put
Net::HTTP::Put.new(uri_or_path)
when :delete
Net::HTTP::Delete.new(uri_or_path)
else
Expand Down
29 changes: 29 additions & 0 deletions lib/mailtrap/permissions_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative 'base_api'

module Mailtrap
class PermissionsAPI
include BaseAPI

# Bulk-updates user or token permissions on an account access
# @param account_access_id [Integer] The account access ID
# @param permissions [Array<Hash>] Array of permission entries
# - `{ resource_id:, resource_type:, access_level: }` to create or update
# - `{ resource_id:, resource_type:, _destroy: true }` to remove
# @return [Hash] API response (e.g. `{ message: 'Permissions have been updated!' }`)
# @!macro api_errors
def bulk_update(account_access_id, permissions)
client.put(
"#{base_path}/account_accesses/#{account_access_id}/permissions/bulk",
{ permissions: permissions }
)
end

private

def base_path
"/api/accounts/#{account_id}"
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.

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

23 changes: 23 additions & 0 deletions spec/mailtrap/api_token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.describe Mailtrap::ApiToken do
describe '#initialize' do
subject(:api_token) { described_class.new(attributes) }

let(:attributes) do
{
id: 12_345,
name: 'My API Token',
last_4_digits: 'x7k9',
created_by: 'user@example.com',
expires_at: nil,
resources: [{ resource_type: 'account', resource_id: 3229, access_level: 100 }],
token: 'a1b2c3d4e5f6g7h8'
}
end

it 'creates an api token with all attributes' do
expect(api_token).to have_attributes(attributes)
end
end
end
Loading
Loading