Skip to content

Commit c186ff0

Browse files
committed
test:automates websocket market data tests
1 parent 1b5f9d1 commit c186ff0

20 files changed

+380
-311
lines changed

Gemfile

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
source 'https://rubygems.org'
44

5-
gem 'eventmachine'
6-
gem 'faye-websocket'
7-
gem 'openssl'
8-
gem 'rest-client'
9-
5+
gem 'eventmachine', '1.2.7'
6+
gem 'faye-websocket', '0.11.3'
7+
gem 'openssl', '3.2.0'
8+
gem 'rest-client', '2.1.0'
109

1110
group :test do
12-
gem 'rubocop'
13-
gem 'test-unit'
14-
end
11+
gem 'rubocop', '1.60.2'
12+
gem 'test-unit', '3.6.2'
13+
end

lib/cryptomarket/client.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def get_ticker_prices(symbols: nil)
185185
# ==== Params
186186
# +String+ +symbol+:: A symbol id
187187

188-
def get_ticker_price_of_symbol(symbol:)
188+
def get_ticker_price(symbol:)
189189
public_get("public/price/ticker/#{symbol}")
190190
end
191191

@@ -227,7 +227,7 @@ def get_trades(symbols: nil, by: nil, sort: nil, from: nil, till: nil, limit: ni
227227
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000
228228
# +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100000
229229

230-
def get_trades_of_symbol(symbol: nil, by: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
230+
def get_trades_by_symbol(symbol: nil, by: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
231231
public_get(
232232
"public/trades/#{symbol}",
233233
{ by: by, sort: sort, from: from, till: till, limit: limit, offset: offset }
@@ -260,7 +260,7 @@ def get_orderbooks(symbols: nil, depth: nil)
260260
# +String+ +symbol+:: A symbol id
261261
# +Integer+ +depth+:: Optional. Order Book depth. Default value is 100. Set to 0 to view the full Order Book
262262

263-
def get_orderbook_of_symbol(symbol:, depth: nil)
263+
def get_orderbook(symbol:, depth: nil)
264264
public_get("public/orderbook/#{symbol}", { depth: depth })
265265
end
266266

@@ -275,7 +275,7 @@ def get_orderbook_of_symbol(symbol:, depth: nil)
275275
# +String+ +symbol+:: A symbol id
276276
# +float+ +volume+:: Optional. Desired volume for market depth search
277277

278-
def get_orderbook_volume_of_symbol(symbol:, volume: nil)
278+
def get_orderbook_volume(symbol:, volume: nil)
279279
public_get("public/orderbook/#{symbol}", { volume: volume })
280280
end
281281

@@ -319,7 +319,7 @@ def get_candles(symbols: nil, period: nil, sort: nil, from: nil, till: nil, limi
319319
# +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1000
320320
# +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100000
321321

322-
def get_candles_of_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
322+
def get_candles_by_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists
323323
public_get(
324324
"public/candles/#{symbol}",
325325
{ period: period, sort: sort, from: from, till: till, limit: limit, offset: offset }
@@ -336,7 +336,7 @@ def get_candles_of_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil,
336336
#
337337
# https://api.exchange.cryptomkt.com/#get-spot-trading-balance
338338

339-
def get_spot_trading_balance # rubocop:disable Naming/AccessorMethodName
339+
def get_spot_trading_balances # rubocop:disable Naming/AccessorMethodName
340340
get('spot/balance')
341341
end
342342

@@ -349,7 +349,7 @@ def get_spot_trading_balance # rubocop:disable Naming/AccessorMethodName
349349
# ==== Params
350350
# +String+ +currency+:: The currency code to query the balance
351351

352-
def get_spot_trading_balance_of_currency(currency:)
352+
def get_spot_trading_balance(currency:)
353353
balance = get("spot/balance/#{currency}")
354354
balance['currency'] = currency
355355
balance

lib/cryptomarket/websocket/callback_cache.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Websocket
99
class CallbackCache
1010
def initialize
1111
@reusable_callbacks = {}
12+
@subscription_callbacks = {}
1213
@next_id = 1
1314
end
1415

@@ -34,19 +35,19 @@ def get_callback(id)
3435
end
3536

3637
def store_subscription_callback(key, callback)
37-
@reusable_callbacks[key] = callback
38+
@subscription_callbacks[key] = callback
3839
end
3940

4041
def get_subscription_callback(key)
41-
return nil unless @reusable_callbacks.key? key
42+
return nil unless @subscription_callbacks.key? key
4243

43-
@reusable_callbacks[key]
44+
@subscription_callbacks[key]
4445
end
4546

4647
def delete_subscription_callback(key)
47-
return unless @reusable_callbacks.key? key
48+
return unless @subscription_callbacks.key? key
4849

49-
@reusable_callbacks.delete(key)
50+
@subscription_callbacks.delete(key)
5051
end
5152
end
5253
end

lib/cryptomarket/websocket/market_data_client.rb

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MarketDataClient < MarketDataClientCore
3131

3232
def subscribe_to_trades(callback:, symbols:, limit: nil, result_callback: nil)
3333
params = { 'symbols' => symbols, 'limit' => limit }
34-
send_channel_subscription('trades', params, callback, intercept_result_callback(result_callback))
34+
send_channel_subscription('trades', callback, intercept_result_callback(result_callback), params)
3535
end
3636

3737
# subscribe to a feed of candles
@@ -56,8 +56,8 @@ def subscribe_to_trades(callback:, symbols:, limit: nil, result_callback: nil)
5656

5757
def subscribe_to_candles(callback:, period:, symbols:, limit: nil, result_callback: nil)
5858
params = { 'symbols' => symbols, 'limit' => limit }
59-
send_channel_subscription("candles/#{period}", params, callback,
60-
intercept_result_callback(result_callback))
59+
send_channel_subscription("candles/#{period}", callback,
60+
intercept_result_callback(result_callback), params)
6161
end
6262

6363
# subscribe to a feed of mini tickers
@@ -78,8 +78,8 @@ def subscribe_to_candles(callback:, period:, symbols:, limit: nil, result_callba
7878

7979
def subscribe_to_mini_ticker(callback:, speed:, symbols: ['*'], result_callback: nil)
8080
params = { 'symbols' => symbols }
81-
send_channel_subscription("ticker/price/#{speed}", params, callback,
82-
intercept_result_callback(result_callback))
81+
send_channel_subscription("ticker/price/#{speed}", callback,
82+
intercept_result_callback(result_callback), params)
8383
end
8484

8585
# subscribe to a feed of mini tickers
@@ -100,8 +100,8 @@ def subscribe_to_mini_ticker(callback:, speed:, symbols: ['*'], result_callback:
100100

101101
def subscribe_to_mini_ticker_in_batches(callback:, speed:, symbols: ['*'], result_callback: nil)
102102
params = { 'symbols' => symbols }
103-
send_channel_subscription("ticker/price/#{speed}/batch", params, callback,
104-
intercept_result_callback(result_callback))
103+
send_channel_subscription("ticker/price/#{speed}/batch", callback,
104+
intercept_result_callback(result_callback), params)
105105
end
106106

107107
# subscribe to a feed of tickers
@@ -122,7 +122,7 @@ def subscribe_to_mini_ticker_in_batches(callback:, speed:, symbols: ['*'], resul
122122

123123
def subscribe_to_ticker(callback:, speed:, symbols: ['*'], result_callback: nil)
124124
params = { 'symbols' => symbols }
125-
send_channel_subscription("ticker/#{speed}", params, callback, intercept_result_callback(result_callback))
125+
send_channel_subscription("ticker/#{speed}", callback, intercept_result_callback(result_callback), params)
126126
end
127127

128128
# subscribe to a feed of tickers
@@ -143,8 +143,7 @@ def subscribe_to_ticker(callback:, speed:, symbols: ['*'], result_callback: nil)
143143

144144
def subscribe_to_ticker_in_batches(callback:, speed:, symbols: ['*'], result_callback: nil)
145145
params = { 'symbols' => symbols }
146-
send_channel_subscription("ticker/#{speed}/batch", params, callback,
147-
intercept_result_callback(result_callback))
146+
send_channel_subscription("ticker/#{speed}/batch", callback, intercept_result_callback(result_callback), params)
148147
end
149148

150149
# subscribe to a feed of a full orderbook
@@ -167,7 +166,7 @@ def subscribe_to_ticker_in_batches(callback:, speed:, symbols: ['*'], result_cal
167166

168167
def subscribe_to_full_order_book(callback:, symbols:, result_callback: nil)
169168
params = { 'symbols' => symbols }
170-
send_channel_subscription('orderbook/full', params, callback, intercept_result_callback(result_callback))
169+
send_channel_subscription('orderbook/full', callback, intercept_result_callback(result_callback), params)
171170
end
172171

173172
# subscribe to a feed of a partial orderbook
@@ -189,8 +188,8 @@ def subscribe_to_full_order_book(callback:, symbols:, result_callback: nil)
189188

190189
def subscribe_to_partial_order_book(callback:, depth:, speed:, symbols: ['*'], result_callback: nil)
191190
params = { 'symbols' => symbols }
192-
send_channel_subscription("orderbook/#{depth}/#{speed}", params, callback,
193-
intercept_result_callback(result_callback))
191+
send_channel_subscription("orderbook/#{depth}/#{speed}", callback,
192+
intercept_result_callback(result_callback), params)
194193
end
195194

196195
# subscribe to a feed of a partial orderbook in batches
@@ -211,8 +210,8 @@ def subscribe_to_partial_order_book(callback:, depth:, speed:, symbols: ['*'], r
211210
def subscribe_to_partial_order_book_in_batches(callback:, depth:, speed:, symbols: ['*'],
212211
result_callback: nil)
213212
params = { 'symbols' => symbols }
214-
send_channel_subscription("orderbook/#{depth}/#{speed}/batch", params, callback,
215-
intercept_result_callback(result_callback))
213+
send_channel_subscription("orderbook/#{depth}/#{speed}/batch", callback,
214+
intercept_result_callback(result_callback), params)
216215
end
217216

218217
# subscribe to a feed of the top of the orderbook
@@ -231,8 +230,8 @@ def subscribe_to_partial_order_book_in_batches(callback:, depth:, speed:, symbol
231230

232231
def subscribe_to_top_of_book(callback:, speed:, symbols: ['*'], result_callback: nil)
233232
params = { 'symbols' => symbols }
234-
send_channel_subscription("orderbook/top/#{speed}", params, callback,
235-
intercept_result_callback(result_callback))
233+
send_channel_subscription("orderbook/top/#{speed}", callback,
234+
intercept_result_callback(result_callback), params)
236235
end
237236

238237
# subscribe to a feed of the top of the orderbook
@@ -251,8 +250,8 @@ def subscribe_to_top_of_book(callback:, speed:, symbols: ['*'], result_callback:
251250

252251
def subscribe_to_top_of_book_in_batches(callback:, speed:, symbols: ['*'], result_callback: nil)
253252
params = { 'symbols' => symbols }
254-
send_channel_subscription("orderbook/top/#{speed}/batch", params, callback,
255-
intercept_result_callback(result_callback))
253+
send_channel_subscription("orderbook/top/#{speed}/batch", callback,
254+
intercept_result_callback(result_callback), params)
256255
end
257256

258257
# subscribe to a feed of the top of the orderbook
@@ -272,8 +271,7 @@ def subscribe_to_price_rates(callback:, speed:, target_currency:, currencies: ['
272271
params = {
273272
speed: speed, target_currency: target_currency, currencies: currencies
274273
}
275-
send_channel_subscription("price/rate/#{speed}", params, callback,
276-
intercept_result_callback(result_callback))
274+
send_channel_subscription("price/rate/#{speed}", callback, intercept_result_callback(result_callback), params)
277275
end
278276

279277
# subscribe to a feed of the top of the orderbook
@@ -296,8 +294,8 @@ def subscribe_to_price_rates_in_batches(callback:, speed:, target_currency:, cur
296294
params = {
297295
speed: speed, target_currency: target_currency, currencies: currencies
298296
}
299-
send_channel_subscription("price/rate/#{speed}/batch", params, callback,
300-
intercept_result_callback(result_callback))
297+
send_channel_subscription("price/rate/#{speed}/batch", callback,
298+
intercept_result_callback(result_callback), params)
301299
end
302300
end
303301
end

lib/cryptomarket/websocket/market_data_client_core.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def initialize
1818
def send_channel_subscription(channel, callback, result_callback, params = {})
1919
params = params.compact unless params.nil?
2020
payload = { 'method' => 'subscribe', 'ch' => channel, 'params' => params }
21-
22-
key = channel
23-
@callback_cache.store_subscription_callback(key, callback)
21+
@callback_cache.store_subscription_callback(channel, callback)
2422
unless result_callback.nil?
2523
id = @callback_cache.store_callback(result_callback)
2624
payload['id'] = id

lib/cryptomarket/websocket/trading_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def get_spot_commissions(callback:)
275275
# +String+ +symbol+:: The symbol of the commission rate
276276
# +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a commission for a symbol for the user
277277

278-
def get_spot_commission_of_symbol(symbol:, callback:)
278+
def get_spot_commission(symbol:, callback:)
279279
request('spot_fee', callback, { symbol: symbol })
280280
end
281281
end

lib/cryptomarket/websocket/ws_manager.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,29 @@ def connect
2424
@thread = Thread.new do
2525
EM.run do
2626
@ws = Faye::WebSocket::Client.new(@url)
27-
@ws.onopen = @on_open
28-
@ws.onclose = @on_close
29-
@ws.onerror = @on_error
30-
@ws.onmessage = @on_message
27+
@ws.onopen = method(:_on_open)
28+
@ws.onclose = method(:_on_close)
29+
@ws.onerror = method(:_on_error)
30+
@ws.onmessage = method(:_on_message)
3131
end
3232
end
3333
end
3434

35-
@on_open = lambda do |_event|
35+
def _on_open(_open_event)
3636
@connected = true
3737
@handler.on_open
3838
end
3939

40-
@on_close = lambda do |_close|
40+
def _on_close(_close_event)
4141
@handler.on_close
4242
EM.stop
4343
end
4444

45-
@on_error = lambda do |error|
45+
def _on_error(error)
4646
@handler.on_error(error)
4747
end
4848

49-
@on_message = lambda do |message|
49+
def _on_message(message)
5050
@handler.handle(JSON.parse(message.data.to_s))
5151
end
5252

0 commit comments

Comments
 (0)