Class: Bibox::Websocket::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bibox/websocket/client.rb

Overview

Websocket client for Bibox

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bibox/websocket/client.rb', line 7

def initialize(options: {})
  self.url          =   options.fetch(:url, "wss://push.bibox.com")
  self.prefix       =   options.fetch(:prefix, "bibox_sub_spot_")
  self.keepalive    =   options.fetch(:keepalive, false)
  
  self.matchers     =   {
    trading_pairs:          /\d?[A-Z]+_[A-Z]+_market/,
    order_book:             /\d?[A-Z]+_[A-Z]+_depth$/,
    trades:                 /\d?[A-Z]+_[A-Z]+_deals$/,
    ticker:                 /\d?[A-Z]+_[A-Z]+_ticker$/,
    klines:                 /\d?[A-Z]+_[A-Z]+_kline_(?<period>.*)$/,
    
    index_markets:          /\d?[A-Z]+_[A-Z]+_indexMarket/,
    contract_price_limits:  /\d?[A-Z]+_[A-Z]+_contractPriceLimit$/,
  }
  
  self.callbacks    =   {
    subscribe:              ->  { subscribe! },
    message:                ->  (data) { message(data) },
    trading_pairs:          ->  (data) { nil },
    order_book:             ->  (data) { nil },
    trades:                 ->  (data) { nil },
    ticker:                 ->  (data) { nil },
    klines:                 ->  (data) { nil },
    
    index_markets:          ->  (data) { nil },
    contract_price_limits:  ->  (data) { nil },
  }
end

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



5
6
7
# File 'lib/bibox/websocket/client.rb', line 5

def callbacks
  @callbacks
end

#keepaliveObject

Returns the value of attribute keepalive.



5
6
7
# File 'lib/bibox/websocket/client.rb', line 5

def keepalive
  @keepalive
end

#matchersObject

Returns the value of attribute matchers.



5
6
7
# File 'lib/bibox/websocket/client.rb', line 5

def matchers
  @matchers
end

#prefixObject

Returns the value of attribute prefix.



5
6
7
# File 'lib/bibox/websocket/client.rb', line 5

def prefix
  @prefix
end

#reactor_ownerObject

Returns the value of attribute reactor_owner.



5
6
7
# File 'lib/bibox/websocket/client.rb', line 5

def reactor_owner
  @reactor_owner
end

#socketObject

Returns the value of attribute socket.



5
6
7
# File 'lib/bibox/websocket/client.rb', line 5

def socket
  @socket
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/bibox/websocket/client.rb', line 5

def url
  @url
end

Instance Method Details

#channel_name(channel) ⇒ Object



106
107
108
# File 'lib/bibox/websocket/client.rb', line 106

def channel_name(channel)
  "#{self.prefix}#{channel}"
end

#login!Object



65
66
67
# File 'lib/bibox/websocket/client.rb', line 65

def login!
  subscribe_to! channel_name("ALL_ALL_login")
end

#message(data) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bibox/websocket/client.rb', line 110

def message(data)
  channel   =   data.fetch("channel", nil)&.gsub(self.prefix, "")
  
  case channel
    when self.matchers[:trading_pairs]
      self.callbacks[:trading_pairs].call(data)
    when self.matchers[:order_book]
      self.callbacks[:order_book].call(data)
    when self.matchers[:trades]
      self.callbacks[:trades].call(data)
    when self.matchers[:ticker]
      self.callbacks[:ticker].call(data)
    when self.matchers[:klines]
      period = channel.match(self.matchers[:klines])&.[](:period)
      data.merge!("period" => period) if !period.to_s.empty?
      self.callbacks[:klines].call(data)
    when self.matchers[:index_markets]
      self.callbacks[:index_markets].call(data)
    when self.matchers[:contract_price_limits]
      self.callbacks[:contract_price_limits].call(data)
  end
end

#refresh!Object



57
58
59
60
61
62
63
# File 'lib/bibox/websocket/client.rb', line 57

def refresh!
  self.socket             =   Faye::WebSocket::Client.new(self.url)
  self.socket.onopen      =   method(:on_open)
  self.socket.onmessage   =   method(:on_message)
  self.socket.onclose     =   method(:on_close)
  self.socket.onerror     =   method(:on_error)
end

#start!Object



37
38
39
40
41
42
43
44
45
# File 'lib/bibox/websocket/client.rb', line 37

def start!
  if EventMachine.reactor_running?
    self.reactor_owner    =   false
    refresh!
  else
    self.reactor_owner    =   true
    EM.run { refresh! }
  end
end

#stop!Object



47
48
49
50
51
52
53
54
55
# File 'lib/bibox/websocket/client.rb', line 47

def stop!
  if self.reactor_owner == true
    self.socket.onclose   =   -> (_event) { EM.stop }
  else
    self.socket.onclose   =   -> (_event) { nil }
  end

  self.socket.close
end

#subscribe_to!(channel, binary: 0) ⇒ Object



101
102
103
104
# File 'lib/bibox/websocket/client.rb', line 101

def subscribe_to!(channel, binary: 0)
  message = { event: "addChannel", channel: channel, binary: binary }
  self.socket.send(message.to_json)
end

#subscribe_to_contract_price_limits!Object



77
78
79
# File 'lib/bibox/websocket/client.rb', line 77

def subscribe_to_contract_price_limits!
  subscribe_to! channel_name("ALL_ALL_contractPriceLimit")
end

#subscribe_to_index_markets!Object



73
74
75
# File 'lib/bibox/websocket/client.rb', line 73

def subscribe_to_index_markets!
  subscribe_to! channel_name("ALL_ALL_indexMarket")
end

#subscribe_to_klines!(pairs: [], periods: ["5min"]) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/bibox/websocket/client.rb', line 93

def subscribe_to_klines!(pairs: [], periods: ["5min"])
  pairs&.each do |pair|
    periods&.each do |period|
      subscribe_to! channel_name("#{pair}_kline_#{period}")
    end
  end
end

#subscribe_to_order_book!(pairs: []) ⇒ Object



81
82
83
# File 'lib/bibox/websocket/client.rb', line 81

def subscribe_to_order_book!(pairs: [])
  pairs&.each { |pair| subscribe_to! channel_name("#{pair}_depth") }
end

#subscribe_to_ticker!(pairs: []) ⇒ Object



89
90
91
# File 'lib/bibox/websocket/client.rb', line 89

def subscribe_to_ticker!(pairs: [])
  pairs&.each { |pair| subscribe_to! channel_name("#{pair}_ticker") }
end

#subscribe_to_trades!(pairs: []) ⇒ Object



85
86
87
# File 'lib/bibox/websocket/client.rb', line 85

def subscribe_to_trades!(pairs: [])
  pairs&.each { |pair| subscribe_to! channel_name("#{pair}_deals") }
end

#subscribe_to_trading_pairs!Object



69
70
71
# File 'lib/bibox/websocket/client.rb', line 69

def subscribe_to_trading_pairs!
  subscribe_to! channel_name("ALL_ALL_market")
end