Class: Sendly::AccountResource

Inherits:
Object
  • Object
show all
Defined in:
lib/sendly/account_resource.rb

Overview

Account resource for accessing account information, credits, and API keys

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ AccountResource

Returns a new instance of AccountResource.

Parameters:



7
8
9
# File 'lib/sendly/account_resource.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#api_key(key_id) ⇒ Sendly::ApiKey

Get a specific API key by ID

Parameters:

  • key_id (String)

    API key ID

Returns:



57
58
59
60
# File 'lib/sendly/account_resource.rb', line 57

def api_key(key_id)
  response = @client.get("/keys/#{key_id}")
  ApiKey.new(response)
end

#api_key_usage(key_id) ⇒ Hash

Get usage statistics for an API key

Parameters:

  • key_id (String)

    API key ID

Returns:

  • (Hash)

    Usage statistics



66
67
68
# File 'lib/sendly/account_resource.rb', line 66

def api_key_usage(key_id)
  @client.get("/keys/#{key_id}/usage")
end

#api_keysArray<Sendly::ApiKey>

List API keys for the account

Returns:



48
49
50
51
# File 'lib/sendly/account_resource.rb', line 48

def api_keys
  response = @client.get("/keys")
  response.map { |data| ApiKey.new(data) }
end

#create_api_key(name, expires_at: nil) ⇒ Hash

Create a new API key

Examples:

result = client..create_api_key("Production")
puts "Save this key: #{result['key']}"  # Only shown once!

Parameters:

  • name (String)

    Display name for the API key

  • expires_at (String, nil) (defaults to: nil)

    Optional expiration date (ISO 8601)

Returns:

  • (Hash)

    Contains 'apiKey' (metadata) and 'key' (full secret - only shown once!)

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
# File 'lib/sendly/account_resource.rb', line 79

def create_api_key(name, expires_at: nil)
  raise ArgumentError, "API key name is required" if name.nil? || name.empty?

  body = { name: name }
  body[:expiresAt] = expires_at if expires_at

  @client.post("/account/keys", body)
end

#creditsSendly::Credits

Get credit balance

Examples:

credits = client..credits
puts "Available: #{credits.available_balance} credits"

Returns:



26
27
28
29
# File 'lib/sendly/account_resource.rb', line 26

def credits
  response = @client.get("/credits")
  Credits.new(response)
end

#getSendly::Account

Get account information

Returns:



14
15
16
17
# File 'lib/sendly/account_resource.rb', line 14

def get
  response = @client.get("/account")
  Account.new(response)
end

#revoke_api_key(key_id) ⇒ void

This method returns an undefined value.

Revoke an API key

Parameters:

  • key_id (String)

    API key ID to revoke

Raises:

  • (ArgumentError)


92
93
94
95
96
# File 'lib/sendly/account_resource.rb', line 92

def revoke_api_key(key_id)
  raise ArgumentError, "API key ID is required" if key_id.nil? || key_id.empty?

  @client.delete("/account/keys/#{key_id}")
end

#rotate_api_key(key_id, grace_period_hours: nil) ⇒ Hash

Rotate an API key.

Issues a new key that inherits the old key's scopes and type, and puts the old key into a grace period (default 24 hours; 24-168 inclusive) during which BOTH keys work — deploy the new key, then let the old one expire. The new key's raw secret is returned only once, at result["newKey"]["key"]; store it now.

Examples:

result = client..rotate_api_key('key_xxx', grace_period_hours: 72)
puts result['newKey']['key']  # Save this — shown once!
puts result['message']        # "Old key will expire in 72 hours"

Parameters:

  • key_id (String)

    ID of the API key to rotate

  • grace_period_hours (Integer, nil) (defaults to: nil)

    Hours the old key stays valid (24-168 inclusive; the API defaults to 24 when omitted)

Returns:

  • (Hash)

    { "newKey" => { ..., "key" => "sk_...", "warning" => ... }, "oldKey" => { ... }, "message" => ... }

Raises:

  • (ArgumentError)

    If key_id is missing

  • (Sendly::ValidationError)

    If grace_period_hours is outside 24-168, or the key cannot be rotated (inactive/revoked/already-rotating/predecessor still in grace)

  • (Sendly::NotFoundError)

    If the key does not exist



120
121
122
123
124
125
126
127
# File 'lib/sendly/account_resource.rb', line 120

def rotate_api_key(key_id, grace_period_hours: nil)
  raise ArgumentError, "API key ID is required" if key_id.nil? || key_id.empty?

  body = {}
  body[:gracePeriodHours] = grace_period_hours unless grace_period_hours.nil?

  @client.post("/account/keys/#{key_id}/rotate", body)
end

#transactions(limit: nil, offset: nil) ⇒ Array<Sendly::CreditTransaction>

Get credit transaction history

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    Maximum number of transactions to return

  • offset (Integer, nil) (defaults to: nil)

    Number of transactions to skip

Returns:



36
37
38
39
40
41
42
43
# File 'lib/sendly/account_resource.rb', line 36

def transactions(limit: nil, offset: nil)
  params = {}
  params[:limit] = limit if limit
  params[:offset] = offset if offset

  response = @client.get("/credits/transactions", params)
  response.map { |data| CreditTransaction.new(data) }
end

#transfer_credits(target_organization_id:, amount:) ⇒ Object

Raises:

  • (ArgumentError)


129
130
131
132
133
134
135
136
137
# File 'lib/sendly/account_resource.rb', line 129

def transfer_credits(target_organization_id:, amount:)
  raise ArgumentError, "Target organization ID is required" if target_organization_id.nil? || target_organization_id.empty?
  raise ArgumentError, "Amount must be a positive integer" if !amount.is_a?(Integer) || amount <= 0

  @client.post("/credits/transfer", {
    targetOrganizationId: target_organization_id,
    amount: amount
  })
end