Class: Coincap::LiveAssets
- Inherits:
-
Object
- Object
- Coincap::LiveAssets
- Defined in:
- lib/coincap/live_assets.rb
Constant Summary collapse
- BASE_URI =
'wss://ws.coincap.io/prices?assets='
Instance Method Summary collapse
-
#connect ⇒ Object
Connect to the websocket.
-
#disconnect ⇒ Object
Close the websocket.
-
#initialize(assets = 'ALL') ⇒ LiveAssets
constructor
Initialize the websocket.
-
#on_close(&on_close) ⇒ Object
When close the websocket.
-
#on_error(&on_error) ⇒ Object
When error.
-
#on_open(&on_open) ⇒ Object
When open the websocket.
-
#on_price(&on_message) ⇒ Object
When receive the price.
Constructor Details
#initialize(assets = 'ALL') ⇒ LiveAssets
Initialize the websocket
9 10 11 |
# File 'lib/coincap/live_assets.rb', line 9 def initialize(assets = 'ALL') @assets = assets end |
Instance Method Details
#connect ⇒ Object
Connect to the websocket
14 15 16 17 |
# File 'lib/coincap/live_assets.rb', line 14 def connect uri = "#{BASE_URI}#{@assets == 'ALL' ? @assets : @assets.join(',')}" @ws = WebSocket::Client::Simple.connect(uri) end |
#disconnect ⇒ Object
Close the websocket
20 21 22 |
# File 'lib/coincap/live_assets.rb', line 20 def disconnect @ws.close end |
#on_close(&on_close) ⇒ Object
When close the websocket
37 38 39 |
# File 'lib/coincap/live_assets.rb', line 37 def on_close(&on_close) @ws.on :close, &on_close end |
#on_error(&on_error) ⇒ Object
When error
42 43 44 |
# File 'lib/coincap/live_assets.rb', line 42 def on_error(&on_error) @ws.on :error, &on_error end |
#on_open(&on_open) ⇒ Object
When open the websocket
32 33 34 |
# File 'lib/coincap/live_assets.rb', line 32 def on_open(&on_open) @ws.on :open, &on_open end |
#on_price(&on_message) ⇒ Object
When receive the price
25 26 27 28 29 |
# File 'lib/coincap/live_assets.rb', line 25 def on_price(&) @ws.on :message do |event| .call(event.data) end end |