Module: RTCBX::Orderbook::BookMethods

Included in:
RTCBX::Orderbook
Defined in:
lib/rtcbx/orderbook/book_methods.rb

Overview

This class provides methods to apply updates to the state of the orderbook as they are received by the websocket.

Constant Summary collapse

BIGDECIMAL_KEYS =

Names of attributes that should be converted to BigDecimal

%w(size old_size new_size remaining_size price)

Instance Method Summary collapse

Instance Method Details

#apply(msg) ⇒ Object

Applies a message to an Orderbook object by making relevant changes to @bids, @asks, and @last_sequence.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rtcbx/orderbook/book_methods.rb', line 16

def apply(msg)
  return if msg.fetch('sequence') != @last_sequence + 1
  #if msg.fetch('sequence') != @last_sequence + 1
  #  puts "Expected #{@last_sequence + 1}, got #{msg.fetch('sequence')}"
  #  @websocket.stop!
  #end

  @last_sequence = msg.fetch('sequence')
  BIGDECIMAL_KEYS.each do |key|
    msg[key] = BigDecimal.new(msg.fetch(key)) if msg.fetch(key, false)
  end

  __send__(msg.fetch('type'), msg)
end