Class: Mailtrap::ApiTokensAPI

Inherits:
Object
  • Object
show all
Includes:
BaseAPI
Defined in:
lib/mailtrap/api_tokens_api.rb

Instance Attribute Summary

Attributes included from BaseAPI

#account_id, #client

Instance Method Summary collapse

Methods included from BaseAPI

included, #initialize

Instance Method Details

#create(options) ⇒ ApiToken

Creates a new API token. The full token value is returned ONLY ONCE — store it securely.

Parameters:

  • options (Hash)

    The parameters to create

Options Hash (options):

  • :name (String)

    Display name for the token

  • :expires_at (String, nil)

    Optional token expiration as an ISO 8601 date-time. Omit for the server default (a 1-year default is being rolled out). Pass explicit nil for a token that never expires. Past or more-than-5-years-ahead values are rejected with 422

  • :resources (Array<Hash>)

    Permissions to assign

    • { resource_type:, resource_id:, access_level: }

Returns:

  • (ApiToken)

    Created token (full token value populated)

Raises:



41
42
43
# File 'lib/mailtrap/api_tokens_api.rb', line 41

def create(options)
  base_create(options)
end

#delete(token_id) ⇒ Object

Permanently deletes an API token

Parameters:

  • token_id (Integer)

    The API token ID

Returns:

  • nil

Raises:



67
68
69
# File 'lib/mailtrap/api_tokens_api.rb', line 67

def delete(token_id)
  base_delete(token_id)
end

#get(token_id) ⇒ ApiToken

Retrieves a single API token by ID

Parameters:

  • token_id (Integer)

    The API token ID

Returns:

  • (ApiToken)

    API token object (the token field is nil — full value is only returned by #create and #reset)

Raises:



26
27
28
# File 'lib/mailtrap/api_tokens_api.rb', line 26

def get(token_id)
  base_get(token_id)
end

#listArray<ApiToken>

Lists API tokens visible to the current API token

Returns:

  • (Array<ApiToken>)

    Array of API tokens

Raises:



17
18
19
# File 'lib/mailtrap/api_tokens_api.rb', line 17

def list
  base_list
end

#reset(token_id, options = {}) ⇒ ApiToken

Expires the requested token and returns a new one with the same permissions. The old token stops working after a short grace period. The new token value is returned ONLY ONCE — store it securely

Parameters:

  • token_id (Integer)

    The API token ID

  • options (Hash) (defaults to: {})

    The reset parameters

Options Hash (options):

  • :expires_at (String, nil)

    Optional token expiration as an ISO 8601 date-time. Omit for the server default (a 1-year default is being rolled out). Pass explicit nil for a token that never expires. Past or more-than-5-years-ahead values are rejected with 422

Returns:

  • (ApiToken)

    New token (full token value populated)

Raises:



56
57
58
59
60
61
# File 'lib/mailtrap/api_tokens_api.rb', line 56

def reset(token_id, options = {})
  validate_options!(options, %i[expires_at])
  # An empty hash must not be sent as a body — the endpoint historically takes no body
  response = client.post("#{base_path}/#{token_id}/reset", options.empty? ? nil : options)
  handle_response(response)
end