Class: Deribit::Websocket
- Inherits:
-
Object
- Object
- Deribit::Websocket
- Defined in:
- lib/deribit/websocket.rb
Overview
Websocket API adapter
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #authorized? ⇒ Boolean
-
#initialize(host, key: nil, secret: nil) ⇒ Deribit::Websocket
constructor
Create new websocket instance.
-
#subscribe(topic, params: {}) {|Array| ... } ⇒ Object
Subscribe to a specific topic and optionally filter by symbol.
Constructor Details
#initialize(host, key: nil, secret: nil) ⇒ Deribit::Websocket
Create new websocket instance
15 16 17 18 19 20 21 |
# File 'lib/deribit/websocket.rb', line 15 def initialize(host, key: nil, secret: nil) @host = host @key = key @secret = secret @callbacks = {} @ws = nil end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
8 9 10 |
# File 'lib/deribit/websocket.rb', line 8 def access_token @access_token end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
8 9 10 |
# File 'lib/deribit/websocket.rb', line 8 def callbacks @callbacks end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
8 9 10 |
# File 'lib/deribit/websocket.rb', line 8 def host @host end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
8 9 10 |
# File 'lib/deribit/websocket.rb', line 8 def key @key end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
8 9 10 |
# File 'lib/deribit/websocket.rb', line 8 def secret @secret end |
Instance Method Details
#authorized? ⇒ Boolean
50 51 52 |
# File 'lib/deribit/websocket.rb', line 50 def !access_token.nil? end |
#subscribe(topic, params: {}) {|Array| ... } ⇒ Object
Subscribe to a specific topic and optionally filter by symbol
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/deribit/websocket.rb', line 27 def subscribe(topic, params: {}, &callback) raise 'block is required' unless block_given? # connect on demand @ws = connect unless connected? raise 'websocket is closed' unless @ws.open? # save callback handler @callbacks[topic.to_s] = callback # authorize if needed if (topic) # subscription request payload = { jsonrpc: '2.0', method: 'public/subscribe', id: rand(9999), params: { channels: [topic] } } @ws.send payload.to_json.to_s end |