Class: Deribit::WS
- Inherits:
-
Object
- Object
- Deribit::WS
- Defined in:
- lib/deribit/ws.rb,
lib/deribit/ws/handler.rb,
lib/deribit/ws/base_handler.rb
Defined Under Namespace
Classes: BaseHandler, Handler
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
#ids_stack ⇒ Object
readonly
Returns the value of attribute ids_stack.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #auth ⇒ Object
- #close ⇒ Object
- #connect ⇒ Object
- #handler ⇒ Object
-
#initialize(key = nil, secret = nil, handlers: [Handler.new], test_server: false) ⇒ WS
constructor
A new instance of WS.
-
#method_missing(name, **params, &block) ⇒ Object
methods like ws.get_account_history.
- #process_data(json) ⇒ Object
Constructor Details
#initialize(key = nil, secret = nil, handlers: [Handler.new], test_server: false) ⇒ WS
Returns a new instance of WS.
7 8 9 10 11 12 13 14 |
# File 'lib/deribit/ws.rb', line 7 def initialize(key = nil, secret = nil, handlers: [Handler.new], test_server: false) @key = key @secret = secret @ids_stack = {} @url = set_server_url(test_server) @handlers = handlers end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, **params, &block) ⇒ Object
methods like ws.get_account_history
28 29 30 31 32 |
# File 'lib/deribit/ws.rb', line 28 def method_missing(name, **params, &block) method = Deribit.find_method(name, params) # puts "Found method: #{method}, params = #{params}" send(path: method[:path], params: params) end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
5 6 7 |
# File 'lib/deribit/ws.rb', line 5 def handlers @handlers end |
#ids_stack ⇒ Object (readonly)
Returns the value of attribute ids_stack.
5 6 7 |
# File 'lib/deribit/ws.rb', line 5 def ids_stack @ids_stack end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/deribit/ws.rb', line 5 def key @key end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
5 6 7 |
# File 'lib/deribit/ws.rb', line 5 def secret @secret end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
5 6 7 |
# File 'lib/deribit/ws.rb', line 5 def socket @socket end |
#subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
5 6 7 |
# File 'lib/deribit/ws.rb', line 5 def subscriptions @subscriptions end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
5 6 7 |
# File 'lib/deribit/ws.rb', line 5 def url @url end |
Instance Method Details
#auth ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/deribit/ws.rb', line 38 def auth = (Time.now.utc.to_f * 1000).to_i nonce = rand(100000000) params = { grant_type: "client_signature", client_id: key, timestamp: .to_s, signature: generate_signature(, nonce), data: "", nonce: nonce.to_s, } send(path: "public/auth", params: params) # todo: refactor 30.times do |i| handler.access_token == nil ? sleep(0.1) : return end end |
#close ⇒ Object
34 35 36 |
# File 'lib/deribit/ws.rb', line 34 def close socket.close end |
#connect ⇒ Object
16 17 18 19 20 21 |
# File 'lib/deribit/ws.rb', line 16 def connect start_handle sleep 0.1 auth if key && secret handlers.each { |h| subscribe(channels: h.subscriptions.to_a) if h.subscriptions.any? } end |
#handler ⇒ Object
23 24 25 |
# File 'lib/deribit/ws.rb', line 23 def handler handlers.find { |handler| handler.is_a?(Handler) } end |
#process_data(json) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/deribit/ws.rb', line 59 def process_data(json) stack_id = ids_stack[json[:id]] method = json.fetch(:method) { stack_id&.fetch(:method, nil) } handlers.each { |hander| hander.process(json, method: method, ws: self) } ids_stack.delete(stack_id) if stack_id end |