class Coinbasepro::Api::Websocket
Websocket
client for Coinbase Pro
Public Class Methods
new(options = {})
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 5 def initialize(options = {}) @ws_url = options[:ws_url] || "wss://ws-feed.pro.coinbase.com" @product = options[:product_id] || 'BTC-USD' @keepalive = options[:keepalive] || false @message_cb = ->(_data) { nil } @received_cb = ->(_data) { nil } @open_cb = ->(_data) { nil } @match_cb = ->(_data) { nil } @change_cb = ->(_data) { nil } @done_cb = ->(_data) { nil } @error_cb = ->(_data) { nil } end
Public Instance Methods
change(&block)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 75 def change(&block) @change_cb = block end
done(&block)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 79 def done(&block) @done_cb = block end
error(&block)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 83 def error(&block) @error_cb = block end
match(&block)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 71 def match(&block) @match_cb = block end
message(&block)
click to toggle source
Run this before processing every message
# File lib/coinbasepro/api/websocket.rb, line 59 def message(&block) @message_cb = block end
open(&block)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 67 def open(&block) @open_cb = block end
ping(options = {}) { |resp| ... }
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 51 def ping(options = {}) msg = options[:payload] || Time.now.to_s @socket.ping(msg) do |resp| yield(resp) if block_given? end end
received(&block)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 63 def received(&block) @received_cb = block end
refresh!()
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 38 def refresh! @socket = Faye::WebSocket::Client.new(@ws_url) @socket.onopen = method(:ws_opened) @socket.onmessage = method(:ws_received) @socket.onclose = method(:ws_closed) @socket.onerror = method(:ws_error) end
start!()
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 19 def start! if EventMachine.reactor_running? @reactor_owner = false refresh! else @reactor_owner = true EM.run { refresh! } end end
stop!()
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 29 def stop! if @reactor_owner == true @socket.onclose = ->(_event) { EM.stop } else @socket.onclose = ->(_event) { nil } end @socket.close end
subscribe!(options = {})
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 46 def subscribe!(options = {}) product = options[:product_id] || @product @socket.send({ type: 'subscribe', product_id: product }.to_json) end
Private Instance Methods
ws_closed(_event)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 106 def ws_closed(_event) if @keepalive refresh! else EM.stop end end
ws_error(event)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 114 def ws_error(event) fail WebsocketError, event.data end
ws_opened(_event)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 89 def ws_opened(_event) subscribe! end
ws_received(event)
click to toggle source
# File lib/coinbasepro/api/websocket.rb, line 93 def ws_received(event) data = APIObject.new(JSON.parse(event.data)) @message_cb.call(data) case data['type'] when 'received' then @received_cb.call(data) when 'open' then @open_cb.call(data) when 'match' then @match_cb.call(data) when 'change' then @change_cb.call(data) when 'done' then @done_cb.call(data) when 'error' then @error_cb.call(data) end end