Class: Cryptoexchange::Client
- Inherits:
-
Object
- Object
- Cryptoexchange::Client
- Defined in:
- lib/cryptoexchange/client.rb
Instance Method Summary collapse
- #available_exchanges ⇒ Object
- #exchange_for(currency) ⇒ Object
-
#initialize(ticker_ttl: 3, cache_size: 200) ⇒ Client
constructor
A new instance of Client.
- #order_book(market_pair) ⇒ Object
- #order_book_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) ⇒ Object
- #pairs(exchange) ⇒ Object
- #ticker(market_pair) ⇒ Object
- #ticker_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) ⇒ Object
- #trade_page_url(exchange, args = {}) ⇒ Object
- #trade_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) ⇒ Object
- #trades(market_pair) ⇒ Object
Constructor Details
#initialize(ticker_ttl: 3, cache_size: 200) ⇒ Client
Returns a new instance of Client.
3 4 5 |
# File 'lib/cryptoexchange/client.rb', line 3 def initialize(ticker_ttl: 3, cache_size: 200) LruTtlCache.ticker_cache(ticker_ttl, cache_size) end |
Instance Method Details
#available_exchanges ⇒ Object
37 38 39 40 41 42 |
# File 'lib/cryptoexchange/client.rb', line 37 def available_exchanges folder_names = Dir[File.join(File.dirname(__dir__), 'cryptoexchange', 'exchanges', '**')] folder_names.map do |folder_name| folder_name.split('/').last end.sort end |
#exchange_for(currency) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cryptoexchange/client.rb', line 44 def exchange_for(currency) exchanges = [] available_exchanges.each do |exchange| pairs = pairs(exchange) next if pairs.is_a?(Hash) && !pairs[:error].empty? pairs.each do |pair| if [pair.base, pair.target].include?(currency.upcase) exchanges << exchange break end end end exchanges.uniq.sort end |
#order_book(market_pair) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cryptoexchange/client.rb', line 59 def order_book(market_pair) exchange = market_pair.market market_classname = "Cryptoexchange::Exchanges::#{StringHelper.camelize(exchange)}::Services::OrderBook" market_class = Object.const_get(market_classname) order_book = market_class.new if market_class.supports_individual_ticker_query? order_book.fetch(market_pair) else order_books = order_book.fetch order_books.find do |o| o.base.casecmp(market_pair.base) == 0 && o.target.casecmp(market_pair.target) == 0 end end end |
#order_book_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/cryptoexchange/client.rb', line 104 def order_book_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) fetch_stream( market_pair: market_pair, onopen: onopen, onmessage: , onclose: onclose, stream_type: 'order_book' ) end |
#pairs(exchange) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/cryptoexchange/client.rb', line 13 def pairs(exchange) pairs_classname = "Cryptoexchange::Exchanges::#{StringHelper.camelize(exchange)}::Services::Pairs" pairs_class = Object.const_get(pairs_classname) pairs_object = pairs_class.new pairs_object.fetch end |
#ticker(market_pair) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cryptoexchange/client.rb', line 20 def ticker(market_pair) exchange = market_pair.market market_classname = "Cryptoexchange::Exchanges::#{StringHelper.camelize(exchange)}::Services::Market" market_class = Object.const_get(market_classname) market = market_class.new if market_class.supports_individual_ticker_query? market.fetch(market_pair) else tickers = market.fetch tickers.find do |t| t.base.casecmp(market_pair.base) == 0 && t.target.casecmp(market_pair.target) == 0 end end end |
#ticker_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/cryptoexchange/client.rb', line 84 def ticker_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) fetch_stream( market_pair: market_pair, onopen: onopen, onmessage: , onclose: onclose, stream_type: 'market' ) end |
#trade_page_url(exchange, args = {}) ⇒ Object
7 8 9 10 11 |
# File 'lib/cryptoexchange/client.rb', line 7 def trade_page_url(exchange, args={}) pairs_classname = "Cryptoexchange::Exchanges::#{StringHelper.camelize(exchange)}::Market" pairs_class = Object.const_get(pairs_classname) pairs_class.trade_page_url(base: args[:base], target: args[:target]) end |
#trade_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/cryptoexchange/client.rb', line 94 def trade_stream(market_pair:, onopen: nil, onmessage:, onclose: nil) fetch_stream( market_pair: market_pair, onopen: onopen, onmessage: , onclose: onclose, stream_type: 'trade' ) end |
#trades(market_pair) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/cryptoexchange/client.rb', line 76 def trades(market_pair) exchange = market_pair.market market_classname = "Cryptoexchange::Exchanges::#{StringHelper.camelize(exchange)}::Services::Trades" market_class = Object.const_get(market_classname) trades = market_class.new trades.fetch(market_pair) end |