Class: BinanceAPI::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/binance_api/stream.rb

Constant Summary collapse

BASE_ENDPOINT =
'wss://stream.binance.com:9443/'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(endpoints, on_open: nil, on_close: nil, on_error: nil, on_message: nil) ⇒ Stream

Returns a new instance of Stream.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/binance_api/stream.rb', line 6

def initialize(endpoints, on_open: nil, on_close: nil, on_error: nil, on_message: nil)
  raise 'on_open can accept a block only' if !on_open.nil? && !on_open.is_a?(Proc)
  raise 'on_close can accept a block only' if !on_close.nil? && !on_close.is_a?(Proc)
  raise 'on_error can accept a block only' if !on_error.nil? && !on_error.is_a?(Proc)
  raise 'on_data can accept a block only' if !on_message.nil? && !on_message.is_a?(Proc)

  @endpoints = Array(endpoints).reject(&:nil?).reject(&:empty?)
  raise 'endpoints cannot be empty' if @endpoints.empty?

  @on_open = on_open
  @on_close = on_close
  @on_error = on_error
  @on_message = on_message
  @stream_type = @endpoints.length > 1 ? :combined : :single
end

Instance Method Details

#closeObject



31
32
33
# File 'lib/binance_api/stream.rb', line 31

def close
  ws.close
end

#startObject



22
23
24
25
26
27
28
29
# File 'lib/binance_api/stream.rb', line 22

def start
  @ws = WebSocket::Client::Simple.connect "#{BASE_ENDPOINT}#{stream_url}"
  ws.on :message, &on_message
  ws.on :open, &on_open
  ws.on :close, &on_close
  ws.on :error, &on_error
  ws
end