Class: Bitstamper::Models::Balance

Inherits:
Base
  • Object
show all
Defined in:
lib/bitstamper/models/balance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attributes

Constructor Details

#initialize(hash) ⇒ Balance

Returns a new instance of Balance.



6
7
8
9
10
# File 'lib/bitstamper/models/balance.rb', line 6

def initialize(hash)
  hash.each do |key, value|
    self.send("#{key}=", value) if self.respond_to?(key)
  end
end

Instance Attribute Details

#availableObject

Returns the value of attribute available.



4
5
6
# File 'lib/bitstamper/models/balance.rb', line 4

def available
  @available
end

#balanceObject

Returns the value of attribute balance.



4
5
6
# File 'lib/bitstamper/models/balance.rb', line 4

def balance
  @balance
end

#currencyObject

Returns the value of attribute currency.



4
5
6
# File 'lib/bitstamper/models/balance.rb', line 4

def currency
  @currency
end

#feesObject

Returns the value of attribute fees.



4
5
6
# File 'lib/bitstamper/models/balance.rb', line 4

def fees
  @fees
end

#reservedObject

Returns the value of attribute reserved.



4
5
6
# File 'lib/bitstamper/models/balance.rb', line 4

def reserved
  @reserved
end

Class Method Details

.parse(hash) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bitstamper/models/balance.rb', line 12

def self.parse(hash)
  parsed                  =   []
  groups                  =   {}
  
  hash.each do |key, value|
    if key !~ /_fee$/i
      splitted            =   key.split("_")

      if splitted.size == 2
        currency           =   splitted.first
        value_key          =   splitted.last

        groups[currency]  ||=   {}
        groups[currency][value_key] = value&.to_f
      end
    end
  end
  
  hash.each do |key, value|
    if key =~ /_fee$/i
      pair                =   key.split("_")&.first #btceur_fee -> btceur
      
      groups.keys.each do |grouped_key|
        if pair =~ /^#{grouped_key}/i
          quote_key       =   pair.gsub(grouped_key, "")
          groups[grouped_key]["fees"] ||= {}
          groups[grouped_key]["fees"][quote_key] = value&.to_f
        end
      end
    end
  end
  
  groups                  =   groups.deep_symbolize_keys

  groups.each do |key, values|
    values[:currency]     =   key.to_s.upcase
    parsed               <<   ::Bitstamper::Models::Balance.new(values)
  end
  
  return parsed
end