cryptomarket-sdk 1.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,275 +0,0 @@
1
- require "securerandom"
2
-
3
- require_relative "wsClientBase"
4
- require_relative"../utils"
5
-
6
-
7
-
8
-
9
- module Cryptomarket
10
- module Websocket
11
- # PublicClient connects via websocket to cryptomarket to get market information of the exchange.
12
- #
13
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the client once the connection is established. if an error ocurrs is return as the fist parameter of the callback: callback(err, client)
14
-
15
- class PublicClient < ClientBase
16
- include Utils
17
-
18
- def initialize()
19
- orderbook = "orderbook"
20
- tickers = "tickers"
21
- trades = "trades"
22
- candles = "candles"
23
- super(
24
- url:"wss://api.exchange.cryptomkt.com/api/2/ws/public",
25
- subscriptionKeys:{
26
- "subscribeTicker" => tickers,
27
- "unsubscribeTicker" => tickers,
28
- "ticker" => tickers,
29
-
30
- "subscribeOrderbook" => orderbook,
31
- "unsubscribeOrderbook" => orderbook,
32
- "snapshotOrderbook" => orderbook,
33
- "updateOrderbook" => orderbook,
34
-
35
- "subscribeTrades" => trades,
36
- "unsubscribeTrades" => trades,
37
- "snapshotTrades" => trades,
38
- "updateTrades" => trades,
39
-
40
- "subscribeCandles" => candles,
41
- "unsubscribeCandles" => candles,
42
- "snapshotCandles" => candles,
43
- "updateCandles" => candles
44
- })
45
- @OBCache = OrderbookCache.new
46
- end
47
-
48
- def handleNotification(notification)
49
- method = notification['method']
50
- params = notification['params']
51
- key = buildKey(method, params)
52
- callback = @callbackCache.getSubscriptionCallback(key)
53
- if callback.nil?
54
- return
55
- end
56
- if orderbookFeed(method)
57
- @OBCache.update(method, key, params)
58
- if @OBCache.orderbookBroken(key)
59
- storeAndSend('subscribeOrderbook', {'symbol' => params['symbol']}, nil)
60
- @OBCache.waitOrderbook(key)
61
- return
62
- end
63
- if @OBCache.orderbookWating(key)
64
- return
65
- end
66
- params = @OBCache.getOrderbook(key)
67
- end
68
- if candlesFeed(method) or tradesFeed(method)
69
- params = params["data"]
70
- end
71
- callback.call(params)
72
- end
73
-
74
- def buildKey(method, params)
75
- methodKey = @subscriptionKeys[method]
76
-
77
- symbol = ''
78
- if params.has_key? 'symbol'
79
- symbol = params['symbol']
80
- end
81
- period = ''
82
- if params.has_key? 'period'
83
- period = params['period']
84
- end
85
- key = methodKey + ':' + symbol + ':' + period
86
- return key.upcase
87
- end
88
-
89
- def orderbookFeed(method)
90
- return @subscriptionKeys[method] == "orderbook"
91
- end
92
-
93
- def tradesFeed(method)
94
- return @subscriptionKeys[method] == "trades"
95
- end
96
-
97
- def candlesFeed(method)
98
- return @subscriptionKeys[method] == "candles"
99
- end
100
-
101
-
102
- # Get a list all available currencies on the exchange
103
- #
104
- # https://api.exchange.cryptomkt.com/#get-currencies
105
- #
106
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
107
-
108
- def getCurrencies(callback)
109
- sendById("getCurrencies", callback)
110
- end
111
-
112
- # Get the data of a currency
113
- #
114
- # https://api.exchange.cryptomkt.com/#get-currencies
115
- #
116
- # +String+ +currency+:: A currency id
117
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
118
-
119
- def getCurrency(currency, callback)
120
- sendById('getCurrency', callback, {'currency' => currency})
121
- end
122
-
123
- # Get a list of the specified symbols or all of them if no symbols are specified
124
- #
125
- # A symbol is the combination of the base currency (first one) and quote currency (second one)
126
- #
127
- # https://api.exchange.cryptomkt.com/#get-symbols
128
- #
129
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
130
-
131
- def getSymbols(callback)
132
- sendById('getSymbols', callback)
133
- end
134
-
135
- # Get a symbol by its id
136
- #
137
- # A symbol is the combination of the base currency (first one) and quote currency (second one)
138
- #
139
- # https://api.exchange.cryptomkt.com/#get-symbols
140
- #
141
- #
142
- # +String+ +symbol+:: A symbol id
143
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
144
-
145
- def getSymbol(symbol, callback)
146
- sendById('getSymbol', callback, {'symbol' => symbol})
147
- end
148
-
149
- # Subscribe to a ticker of a symbol
150
- #
151
- # https://api.exchange.cryptomkt.com/#subscribe-to-ticker
152
- #
153
- # +String+ +symbol+:: A symbol to subscribe
154
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes one argument. The ticker feed
155
- # +Proc+ +resultCallback+:: Optional. A function to call with the subscription result. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
156
-
157
- def subscribeToTicker(symbol, callback, resultCallback=nil)
158
- sendSubscription('subscribeTicker', callback, {'symbol' => symbol}, resultCallback)
159
- end
160
-
161
- # Unsubscribe to a ticker of a symbol
162
- #
163
- # https://api.exchange.cryptomkt.com/#subscribe-to-ticker
164
- #
165
- #
166
- # +String+ +symbol+:: The symbol to stop the ticker subscribption
167
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
168
-
169
- def unsubscribeToTicker(symbol, callback=nil)
170
- sendUnsubscription('unsubscribeTicker', callback, {'symbol' => symbol})
171
- end
172
-
173
- # Subscribe to the order book of a symbol
174
- #
175
- # An Order Book is an electronic list of buy and sell orders for a specific symbol, structured by price level
176
- #
177
- # https://api.exchange.cryptomkt.com/#subscribe-to-order-book
178
- #
179
- # +String+ +symbol+:: The symbol of the orderbook
180
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes one argument. the order book feed
181
- # +Proc+ +resultCallback+:: A function to call with the subscription result. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
182
-
183
- def subscribeToOrderbook(symbol, callback, resultCallback=nil)
184
- sendSubscription('subscribeOrderbook', callback, {'symbol' => symbol}, resultCallback)
185
- end
186
-
187
- # Unsubscribe to an order book of a symbol
188
- #
189
- # An Order Book is an electronic list of buy and sell orders for a specific symbol, structured by price level
190
- #
191
- # https://api.exchange.cryptomkt.com/#subscribe-to-order-book
192
- #
193
- # +String+ +symbol+:: The symbol of the orderbook
194
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
195
-
196
- def unsubscribeToOrderbook(symbol, callback=nil)
197
- sendUnsubscription('unsubscribeOrderbook', callback, {'symbol' => symbol})
198
- end
199
-
200
- # Subscribe to the trades of a symbol
201
- #
202
- # https://api.exchange.cryptomkt.com/#subscribe-to-trades
203
- #
204
- # +String+ +symbol+:: The symbol of the trades
205
- # +Integer+ [limit] Optional. Maximum number of trades in the first feed, the nexts feeds have one trade
206
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes one argument. the trades feed
207
- # +Proc+ +resultCallback+:: Optional. A function to call with the subscription result. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
208
-
209
- def subscribeToTrades(symbol, callback, limit=nil, resultCallback=nil)
210
- params = {'symbol' => symbol}
211
- sendSubscription('subscribeTrades', callback, params, resultCallback)
212
- end
213
-
214
- # Unsubscribe to a trades of a symbol
215
- #
216
- # https://api.exchange.cryptomkt.com/#subscribe-to-trades
217
- #
218
- # +String+ +symbol+:: The symbol of the trades
219
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
220
-
221
- def unsubscribeToTrades(symbol, callback=nil)
222
- sendUnsubscription('unsubscribeTrades', callback, {'symbol' => symbol})
223
- end
224
-
225
-
226
- # Get trades of the specified symbol
227
- #
228
- # https://api.exchange.cryptomkt.com/#get-trades
229
- #
230
- # +String+ +symbol+:: The symbol to get the trades
231
- # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
232
- # +String+ +from+:: Optional. Initial value of the queried interval.
233
- # +String+ +till+:: Optional. Last value of the queried interval.
234
- # +Integer+ +limit+:: Optional. Trades per query. Defaul is 100. Max is 1000
235
- # +Integer+ +offset+:: Optional. Default is 0. Max is 100000
236
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
237
-
238
- def getTrades(symbol, callback, from:nil, till:nil, limit:nil, offset:nil)
239
- params = {'symbol' => symbol}
240
- extend_hash_with_pagination! params, from:from, till:till, limit:limit, offset:offset
241
- sendById('getTrades', callback, params)
242
- end
243
-
244
- # Subscribe to the candles of a symbol, at the given period
245
- #
246
- # Candels are used for OHLC representation
247
- #
248
- #
249
- # https://api.exchange.cryptomkt.com/#subscribe-to-candles
250
- #
251
- # +String+ +symbol+:: A symbol to recieve a candle feed
252
- # +String+ period A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month)
253
- # +Integer+ +limit+:: Optional. Maximum number of candles in the first feed. The rest of the feeds have one candle
254
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes one argument. recieves the candle feed
255
- # +Proc+ +resultCallback+:: Optional. A callable to call with the subscription result. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
256
-
257
- def subscribeToCandles(symbol, period, limit, callback, resultCallback=nil)
258
- params = {'symbol' => symbol, 'period' => period}
259
- sendSubscription('subscribeCandles', callback, params, resultCallback)
260
- end
261
-
262
- # Unsubscribe to the candles of a symbol at a given period
263
- #
264
- # https://api.exchange.cryptomkt.com/#subscribe-to-candles
265
- #
266
- # +String+ +symbol+:: The symbol of the candles
267
- # +String+ period 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month)
268
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
269
-
270
- def unsubscribeToCandles(symbol, period, callback=nil)
271
- sendUnsubscription('unsubscribeCandles', callback, {'symbol'=> symbol, 'period' => period})
272
- end
273
- end
274
- end
275
- end
@@ -1,134 +0,0 @@
1
- require_relative "authClient"
2
- require_relative"../utils"
3
-
4
- module Cryptomarket
5
- module Websocket
6
-
7
- # TradingClient connects via websocket to cryptomarket to enable the user to manage orders. uses SHA256 as auth method and authenticates automatically.
8
- #
9
- # +string+ +apiKey+:: the user api key
10
- # +string+ +apiSecret+:: the user api secret
11
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the client once the connection is established and the authentication is successful. if an error ocurrs is return as the fist parameter of the callback: callback(err, client)
12
-
13
- class TradingClient < AuthClient
14
- include Utils
15
- # Creates a new client
16
-
17
- def initialize(apiKey:, apiSecret:)
18
- reports = "reports"
19
- super(
20
- url:"wss://api.exchange.cryptomkt.com/api/2/ws/trading",
21
- apiKey:apiKey,
22
- apiSecret:apiSecret,
23
- subscriptionKeys:{
24
- "subscribeReports" => reports,
25
- "unsubscribeReports" => reports,
26
- "activeOrders" => reports,
27
- "report" => reports,
28
- })
29
- end
30
-
31
- # Subscribe to a feed of trading events of the account
32
- #
33
- # Requires authentication
34
- #
35
- # https://api.exchange.cryptomkt.com/#subscribe-to-reports
36
- #
37
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes one argument. a feed of reports
38
- # +Proc+ +resultCallback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
39
-
40
- def subscribeToReports(callback, resultCallback=nil)
41
- sendSubscription('subscribeReports', callback, {}, resultCallback)
42
- end
43
-
44
- # Create a new order
45
- #
46
- # Requires authentication
47
- #
48
- # https://api.exchange.cryptomkt.com/#place-new-order
49
- #
50
- # +String+ +clientOrderId+:: If given must be unique within the trading day, including all active orders. If not given, is generated by the server
51
- # +String+ +symbol+:: Trading symbol
52
- # +String+ +side+:: 'buy' or 'sell'
53
- # +String+ +quantity+:: Order quantity
54
- # +String+ +type+:: Optional. 'limit', 'market', 'stopLimit' or 'stopMarket'. Default is 'limit'
55
- # +String+ +timeInForce+:: Optional. 'GTC', 'IOC', 'FOK', 'Day', 'GTD'
56
- # +String+ +price+:: Required for 'limit' and 'stopLimit'. limit price of the order
57
- # +String+ +stopPrice+:: Required for 'stopLimit' and 'stopMarket' orders. stop price of the order
58
- # +String+ +expireTime+:: Required for orders with timeInForce = 'GDT'
59
- # +bool+ +strictValidate+:: Optional. If False, the server rounds half down for tickerSize and quantityIncrement. Example of ETHBTC: tickSize = '0.000001', then price '0.046016' is valid, '0.0460165' is invalid
60
- # +bool+ +postOnly+:: Optional. If True, your postOnly order causes a match with a pre-existing order as a taker, then the order will be cancelled
61
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
62
-
63
- def createOrder(clientOrderId:, symbol:, side:, quantity:, type:nil, timeInForce:nil, price:nil, stopPrice:nil, expireTime:nil, strictValidate:nil, postOnly:nil, callback:nil)
64
- params = {'symbol' => symbol, 'side' => side, 'quantity' => quantity, 'clientOrderId' => clientOrderId}
65
- extend_hash_with_order_params! params, type:type, timeInForce:timeInForce, price:price, stopPrice:stopPrice, expireTime:expireTime, strictValidate:strictValidate, postOnly:postOnly
66
- sendById('newOrder', callback, params)
67
- end
68
-
69
- # Cancel the order with ClientOrderId
70
- #
71
- # Requires authentication
72
- #
73
- # https://api.exchange.cryptomkt.com/#cancel-order
74
- #
75
- # +String+ +clientOrderId+:: The client order id of the order to cancel
76
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
77
-
78
- def cancelOrder(clientOrderId, callback:nil)
79
- sendById('cancelOrder', callback, {'clientOrderId' => clientOrderId})
80
- end
81
-
82
- # Rewrites an order, canceling it or replacing it
83
- #
84
- # The Cancel/Replace request is used to change the parameters of an existing order and to change the quantity or price attribute of an open order
85
- #
86
- # Do not use this request to cancel the quantity remaining in an outstanding order. Use the cancel_order for this purpose
87
- #
88
- # It is stipulated that a newly entered order cancels a prior order that has been entered, but not yet executed
89
- #
90
- # Requires authentication
91
- #
92
- # https://api.exchange.cryptomkt.com/#cancel-replace-order
93
- #
94
- # +String+ +clientOrderId+:: The client id of the order to modify
95
- # +String+ +requestClientId+:: The new id for the modified order
96
- # +String+ +quantity+:: The new quantity of the order
97
- # +String+ +price+:: The new price of the order
98
- # +bool+ +strictValidate+:: Optional. If False, the server rounds half down for tickerSize and quantityIncrement. Example of ETHBTC: tickSize = '0.000001', then price '0.046016' is valid, '0.0460165' is invalid
99
- # +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
100
-
101
- def replaceOrder(clientOrderId:, requestClientId:, quantity:, price:, strictValidate:nil, callback:nil)
102
- params = {'clientOrderId' => clientOrderId, 'requestClientId' => requestClientId, 'quantity' => quantity, 'price' => price}
103
- if not strictValidate.nil?
104
- params['strictValidate'] = strictValidate
105
- end
106
- sendById('cancelReplaceOrder', callback, params)
107
- end
108
-
109
- # Get the account active orders
110
- #
111
- # Requires authentication
112
- #
113
- # https://api.exchange.cryptomkt.com/#get-active-orders-2
114
- #
115
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
116
-
117
- def getActiveOrders(callback)
118
- sendById('getOrders', callback)
119
- end
120
-
121
- # Get the user trading balance
122
- #
123
- # Requires authentication
124
- #
125
- # https://api.exchange.cryptomkt.com/#get-trading-balance
126
- #
127
- # +Proc+ +callback+:: A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
128
-
129
- def getTradingBalance(callback)
130
- sendById('getTradingBalance', callback)
131
- end
132
- end
133
- end
134
- end
@@ -1,158 +0,0 @@
1
- require_relative "callbackCache"
2
- require_relative "orderbookCache"
3
- require_relative "wsManager"
4
- require_relative '../exceptions'
5
-
6
- module Cryptomarket
7
- module Websocket
8
- class ClientBase
9
- def initialize(url:, subscriptionKeys:)
10
- @subscriptionKeys = subscriptionKeys
11
- @callbackCache = CallbackCache.new
12
- @wsmanager = WSManager.new self, url:url
13
- @onconnect = ->{}
14
- @onerror = ->(error){}
15
- @onclose = ->{}
16
- end
17
-
18
- def connected?
19
- return @wsmanager.connected?
20
- end
21
-
22
- def close()
23
- @wsmanager.close()
24
- end
25
-
26
- # connects via websocket to the exchange, it blocks until the connection is stablished
27
- def connect()
28
- @wsmanager.connect()
29
- while (not @wsmanager.connected?)
30
- sleep(1)
31
- end
32
- end
33
-
34
- def on_open()
35
- @onconnect.call()
36
- end
37
-
38
- def onconnect=(callback=nil, &block)
39
- callback = callback || block
40
- @onconnect = callback
41
- end
42
-
43
- def onconnect()
44
- @onconnect.call()
45
- end
46
-
47
- def onerror=(callback=nil, &block)
48
- callback = callback || block
49
- @onerror = callback
50
- end
51
-
52
- def onerror(error)
53
- @onerror.call(error)
54
- end
55
-
56
- def onclose=(callback=nil, &block)
57
- callback = callback || block
58
- @onclose = callback
59
- end
60
-
61
- def onclose()
62
- @onclose.call()
63
- end
64
-
65
- def sendSubscription(method, callback, params, resultCallback)
66
- key = buildKey(method, params)
67
- @callbackCache.storeSubscriptionCallback(key, callback)
68
- storeAndSend(method, params, resultCallback)
69
- end
70
-
71
- def sendUnsubscription(method, callback, params)
72
- key = buildKey(method, params)
73
- @callbackCache.deleteSubscriptionCallback(key)
74
- storeAndSend(method, params, callback)
75
- end
76
-
77
- def sendById(method, callback, params={})
78
- storeAndSend(method, params, callback)
79
- end
80
-
81
- def storeAndSend(method, params, callbackToStore=nil)
82
- payload = {'method' => method, 'params' => params}
83
- if not callbackToStore.nil?
84
- id = @callbackCache.storeCallback(callbackToStore)
85
- payload['id'] = id
86
- end
87
- @wsmanager.send(payload)
88
- end
89
-
90
- def handle(message)
91
- if message.has_key? 'method'
92
- handleNotification(message)
93
- elsif message.has_key? 'id'
94
- handleResponse(message)
95
- end
96
- end
97
-
98
- def handleNotification(notification)
99
- key = buildKey(notification["method"])
100
- callback = @callbackCache.getSubscriptionCallback(key)
101
- if callback.nil?
102
- return
103
- end
104
- callback.call(notification["params"])
105
- end
106
-
107
- def buildKey(method=nil, params=nil)
108
- if @subscriptionKeys.key? method
109
- return @subscriptionKeys[method]
110
- end
111
- return "subscription"
112
- end
113
-
114
- def handleResponse(response)
115
- id = response['id']
116
- if id.nil?
117
- return
118
- end
119
- callback = @callbackCache.popCallback(id)
120
- if callback.nil?
121
- return
122
- end
123
- if response.has_key? 'error'
124
- callback.call(Cryptomarket::APIException.new(response['error']), nil)
125
- return
126
- elsif
127
- result = response['result']
128
- if result.is_a?(Hash) and result.has_key? 'data'
129
- callback.call(nil, result['data'])
130
- else
131
- callback.call(nil, result)
132
- end
133
- end
134
- end
135
-
136
- def handleNotification(notification)
137
- key = "subscription"
138
- callback = @callbackCache.getSubscriptionCallback(key)
139
- if callback.nil?
140
- return
141
- end
142
- if notification["params"].kind_of?(Array)
143
- notification["params"].each{|feed| callback.call(feed)}
144
- else
145
- callback.call(notification["params"])
146
- end
147
- end
148
-
149
- def buildKey(method=nil, params=nil)
150
- return "subscription"
151
- end
152
-
153
- def close()
154
- @wsmanager.close()
155
- end
156
- end
157
- end
158
- end
@@ -1,59 +0,0 @@
1
- require 'json'
2
- require 'faye/websocket'
3
- require 'eventmachine'
4
-
5
- require_relative '../exceptions'
6
-
7
- module Cryptomarket
8
- module Websocket
9
- class WSManager
10
- def initialize(handler, url:)
11
- @url = url
12
- @handler = handler
13
- @connected = false
14
- end
15
-
16
- def connected?
17
- return @connected
18
- end
19
-
20
- def connect()
21
- @thread = Thread.new do
22
- EM.run {
23
- @ws = Faye::WebSocket::Client.new(@url)
24
-
25
- @ws.onopen = lambda do |event|
26
- @connected = true
27
- @handler.on_open()
28
- end
29
-
30
- @ws.onclose = lambda do |close|
31
- @handler.onclose()
32
- EM.stop
33
- end
34
-
35
- @ws.onerror = lambda do |error|
36
- @handler.onerror(error)
37
- end
38
-
39
- @ws.onmessage = lambda do |message|
40
- @handler.handle(JSON.parse(message.data.to_s))
41
- end
42
- }
43
- end
44
- end
45
-
46
- def close
47
- @ws.close
48
- @connected = false
49
- end
50
-
51
- def send hash
52
- if not @connected
53
- raise Cryptomarket::SDKException.new, "connection closed"
54
- end
55
- @ws.send hash.to_json
56
- end
57
- end
58
- end
59
- end