Class: Cryptomarket::Websocket::PublicClient

Inherits:
ClientBase
  • Object
show all
Includes:
Utils, Methods
Defined in:
lib/cryptomarket/websocket/publicClient.rb

Overview

PublicClient connects via websocket to cryptomarket to get market information of the exchange.

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)

Constant Summary

Constants included from Methods

Methods::CANDLES, Methods::MAP, Methods::ORDERBOOK, Methods::REPORTS, Methods::TICKERS, Methods::TRADES

Instance Method Summary collapse

Methods included from Methods

#candlesFeed, #mapping, #orderbookFeed, #reportsFeed, #tradesFeed

Methods included from Utils

#extend_hash_with_order_params!, #extend_hash_with_pagination!

Methods inherited from ClientBase

#close, #connect, #connected?, #handle, #handleResponse, #on_open, #onclose, #onclose=, #onconnect, #onconnect=, #onerror, #onerror=, #sendById, #sendSubscription, #sendUnsubscription, #storeAndSend

Constructor Details

#initializePublicClient

Returns a new instance of PublicClient.



20
21
22
23
# File 'lib/cryptomarket/websocket/publicClient.rb', line 20

def initialize()
    @OBCache = OrderbookCache.new
    super url:"wss://api.exchange.cryptomkt.com/api/2/ws/public"
end

Instance Method Details

#buildKey(method, params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cryptomarket/websocket/publicClient.rb', line 51

def buildKey(method, params)
    methodKey = mapping(method)

    symbol =  ''
    if params.has_key? 'symbol'
        symbol = params['symbol']
    end
    period = ''
    if params.has_key? 'period'
        period = params['period']
    end
    key = methodKey + ':' + symbol + ':' + period
    return key.upcase
end

#getCurrencies(callback) ⇒ Object

Get a list all available currencies on the exchange

api.exchange.cryptomkt.com/#get-currencies

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| …}



72
73
74
# File 'lib/cryptomarket/websocket/publicClient.rb', line 72

def getCurrencies(callback)
    sendById("getCurrencies", callback)
end

#getCurrency(currency, callback) ⇒ Object

Get the data of a currency

api.exchange.cryptomkt.com/#get-currencies

String currency

A currency id

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| …}



83
84
85
# File 'lib/cryptomarket/websocket/publicClient.rb', line 83

def getCurrency(currency, callback)
    sendById('getCurrency', callback, {'currency' => currency})
end

#getSymbol(symbol, callback) ⇒ Object

Get a symbol by its id

A symbol is the combination of the base currency (first one) and quote currency (second one)

api.exchange.cryptomkt.com/#get-symbols

String symbol

A symbol id

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| …}



109
110
111
# File 'lib/cryptomarket/websocket/publicClient.rb', line 109

def getSymbol(symbol, callback)
    sendById('getSymbol', callback, {'symbol' => symbol})
end

#getSymbols(callback) ⇒ Object

Get a list of the specified symbols or all of them if no symbols are specified

A symbol is the combination of the base currency (first one) and quote currency (second one)

api.exchange.cryptomkt.com/#get-symbols

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| …}



95
96
97
# File 'lib/cryptomarket/websocket/publicClient.rb', line 95

def getSymbols(callback)
    sendById('getSymbols', callback)
end

#getTrades(symbol, callback, from: nil, till: nil, limit: nil, offset: nil) ⇒ Object

Get trades of the specified symbol

api.exchange.cryptomkt.com/#get-trades

String symbol

The symbol to get the trades

String sort

Optional. Sort direction. ‘ASC’ or ‘DESC’. Default is ‘DESC’

String from

Optional. Initial value of the queried interval.

String till

Optional. Last value of the queried interval.

Integer limit

Optional. Trades per query. Defaul is 100. Max is 1000

Integer offset

Optional. Default is 0. Max is 100000

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| …}



202
203
204
205
206
# File 'lib/cryptomarket/websocket/publicClient.rb', line 202

def getTrades(symbol, callback, from:nil, till:nil, limit:nil, offset:nil)
    params = {'symbol' => symbol}
    extend_hash_with_pagination! params, from:from, till:till, limit:limit, offset:offset
    sendById('getTrades', callback, params)
end

#handleNotification(notification) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cryptomarket/websocket/publicClient.rb', line 25

def handleNotification(notification)
    method = notification['method']
    params = notification['params']
    key = buildKey(method, params)
    callback = @callbackCache.getSubscriptionCallback(key)
    if callback.nil?
        return
    end
    if orderbookFeed(method)
        @OBCache.update(method, key, params)
        if @OBCache.orderbookBroken(key)
            storeAndSend('subscribeOrderbook', {'symbol' => params['symbol']}, nil)
            @OBCache.waitOrderbook(key)
            return
        end
        if @OBCache.orderbookWating(key)
            return
        end
        params = @OBCache.getOrderbook(key)
    end
    if candlesFeed(method) or tradesFeed(method)
        params = params["data"]
    end
    callback.call(params) 
end

#subscribeToCandles(symbol, period, limit, callback, resultCallback = nil) ⇒ Object

Subscribe to the candles of a symbol, at the given period

Candels are used for OHLC representation

api.exchange.cryptomkt.com/#subscribe-to-candles

String symbol

A symbol to recieve a candle feed

String period A valid tick interval. ‘M1’ (one minute), ‘M3’, ‘M5’, ‘M15’, ‘M30’, ‘H1’ (one hour), ‘H4’, ‘D1’ (one day), ‘D7’, ‘1M’ (one month)

Integer limit

Optional. Maximum number of candles in the first feed. The rest of the feeds have one candle

Proc callback

A Proc to call with the result data. It takes one argument. recieves the candle feed

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| …}



221
222
223
224
# File 'lib/cryptomarket/websocket/publicClient.rb', line 221

def subscribeToCandles(symbol, period, limit, callback, resultCallback=nil)
    params = {'symbol' => symbol, 'period' => period}
    sendSubscription('subscribeCandles', callback, params, resultCallback)
end

#subscribeToOrderbook(symbol, callback, resultCallback = nil) ⇒ Object

Subscribe to the order book of a symbol

An Order Book is an electronic list of buy and sell orders for a specific symbol, structured by price level

api.exchange.cryptomkt.com/#subscribe-to-order-book

String symbol

The symbol of the orderbook

Proc callback

A Proc to call with the result data. It takes one argument. the order book feed

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| …}



147
148
149
# File 'lib/cryptomarket/websocket/publicClient.rb', line 147

def subscribeToOrderbook(symbol, callback, resultCallback=nil)
    sendSubscription('subscribeOrderbook', callback, {'symbol' => symbol}, resultCallback)
end

#subscribeToTicker(symbol, callback, resultCallback = nil) ⇒ Object

Subscribe to a ticker of a symbol

api.exchange.cryptomkt.com/#subscribe-to-ticker

String symbol

A symbol to subscribe

Proc callback

A Proc to call with the result data. It takes one argument. The ticker feed

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| …}



121
122
123
# File 'lib/cryptomarket/websocket/publicClient.rb', line 121

def subscribeToTicker(symbol, callback, resultCallback=nil)
    sendSubscription('subscribeTicker', callback, {'symbol' => symbol}, resultCallback)
end

#subscribeToTrades(symbol, callback, limit = nil, resultCallback = nil) ⇒ Object

Subscribe to the trades of a symbol

api.exchange.cryptomkt.com/#subscribe-to-trades

String symbol

The symbol of the trades

Integer [limit] Optional. Maximum number of trades in the first feed, the nexts feeds have one trade

Proc callback

A Proc to call with the result data. It takes one argument. the trades feed

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| …}



173
174
175
176
# File 'lib/cryptomarket/websocket/publicClient.rb', line 173

def subscribeToTrades(symbol, callback, limit=nil, resultCallback=nil)
    params = {'symbol' => symbol}
    sendSubscription('subscribeTrades', callback, params, resultCallback)
end

#unsubscribeToCandles(symbol, period, callback = nil) ⇒ Object

Unsubscribe to the candles of a symbol at a given period

api.exchange.cryptomkt.com/#subscribe-to-candles

String symbol

The symbol of the candles

String period ‘M1’ (one minute), ‘M3’, ‘M5’, ‘M15’, ‘M30’, ‘H1’ (one hour), ‘H4’, ‘D1’ (one day), ‘D7’, ‘1M’ (one month)

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| …}



234
235
236
# File 'lib/cryptomarket/websocket/publicClient.rb', line 234

def unsubscribeToCandles(symbol, period, callback=nil)
    sendUnsubscription('unsubscribeCandles', callback, {'symbol'=> symbol, 'period' => period})
end

#unsubscribeToOrderbook(symbol, callback = nil) ⇒ Object

Unsubscribe to an order book of a symbol

An Order Book is an electronic list of buy and sell orders for a specific symbol, structured by price level

api.exchange.cryptomkt.com/#subscribe-to-order-book

String symbol

The symbol of the orderbook

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| …}



160
161
162
# File 'lib/cryptomarket/websocket/publicClient.rb', line 160

def unsubscribeToOrderbook(symbol, callback=nil)
    sendUnsubscription('unsubscribeOrderbook', callback, {'symbol' => symbol})
end

#unsubscribeToTicker(symbol, callback = nil) ⇒ Object

Unsubscribe to a ticker of a symbol

api.exchange.cryptomkt.com/#subscribe-to-ticker

String symbol

The symbol to stop the ticker subscribption

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| …}



133
134
135
# File 'lib/cryptomarket/websocket/publicClient.rb', line 133

def unsubscribeToTicker(symbol, callback=nil)
    sendUnsubscription('unsubscribeTicker', callback, {'symbol' => symbol})
end

#unsubscribeToTrades(symbol, callback = nil) ⇒ Object

Unsubscribe to a trades of a symbol

api.exchange.cryptomkt.com/#subscribe-to-trades

String symbol

The symbol of the trades

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| …}



185
186
187
# File 'lib/cryptomarket/websocket/publicClient.rb', line 185

def unsubscribeToTrades(symbol, callback=nil)
    sendUnsubscription('unsubscribeTrades', callback, {'symbol' => symbol})
end