Class: Airwallex::Balance

Inherits:
APIResource show all
Extended by:
APIOperations::List
Defined in:
lib/airwallex/resources/balance.rb

Overview

Balance resource for account balance queries

Query account balances across all currencies or for specific currencies. Shows available, pending, and reserved amounts.

Examples:

Get all balances

balances = Airwallex::Balance.list
balances.each do |balance|
  puts "#{balance.currency}: #{balance.available_amount}"
end

Get specific currency balance

usd_balance = Airwallex::Balance.retrieve('USD')
puts "Available: #{usd_balance.available_amount}"
puts "Pending: #{usd_balance.pending_amount}"
puts "Reserved: #{usd_balance.reserved_amount}"

Instance Attribute Summary

Attributes inherited from APIResource

#attributes, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::List

list

Methods inherited from APIResource

#changed_attributes, #dirty?, #initialize, #inspect, #method_missing, #refresh, #refresh_from, resource_name, #respond_to_missing?, #to_hash, #to_json, #to_s

Constructor Details

This class inherits a constructor from Airwallex::APIResource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Airwallex::APIResource

Class Method Details

.resource_pathObject



24
25
26
# File 'lib/airwallex/resources/balance.rb', line 24

def self.resource_path
  "/api/v1/balances/current"
end

.retrieve(currency) ⇒ Airwallex::Balance

Retrieve balance for a specific currency

Parameters:

  • currency (String)

    Currency code (e.g., ‘USD’, ‘EUR’)

Returns:

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/airwallex/resources/balance.rb', line 32

def self.retrieve(currency)
  response = Airwallex.client.get(resource_path, currency: currency)
  # Balance API returns array directly at top level
  balances_array = response.is_a?(Array) ? response : response[:data] || response["data"] || []
  balances = ListObject.new(
    data: balances_array,
    has_more: false,
    resource_class: self
  )

  # Filter to find the requested currency
  balance = balances.find { |b| b.currency&.upcase == currency.upcase }
  raise NotFoundError, "Balance not found for currency: #{currency}" unless balance

  balance
end

Instance Method Details

#total_amountFloat

Calculate total balance

Returns:

  • (Float)

    Sum of available, pending, and reserved amounts



52
53
54
55
56
57
# File 'lib/airwallex/resources/balance.rb', line 52

def total_amount
  available = respond_to?(:available_amount) ? available_amount.to_f : 0.0
  pending = respond_to?(:pending_amount) ? pending_amount.to_f : 0.0
  reserved = respond_to?(:reserved_amount) ? reserved_amount.to_f : 0.0
  (available + pending + reserved).round(2)
end