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:, asset_id: nil) ⇒ 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) (defaults to: nil)

    The Asset ID



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

def initialize(amount:, asset:, asset_id: nil)
  @amount = amount
  @asset = asset
  @asset_id = asset_id || asset.asset_id
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#assetObject (readonly)

Returns the value of attribute asset.



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

def asset
  @asset
end

#asset_idObject (readonly)

Returns the value of attribute asset_id.



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

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 = Coinbase::Asset.from_model(balance_model.asset)

  new(amount: asset.from_atomic_amount(balance_model.amount), asset: asset)
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
27
28
29
# File 'lib/coinbase/balance.rb', line 21

def self.from_model_and_asset_id(balance_model, asset_id)
  asset = Coinbase::Asset.from_model(balance_model.asset, asset_id: asset_id)

  new(
    amount: asset.from_atomic_amount(balance_model.amount),
    asset: asset,
    asset_id: asset_id
  )
end

Instance Method Details

#inspectString

Same as to_s.

Returns:

  • (String)

    a string representation of the Balance



51
52
53
# File 'lib/coinbase/balance.rb', line 51

def inspect
  to_s
end

#to_sString

Returns a string representation of the Balance.

Returns:

  • (String)

    a string representation of the Balance



45
46
47
# File 'lib/coinbase/balance.rb', line 45

def to_s
  Coinbase.pretty_print_object(self.class, amount: amount.to_s('F'), asset_id: asset_id)
end