Class: Coinbase::BalanceMap

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

Overview

A convenience class for printing out Asset balances in a human-readable format.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_balances(balances) ⇒ BalanceMap

Converts a list of Coinbase::Client::Balance models to a Coinbase::BalanceMap.

Parameters:

Returns:

  • (BalanceMap)

    The converted BalanceMap object.



11
12
13
14
15
16
17
18
19
# File 'lib/coinbase/balance_map.rb', line 11

def self.from_balances(balances)
  BalanceMap.new.tap do |balance_map|
    balances.each do |balance_model|
      balance = Coinbase::Balance.from_model(balance_model)

      balance_map.add(balance)
    end
  end
end

Instance Method Details

#add(balance) ⇒ Object

Adds a balance to the map.

Parameters:

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/coinbase/balance_map.rb', line 23

def add(balance)
  raise ArgumentError, 'balance must be a Coinbase::Balance' unless balance.is_a?(Coinbase::Balance)

  self[balance.asset_id] = balance.amount
end

#inspectString

Returns a string representation of the balance map.

Returns:

  • (String)

    The string representation of the balance map



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

def inspect
  to_string
end

#to_sString

Returns a string representation of the balance map.

Returns:

  • (String)

    The string representation of the balance map



31
32
33
# File 'lib/coinbase/balance_map.rb', line 31

def to_s
  to_string
end