class Kucoin::Api::Websocket

Attributes

rest_client[R]

Public Class Methods

new(rest_client: Kucoin::Api::REST.new) click to toggle source
# File lib/kucoin/api/websocket.rb, line 30
def initialize rest_client: Kucoin::Api::REST.new
  @rest_client = rest_client
end
open_tunnel(channel:, params: {}) click to toggle source
# File lib/kucoin/api/websocket.rb, line 19
def open_tunnel channel:, params: {}
  stream  = {id: request_id, response: true}.merge(params)
  params = {
    id: stream[:id], type: 'openTunnel', newTunnelId: stream[:newTunnelId], response: stream[:response]
  }
  channel.send(params.to_json)
end
request_id() click to toggle source
# File lib/kucoin/api/websocket.rb, line 7
def request_id; rand(Time.now.to_i) end
subscribe(channel:, params: {}) click to toggle source
# File lib/kucoin/api/websocket.rb, line 9
def subscribe channel:, params: {}
  stream  = {id: request_id, privateChannel: false, response: true}.merge(params)
  params = {
    id: stream[:id], type: 'subscribe', topic: stream[:topic],
    privateChannel: stream[:privateChannel], response: stream[:response],
    tunnelId: stream[:tunnelId]
  }.select { |k,v| !v.nil? }
  channel.send(params.to_json)
end

Public Instance Methods

all_ticker(methods: ticker symbols: :all, methods: methods) click to toggle source
# File lib/kucoin/api/websocket.rb, line 76
def all_ticker methods:
  ticker symbols: :all, methods: methods
end
balance(methods: start stream: { topic: '/account/balance', privateChannel: true }) click to toggle source
# File lib/kucoin/api/websocket.rb, line 104
def balance methods:
  start stream: { topic: '/account/balance', privateChannel: true }, methods: methods
end
connect(private: false, params: {}) click to toggle source
# File lib/kucoin/api/websocket.rb, line 34
def connect private: false, params: {}, methods:
  create_stream(private ? auth_client(params: params) : open_client(params: params), methods: methods)
end
full_match_engine_data(symbols:, methods:, private_channel: false) click to toggle source
# File lib/kucoin/api/websocket.rb, line 94
def full_match_engine_data symbols:, methods:, private_channel: false
  start stream: { topic: topic_path('/market/level3', symbols), privateChannel: private_channel }, methods: methods
end
level_2_market_data(symbols:, methods: start stream: { topic: topic_path('/market/level2', symbols) }) click to toggle source
# File lib/kucoin/api/websocket.rb, line 86
def level_2_market_data symbols:, methods:
  start stream: { topic: topic_path('/market/level2', symbols) }, methods: methods
end
market_snapshot(symbol:, methods: start stream: { topic: "/market/snapshot:
Alias for: snapshot
match_execution_data(symbols:, methods:, private_channel: false) click to toggle source
# File lib/kucoin/api/websocket.rb, line 90
def match_execution_data symbols:, methods:, private_channel: false
  start stream: { topic: topic_path('/market/match', symbols), privateChannel: private_channel }, methods: methods
end
multiplex(stream:, methods: channel = connect(private: !!stream[:privateChannel], methods: methods)) click to toggle source

Public: Create a WebSocket stream for multiplex tunnels

:stream - The Hash used to define the stream

:id             - Unique string to mark the request
:newTunnelId    - Required
:privateChannel - The user will only receive messages related himself on the topic(default is false)
# File lib/kucoin/api/websocket.rb, line 65
def multiplex stream:, methods:
  channel = connect(private: !!stream[:privateChannel], methods: methods)
  self.class.open_tunnel channel: channel, params: stream
  channel
end
snapshot(symbol:, methods: start stream: { topic: "/market/snapshot: click to toggle source
# File lib/kucoin/api/websocket.rb, line 80
def snapshot symbol:, methods:
  start stream: { topic: "/market/snapshot:#{symbol}" }, methods: methods
end
Also aliased as: symbol_snapshot, market_snapshot
start(stream:, methods: channel = connect(private: !!stream[:privateChannel], methods: methods)) click to toggle source

Public: Create a WebSocket stream

:stream - The Hash used to define the stream

:id             - Unique string to mark the request
:topic          - The topic you want to subscribe to
:privateChannel - The user will only receive messages related himself on the topic(default is false)
:response       - To return the ack messages after the subscriptions succeed(default is true)

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)
# File lib/kucoin/api/websocket.rb, line 52
def start stream:, methods:
  channel = connect(private: !!stream[:privateChannel], methods: methods)
  self.class.subscribe channel: channel, params: stream
  channel
end
stop_order_activate_event(symbols: , methods: full_match_engine_data symbols: symbols, methods: methods, private_channel: true)
stop_order_received_event(symbols: , methods: full_match_engine_data symbols: symbols, methods: methods, private_channel: true) click to toggle source

PRIVATE

# File lib/kucoin/api/websocket.rb, line 99
def stop_order_received_event symbols: , methods:
  full_match_engine_data symbols: symbols, methods: methods, private_channel: true
end
Also aliased as: stop_order_activate_event
symbol_snapshot(symbol:, methods: start stream: { topic: "/market/snapshot:
Alias for: snapshot
ticker(symbols:, methods: start stream: { topic: topic_path('/market/ticker', symbols) }) click to toggle source

PUBLIC

# File lib/kucoin/api/websocket.rb, line 72
def ticker symbols:, methods:
  start stream: { topic: topic_path('/market/ticker', symbols) }, methods: methods
end

Private Instance Methods

attach_methods(websocket, methods) click to toggle source

Internal: Iterate through methods passed and add them to the WebSocket

websocket - The Faye::WebSocket::Client to apply methods to

methods - The Hash which contains the event handler methods to pass to

the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)
# File lib/kucoin/api/websocket.rb, line 138
def attach_methods websocket, methods
  methods.each_pair do |key, method|
    websocket.on(key) { |event| method.call(event) }
  end
end
auth_client(params: {}) click to toggle source
# File lib/kucoin/api/websocket.rb, line 114
def auth_client params: {}
  get_client(rest_client.websocket.private, params: params)
end
create_stream(websocket, methods: attach_methods websocket, methods) click to toggle source
# File lib/kucoin/api/websocket.rb, line 123
def create_stream websocket, methods:
  attach_methods websocket, methods
  websocket
end
get_client(response, params: {}) click to toggle source
# File lib/kucoin/api/websocket.rb, line 118
def get_client response, params: {}
  url = ["#{response.endpoint}?token=#{response.token}", URI.encode_www_form(params)].join('&')
  Faye::WebSocket::Client.new(url, [], ping: response.ping_interval)
end
open_client(params: {}) click to toggle source
# File lib/kucoin/api/websocket.rb, line 110
def open_client params: {}
  get_client(rest_client.websocket.public, params: params)
end
topic_path(path, values=[]) click to toggle source
# File lib/kucoin/api/websocket.rb, line 144
def topic_path path, values=[]
  values = [values] unless values.is_a?(Array)
  "#{path}:#{values.compact.join(',')}"
end