Class: Coinbase::Exchange::Websocket

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/exchange/websocket.rb

Overview

Websocket client for Coinbase Exchange

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Websocket

Returns a new instance of Websocket.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/coinbase/exchange/websocket.rb', line 5

def initialize(options = {})
  @ws_url = options[:ws_url] || "wss://ws-feed.gdax.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

Instance Method Details

#change(&block) ⇒ Object



75
76
77
# File 'lib/coinbase/exchange/websocket.rb', line 75

def change(&block)
  @change_cb = block
end

#done(&block) ⇒ Object



79
80
81
# File 'lib/coinbase/exchange/websocket.rb', line 79

def done(&block)
  @done_cb = block
end

#error(&block) ⇒ Object



83
84
85
# File 'lib/coinbase/exchange/websocket.rb', line 83

def error(&block)
  @error_cb = block
end

#match(&block) ⇒ Object



71
72
73
# File 'lib/coinbase/exchange/websocket.rb', line 71

def match(&block)
  @match_cb = block
end

#message(&block) ⇒ Object

Run this before processing every message



59
60
61
# File 'lib/coinbase/exchange/websocket.rb', line 59

def message(&block)
  @message_cb = block
end

#open(&block) ⇒ Object



67
68
69
# File 'lib/coinbase/exchange/websocket.rb', line 67

def open(&block)
  @open_cb = block
end

#ping(options = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/coinbase/exchange/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) ⇒ Object



63
64
65
# File 'lib/coinbase/exchange/websocket.rb', line 63

def received(&block)
  @received_cb = block
end

#refresh!Object



38
39
40
41
42
43
44
# File 'lib/coinbase/exchange/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!Object



19
20
21
22
23
24
25
26
27
# File 'lib/coinbase/exchange/websocket.rb', line 19

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

#stop!Object



29
30
31
32
33
34
35
36
# File 'lib/coinbase/exchange/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 = {}) ⇒ Object



46
47
48
49
# File 'lib/coinbase/exchange/websocket.rb', line 46

def subscribe!(options = {})
  product = options[:product_id] || @product
  @socket.send({ type: 'subscribe', product_id: product }.to_json)
end