Class: Coinbasepro::Api::Websocket
- Inherits:
-
Object
- Object
- Coinbasepro::Api::Websocket
- Defined in:
- lib/coinbasepro/api/websocket.rb
Overview
Websocket client for Coinbase Pro
Instance Method Summary collapse
- #change(&block) ⇒ Object
- #done(&block) ⇒ Object
- #error(&block) ⇒ Object
-
#initialize(options = {}) ⇒ Websocket
constructor
A new instance of Websocket.
- #match(&block) ⇒ Object
-
#message(&block) ⇒ Object
Run this before processing every message.
- #open(&block) ⇒ Object
- #ping(options = {}) ⇒ Object
- #received(&block) ⇒ Object
- #refresh! ⇒ Object
- #start! ⇒ Object
- #stop! ⇒ Object
- #subscribe!(options = {}) ⇒ Object
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/coinbasepro/api/websocket.rb', line 5 def initialize( = {}) @ws_url = [:ws_url] || "wss://ws-feed.pro.coinbase.com" @product = [:product_id] || 'BTC-USD' @keepalive = [: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/coinbasepro/api/websocket.rb', line 75 def change(&block) @change_cb = block end |
#done(&block) ⇒ Object
79 80 81 |
# File 'lib/coinbasepro/api/websocket.rb', line 79 def done(&block) @done_cb = block end |
#error(&block) ⇒ Object
83 84 85 |
# File 'lib/coinbasepro/api/websocket.rb', line 83 def error(&block) @error_cb = block end |
#match(&block) ⇒ Object
71 72 73 |
# File 'lib/coinbasepro/api/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/coinbasepro/api/websocket.rb', line 59 def (&block) @message_cb = block end |
#open(&block) ⇒ Object
67 68 69 |
# File 'lib/coinbasepro/api/websocket.rb', line 67 def open(&block) @open_cb = block end |
#ping(options = {}) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/coinbasepro/api/websocket.rb', line 51 def ping( = {}) msg = [: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/coinbasepro/api/websocket.rb', line 63 def received(&block) @received_cb = block end |
#refresh! ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/coinbasepro/api/websocket.rb', line 38 def refresh! @socket = Faye::WebSocket::Client.new(@ws_url) @socket.onopen = method(:ws_opened) @socket. = 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/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! ⇒ Object
29 30 31 32 33 34 35 36 |
# 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 = {}) ⇒ Object
46 47 48 49 |
# File 'lib/coinbasepro/api/websocket.rb', line 46 def subscribe!( = {}) product = [:product_id] || @product @socket.send({ type: 'subscribe', product_id: product }.to_json) end |