Class: Coinbase::Balance

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/balance.rb

Overview

A representation of an Balance.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, asset_id:) ⇒ Balance

Returns a new Balance object. Do not use this method. Instead, use Balance.from_model or Balance.from_model_and_asset_id.

Parameters:

  • amount (BigDecimal)

    The amount of the Asset

  • asset_id (Symbol)

    The Asset ID



32
33
34
35
# File 'lib/coinbase/balance.rb', line 32

def initialize(amount:, asset_id:)
  @amount = amount
  @asset_id = asset_id
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



37
38
39
# File 'lib/coinbase/balance.rb', line 37

def amount
  @amount
end

#asset_idObject (readonly)

Returns the value of attribute asset_id.



37
38
39
# File 'lib/coinbase/balance.rb', line 37

def asset_id
  @asset_id
end

Class Method Details

.from_model(balance_model) ⇒ Balance

Converts a Coinbase::Client::Balance model to a Coinbase::Balance

Parameters:

Returns:

  • (Balance)

    The converted Balance object.



9
10
11
12
13
# File 'lib/coinbase/balance.rb', line 9

def self.from_model(balance_model)
  asset_id = Coinbase.to_sym(balance_model.asset.asset_id.downcase)

  from_model_and_asset_id(balance_model, asset_id)
end

.from_model_and_asset_id(balance_model, asset_id) ⇒ Balance

Converts a Coinbase::Client::Balance model and asset ID to a Coinbase::Balance This can be used to specify a non-primary denomination that we want the balance to be converted to.

Parameters:

  • balance_model (Coinbase::Client::Balance)

    The balance fetched from the API.

  • asset_id (Symbol)

    The Asset ID of the denomination we want returned.

Returns:

  • (Balance)

    The converted Balance object.



21
22
23
24
25
26
# File 'lib/coinbase/balance.rb', line 21

def self.from_model_and_asset_id(balance_model, asset_id)
  new(
    amount: Coinbase::Asset.from_atomic_amount(BigDecimal(balance_model.amount), asset_id),
    asset_id: asset_id
  )
end

Instance Method Details

#inspectString

Same as to_s.

Returns:

  • (String)

    a string representation of the Balance



47
48
49
# File 'lib/coinbase/balance.rb', line 47

def inspect
  to_s
end

#to_sString

Returns a string representation of the Balance.

Returns:

  • (String)

    a string representation of the Balance



41
42
43
# File 'lib/coinbase/balance.rb', line 41

def to_s
  "Coinbase::Balance{amount: '#{amount.to_i}', asset_id: '#{asset_id}'}"
end