Class: Cryptomarket::Websocket::ClientBase
- Inherits:
-
Object
- Object
- Cryptomarket::Websocket::ClientBase
show all
- Defined in:
- lib/cryptomarket/websocket/client_base.rb
Overview
websockt client able to handle requests and subscriptions
Instance Method Summary
collapse
-
#close ⇒ Object
-
#connect ⇒ Object
connects via websocket to the exchange, it blocks until the connection is stablished.
-
#connected? ⇒ Boolean
-
#get_callback_for_response(response) ⇒ Object
-
#handle(message) ⇒ Object
-
#handle_good_response(response, callback) ⇒ Object
-
#handle_notification(notification) ⇒ Object
-
#handle_response(response) ⇒ Object
-
#initialize(url:, subscription_keys:, on_connect: -> {}, on_error: ->(error) {}, on_close: -> {}) ⇒ ClientBase
constructor
A new instance of ClientBase.
-
#on_close ⇒ Object
-
#on_close=(callback = nil, &block) ⇒ Object
-
#on_connect ⇒ Object
-
#on_connect=(callback = nil, &block) ⇒ Object
-
#on_error(error) ⇒ Object
-
#on_error=(callback = nil, &block) ⇒ Object
-
#on_open ⇒ Object
-
#request(method, callback, params = {}, call_count = 1) ⇒ Object
-
#send_subscription(method, callback, params, result_callback) ⇒ Object
-
#send_unsubscription(method, callback, params) ⇒ Object
-
#store_callback_and_send(method, params, callback_to_store = nil, call_count = 1) ⇒ Object
Constructor Details
#initialize(url:, subscription_keys:, on_connect: -> {}, on_error: ->(error) {}, on_close: -> {}) ⇒ ClientBase
Returns a new instance of ClientBase.
11
12
13
14
15
16
17
18
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 11
def initialize(url:, subscription_keys:, on_connect: -> {}, on_error: ->(error) {}, on_close: -> {})
@subscription_keys = subscription_keys
@callback_cache = CallbackCache.new
@ws_manager = WSManager.new self, url: url
@on_connect = on_connect
@on_error = on_error
@on_close = on_close
end
|
Instance Method Details
#close ⇒ Object
61
62
63
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 61
def close
@ws_manager.close
end
|
#connect ⇒ Object
connects via websocket to the exchange, it blocks until the connection is stablished
25
26
27
28
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 25
def connect
@ws_manager.connect
sleep(1) until @ws_manager.connected?
end
|
#connected? ⇒ Boolean
20
21
22
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 20
def connected?
@ws_manager.connected?
end
|
#get_callback_for_response(response) ⇒ Object
107
108
109
110
111
112
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 107
def get_callback_for_response(response)
id = response['id']
return if id.nil?
@callback_cache.get_callback(id)
end
|
#handle(message) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 89
def handle(message)
if message.key? 'id'
handle_response(message)
elsif message.key? 'method'
handle_notification(message)
end
end
|
#handle_good_response(response, callback) ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 125
def handle_good_response(response, callback)
result = response['result']
if result.is_a?(Hash) && result.key?('data')
callback.call(nil, result['data'])
else
callback.call(nil, result)
end
end
|
#handle_notification(notification) ⇒ Object
97
98
99
100
101
102
103
104
105
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 97
def handle_notification(notification)
method = notification['method']
method_data = @subscription_keys[method]
notification_type = method_data[1]
callback = @callback_cache.get_subscription_callback(method_data[0])
return if callback.nil?
callback.call(notification['params'], notification_type)
end
|
#handle_response(response) ⇒ Object
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 114
def handle_response(response)
callback = get_callback_for_response(response)
return if callback.nil?
if response.key? 'error'
callback.call(Cryptomarket::APIException.new(response['error']), nil)
nil
end
handle_good_response(response, callback)
end
|
#on_close ⇒ Object
57
58
59
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 57
def on_close
@on_close.call
end
|
#on_close=(callback = nil, &block) ⇒ Object
52
53
54
55
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 52
def on_close=(callback = nil, &block)
callback ||= block
@on_close = callback
end
|
#on_connect ⇒ Object
39
40
41
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 39
def on_connect
@on_connect.call
end
|
#on_connect=(callback = nil, &block) ⇒ Object
34
35
36
37
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 34
def on_connect=(callback = nil, &block)
callback ||= block
@on_connect = callback
end
|
#on_error(error) ⇒ Object
48
49
50
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 48
def on_error(error)
@on_error.call(error)
end
|
#on_error=(callback = nil, &block) ⇒ Object
43
44
45
46
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 43
def on_error=(callback = nil, &block)
callback ||= block
@on_error = callback
end
|
#on_open ⇒ Object
30
31
32
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 30
def on_open
@on_connect.call
end
|
#request(method, callback, params = {}, call_count = 1) ⇒ Object
75
76
77
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 75
def request(method, callback, params = {}, call_count = 1)
store_callback_and_send(method, params, callback, call_count)
end
|
#send_subscription(method, callback, params, result_callback) ⇒ Object
65
66
67
68
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 65
def send_subscription(method, callback, params, result_callback)
@callback_cache.store_subscription_callback(@subscription_keys[method][0], callback)
store_callback_and_send(method, params, result_callback)
end
|
#send_unsubscription(method, callback, params) ⇒ Object
70
71
72
73
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 70
def send_unsubscription(method, callback, params)
@callback_cache.delete_subscription_callback(@subscription_keys[method][0])
store_callback_and_send(method, params, callback)
end
|
#store_callback_and_send(method, params, callback_to_store = nil, call_count = 1) ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'lib/cryptomarket/websocket/client_base.rb', line 79
def store_callback_and_send(method, params, callback_to_store = nil, call_count = 1)
params = params.compact unless params.nil?
payload = { 'method' => method, 'params' => params }
unless callback_to_store.nil?
id = @callback_cache.store_callback(callback_to_store, call_count)
payload['id'] = id
end
@ws_manager.send(payload)
end
|