Method: CBX::Feed#initialize

Defined in:
lib/cbx/feed.rb

#initialize(on_message, on_close = nil, on_error = nil) ⇒ Feed

Returns a new instance of Feed.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cbx/feed.rb', line 7

def initialize(on_message, on_close = nil, on_error = nil)
  ws = WebSocket::Client::Simple.connect API_URL
  @ws = ws
  ws.on :message do |event|
    msg = JSON.parse event.data
    on_message.call(msg)
  end

  ws.on :open do
    ws.send SUBSCRIPTION_REQUEST.to_json
  end

  ws.on :close do |e|
    on_close && on_close.call(e)
    exit 1
  end

  ws.on :error do |e|
    on_error && on_error.call(e)
  end
end