Class: Bitmex::Websocket
- Inherits:
-
Object
- Object
- Bitmex::Websocket
- Defined in:
- lib/bitmex/websocket.rb
Overview
Websocket API support www.bitmex.com/app/wsAPI
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Instance Method Summary collapse
-
#initialize(host, api_key: nil, api_secret: nil) ⇒ Bitmex::Websocket
constructor
Create new websocket instance.
-
#listen(topics) {|data, result| ... } ⇒ Object
Listen to generic topics.
-
#stop ⇒ Object
Stop websocket listener.
-
#subscribe(topic, symbol = nil, auth: false) {|Array| ... } ⇒ Object
Subscribe to a specific topic and optionally filter by symbol.
-
#unsubscribe(topic, symbol = nil) ⇒ Object
Unsubscribe from a specific topic and symbol.
Constructor Details
#initialize(host, api_key: nil, api_secret: nil) ⇒ Bitmex::Websocket
Create new websocket instance
15 16 17 18 19 20 |
# File 'lib/bitmex/websocket.rb', line 15 def initialize(host, api_key: nil, api_secret: nil) @host = host @api_key = api_key @api_secret = api_secret end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/bitmex/websocket.rb', line 8 def api_key @api_key end |
#api_secret ⇒ Object (readonly)
Returns the value of attribute api_secret.
8 9 10 |
# File 'lib/bitmex/websocket.rb', line 8 def api_secret @api_secret end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
8 9 10 |
# File 'lib/bitmex/websocket.rb', line 8 def host @host end |
Instance Method Details
#listen(topics) {|data, result| ... } ⇒ Object
Listen to generic topics
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bitmex/websocket.rb', line 49 def listen(topics, &ablock) EM.run do connect topics.each do |topic, symbol| symbols = [symbol] if symbol.nil? || symbol.is_a?(String) symbols ||= symbol symbols.each do |symbol| subscribe topic, symbol, &ablock end end end end |
#stop ⇒ Object
Stop websocket listener
64 65 66 |
# File 'lib/bitmex/websocket.rb', line 64 def stop EM.stop_event_loop end |
#subscribe(topic, symbol = nil, auth: false) {|Array| ... } ⇒ Object
Subscribe to a specific topic and optionally filter by symbol
26 27 28 29 30 31 32 33 |
# File 'lib/bitmex/websocket.rb', line 26 def subscribe(topic, symbol = nil, auth: false, &callback) raise 'callback block is required' unless block_given? @callbacks[topic.to_s] = callback payload = { op: :subscribe, args: [subscription(topic, symbol)] } @faye.send payload.to_json.to_s end |
#unsubscribe(topic, symbol = nil) ⇒ Object
Unsubscribe from a specific topic and symbol
38 39 40 41 42 43 |
# File 'lib/bitmex/websocket.rb', line 38 def unsubscribe(topic, symbol = nil) @callbacks[topic.to_s] = nil payload = { op: :unsubscribe, args: [subscription(topic, symbol)] } @faye.send payload.to_json.to_s end |