Class: Bitstamper::Models::OrderBook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ OrderBook

Returns a new instance of OrderBook.



6
7
8
9
10
11
12
13
# File 'lib/bitstamper/models/order_book.rb', line 6

def initialize(hash)
  self.bids   =   []
  self.asks   =   []
  
  self.timestamp  =  ::Bitstamper::Utilities.epoch_to_time(hash.fetch("timestamp", nil)) if hash.has_key?("timestamp") && !hash.fetch("timestamp", nil).to_s.empty?
  
  process(hash)
end

Instance Attribute Details

#asksObject

Returns the value of attribute asks.



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

def asks
  @asks
end

#bidsObject

Returns the value of attribute bids.



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

def bids
  @bids
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Instance Method Details

#process(hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/bitstamper/models/order_book.rb', line 15

def process(hash)
  [:bids, :asks].each do |type|
    hash.fetch(type.to_s, []).each do |item|
      price     =   item&.first&.to_f
      quantity  =   item&.last&.to_f
      value     =   !price.nil? && !quantity.nil? ? price * quantity : nil
      self.send(type).send(:<<, {price: price, quantity: quantity, value: value})
    end
  end
end