Class: Cryptoexchange::Exchanges::Bitfinex::Services::OrderBookStream

Inherits:
Services::OrderBookStream show all
Defined in:
lib/cryptoexchange/exchanges/bitfinex/services/order_book_stream.rb

Instance Method Summary collapse

Instance Method Details

#parse_message(message, market_pair) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cryptoexchange/exchanges/bitfinex/services/order_book_stream.rb', line 21

def parse_message(message, market_pair)
  # CHANNEL_ID integer	Channel ID
  # PRICE	     float    Price level
  # COUNT      int      Number of orders at that price level
  # ±AMOUNT	   float    Total amount available at that price level. Positive values mean bid, negative values mean ask.
  order_book        = Cryptoexchange::Models::OrderBook.new
  order_book.base   = market_pair.base
  order_book.target = market_pair.target
  order_book.market = Bitfinex::Market::NAME

  if message[3] > 0
    order_book.bids = adapt_orders(message)
  else
    order_book.asks = adapt_orders(message)
  end

  order_book.timestamp = Time.now.to_i
  order_book.payload   = message
  order_book
end

#subscribe_event(market_pair) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/cryptoexchange/exchanges/bitfinex/services/order_book_stream.rb', line 9

def subscribe_event(market_pair)
  {
    event:   "subscribe",
    channel: "book",
    pair:    "#{market_pair.base}#{market_pair.target}"
  }.to_json
end

#urlObject



5
6
7
# File 'lib/cryptoexchange/exchanges/bitfinex/services/order_book_stream.rb', line 5

def url
  Cryptoexchange::Exchanges::Bitfinex::Market::WS_URL
end

#valid_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/cryptoexchange/exchanges/bitfinex/services/order_book_stream.rb', line 17

def valid_message?(message)
  message.is_a?(Array) && message.length == 4
end