Class: Kucoin::Models::OrderBook

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

Constant Summary collapse

ARRAY_MAPPING =
{
  0 => :price,
  1 => :amount,
  2 => :volume
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, type: :nil) ⇒ OrderBook

Returns a new instance of OrderBook.



12
13
14
15
16
17
18
19
20
# File 'lib/kucoin/models/order_book.rb', line 12

def initialize(hash, type: :nil)
  self.symbol         =   hash.fetch("symbol", nil)
  self.timestamp      =   ::Kucoin::Utilities::Parsing.epoch_to_time(hash.fetch("timestamp", nil), ms: true) if hash.has_key?("timestamp") && !hash.fetch("timestamp", nil).to_s.empty?

  self.bids           =   []
  self.asks           =   []

  process(hash.fetch("data", []), type: type)
end

Instance Attribute Details

#asksObject

Returns the value of attribute asks.



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

def asks
  @asks
end

#bidsObject

Returns the value of attribute bids.



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

def bids
  @bids
end

#symbolObject

Returns the value of attribute symbol.



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

def symbol
  @symbol
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Instance Method Details

#process(data, type: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kucoin/models/order_book.rb', line 22

def process(data, type: nil)
  pp data
  
  if data.is_a?(Hash)
    ["BUY", "SELL"].each do |type|
      process_orders(data.fetch(type.to_s, []), type: type)
    end
  elsif data.is_a?(Array)
    process_orders(data, type: type)
  end
end

#process_orders(orders, type: "BUY") ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kucoin/models/order_book.rb', line 34

def process_orders(orders, type: "BUY")
  orders.each do |item|
    data              =   {}
    
    item.each_with_index do |value, index|
      data[ARRAY_MAPPING[index]] = value
    end
    
    case type
      when "BUY"
        self.bids << data
      when "SELL"
        self.asks << data
    end
  end
end