Class: Cryptoexchange::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptoexchange/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(ticker_ttl: 3) ⇒ Client

Returns a new instance of Client.



3
4
5
# File 'lib/cryptoexchange/client.rb', line 3

def initialize(ticker_ttl: 3)
  LruTtlCache.ticker_cache(ticker_ttl)
end

Instance Method Details

#available_exchangesObject



34
35
36
37
38
39
# File 'lib/cryptoexchange/client.rb', line 34

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cryptoexchange/client.rb', line 41

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cryptoexchange/client.rb', line 56

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

#pairs(exchange) ⇒ Object



7
8
9
10
11
12
# File 'lib/cryptoexchange/client.rb', line 7

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cryptoexchange/client.rb', line 14

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

  rescue NoMethodError => e
    raise Cryptoexchange::ResultParseError, { response: "General no method error thrown" }
end

#trades(market_pair) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/cryptoexchange/client.rb', line 73

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