Class: Deribit::WS

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#handlersObject (readonly)

Returns the value of attribute handlers.



5
6
7
# File 'lib/deribit/ws.rb', line 5

def handlers
  @handlers
end

#ids_stackObject (readonly)

Returns the value of attribute ids_stack.



5
6
7
# File 'lib/deribit/ws.rb', line 5

def ids_stack
  @ids_stack
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/deribit/ws.rb', line 5

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



5
6
7
# File 'lib/deribit/ws.rb', line 5

def secret
  @secret
end

#socketObject (readonly)

Returns the value of attribute socket.



5
6
7
# File 'lib/deribit/ws.rb', line 5

def socket
  @socket
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



5
6
7
# File 'lib/deribit/ws.rb', line 5

def subscriptions
  @subscriptions
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/deribit/ws.rb', line 5

def url
  @url
end

Instance Method Details

#authObject



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
  timestamp = (Time.now.utc.to_f * 1000).to_i
  nonce = rand(100000000)

  params = {
    grant_type: "client_signature",
    client_id: key,
    timestamp: timestamp.to_s,
    signature: generate_signature(timestamp, 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

#closeObject



34
35
36
# File 'lib/deribit/ws.rb', line 34

def close
  socket.close
end

#connectObject



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

#handlerObject



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