Class: Cryptomarket::Websocket::MarketDataClientCore

Inherits:
ClientBase
  • Object
show all
Defined in:
lib/cryptomarket/websocket/market_data_client_core.rb

Overview

websocket client able to handle market data subscriptions

Direct Known Subclasses

MarketDataClient

Instance Method Summary collapse

Methods inherited from ClientBase

#close, #connect, #connected?, #get_callback_for_response, #handle_good_response, #handle_notification, #handle_response, #on_close, #on_close=, #on_connect, #on_connect=, #on_error, #on_error=, #on_open, #request, #send_subscription, #send_unsubscription, #store_callback_and_send

Constructor Details

#initializeMarketDataClientCore

Returns a new instance of MarketDataClientCore.



12
13
14
15
16
# File 'lib/cryptomarket/websocket/market_data_client_core.rb', line 12

def initialize
  super(
    url: 'wss://api.exchange.cryptomkt.com/api/3/ws/public',
    subscription_keys: {})
end

Instance Method Details

#handle(message) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cryptomarket/websocket/market_data_client_core.rb', line 29

def handle(message)
  if message.key? 'ch'
    handle_ch_notification(message)
  elsif message.key? 'id'
    handle_response(message)
  end
end

#handle_ch_notification(notification) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cryptomarket/websocket/market_data_client_core.rb', line 37

def handle_ch_notification(notification)
  key = notification['ch']
  callback = @callback_cache.get_subscription_callback(key)
  return if callback.nil?

  callback.call(notification['data'], Args::NotificationType::DATA) if notification.key? 'data'
  callback.call(notification['snapshot'], Args::NotificationType::SNAPSHOT) if notification.key? 'snapshot'
  return unless notification.key? 'update'

  callback.call(notification['update'], Args::NotificationType::UPDATE)
end

#intercept_result_callback(result_callback) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cryptomarket/websocket/market_data_client_core.rb', line 49

def intercept_result_callback(result_callback)
  return result_callback if result_callback.nil?

  proc { |err, result|
    if result.nil?
      result_callback.call(err, result)
    else
      result_callback.call(err, result['subscriptions'])
    end
  }
end

#send_channel_subscription(channel, callback, result_callback, params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/cryptomarket/websocket/market_data_client_core.rb', line 18

def send_channel_subscription(channel, callback, result_callback, params = {})
  params = params.compact unless params.nil?
  payload = { 'method' => 'subscribe', 'ch' => channel, 'params' => params }
  @callback_cache.store_subscription_callback(channel, callback)
  unless result_callback.nil?
    id = @callback_cache.store_callback(result_callback)
    payload['id'] = id
  end
  @ws_manager.send(payload)
end