Class: Kucoin::Api::Websocket

Inherits:
Object
  • Object
show all
Defined in:
lib/kucoin/api/websocket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rest_client: Kucoin::Api::REST.new) ⇒ Websocket

Returns a new instance of Websocket.



30
31
32
# File 'lib/kucoin/api/websocket.rb', line 30

def initialize rest_client: Kucoin::Api::REST.new
  @rest_client = rest_client
end

Instance Attribute Details

#rest_clientObject (readonly)

Returns the value of attribute rest_client.



28
29
30
# File 'lib/kucoin/api/websocket.rb', line 28

def rest_client
  @rest_client
end

Class Method Details

.open_tunnel(channel:, params: {}) ⇒ Object



19
20
21
22
23
24
25
# 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_idObject



7
# File 'lib/kucoin/api/websocket.rb', line 7

def request_id; rand(Time.now.to_i) end

.subscribe(channel:, params: {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# 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

Instance Method Details

#all_ticker(methods:) ⇒ Object



76
77
78
# File 'lib/kucoin/api/websocket.rb', line 76

def all_ticker methods:
  ticker symbols: :all, methods: methods
end

#balance(methods:) ⇒ Object



104
105
106
# 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: {}, methods:) ⇒ Object



34
35
36
# 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) ⇒ Object



94
95
96
# 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:) ⇒ Object



86
87
88
# 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

#match_execution_data(symbols:, methods:, private_channel: false) ⇒ Object



90
91
92
# 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:) ⇒ Object

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)


65
66
67
68
69
# 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:) ⇒ Object Also known as: symbol_snapshot, market_snapshot



80
81
82
# File 'lib/kucoin/api/websocket.rb', line 80

def snapshot symbol:, methods:
  start stream: { topic: "/market/snapshot:#{symbol}" }, methods: methods
end

#start(stream:, methods:) ⇒ Object

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)


52
53
54
55
56
# 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_received_event(symbols:, methods:) ⇒ Object Also known as: stop_order_activate_event

PRIVATE



99
100
101
# 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

#ticker(symbols:, methods:) ⇒ Object

PUBLIC



72
73
74
# File 'lib/kucoin/api/websocket.rb', line 72

def ticker symbols:, methods:
  start stream: { topic: topic_path('/market/ticker', symbols) }, methods: methods
end