class Money::Bank::CoinMarketCap

Constants

BASE_CURRENCY
EXCHANGE_URL

Attributes

currencies_list[R]

List of currencies

rates_expire_at[R]

Rates expiration time

ttl_in_seconds[RW]

Seconds after which the current rates are automatically expired

Public Class Methods

new(*args, &block) click to toggle source
Calls superclass method
# File lib/money/bank/coin_market_cap.rb, line 20
def initialize(*args, &block)
  super

  @currencies_list = []

  @ttl_in_seconds = 3600 # 1 hour
end

Public Instance Methods

get_rate(iso_from, iso_to, opts = {}) click to toggle source
Calls superclass method
# File lib/money/bank/coin_market_cap.rb, line 40
def get_rate(iso_from, iso_to, opts = {})
  update_rates_if_expired

  super || get_indirect_rate(iso_from, iso_to, opts)
end
Also aliased as: original_get_rate
original_get_rate(iso_from, iso_to, opts = {})
Alias for: get_rate
update_rates() click to toggle source
# File lib/money/bank/coin_market_cap.rb, line 28
def update_rates
  store.each_rate do |iso_from, iso_to, _rate|
    add_rate(iso_from, iso_to, nil)
  end

  @currencies_list = []

  add_exchange_rates
end

Private Instance Methods

add_exchange_rates() click to toggle source
# File lib/money/bank/coin_market_cap.rb, line 71
def add_exchange_rates
  currencies.each do |currency|
    next if currency['availableSupplyNumber'] == 0

    iso_from = currency['symbol']
    @currencies_list << iso_from

    currency['price'].each do |iso_to, rate|
      iso_to = iso_to.upcase
      next unless Money::Currency.find(iso_from) && Money::Currency.find(iso_to)

      add_rate(iso_from, iso_to, rate)
      add_rate(iso_to, iso_from, 1.0 / rate)
    end
  end
end
currencies() click to toggle source
# File lib/money/bank/coin_market_cap.rb, line 64
def currencies
  response = open(EXCHANGE_URL).read
  @coins = JSON.parse(response)['markets']
  @rates_expire_at = Time.now + ttl_in_seconds
  @coins
end
get_indirect_rate(iso_from, iso_to, opts = {}) click to toggle source
# File lib/money/bank/coin_market_cap.rb, line 48
def get_indirect_rate(iso_from, iso_to, opts = {})
  return 1 if Money::Currency.wrap(iso_from).iso_code == Money::Currency.wrap(iso_to).iso_code

  rate_to_base = original_get_rate(iso_from, BASE_CURRENCY, opts)
  rate_from_base = original_get_rate(BASE_CURRENCY, iso_to, opts)

  return unless rate_to_base && rate_from_base

  rate = rate_to_base * rate_from_base

  add_rate(iso_from, iso_to, rate)
  add_rate(iso_to, iso_from, 1.0 / rate)

  rate
end
rates_expired?() click to toggle source
# File lib/money/bank/coin_market_cap.rb, line 92
def rates_expired?
  return true unless rates_expire_at

  rates_expire_at <= Time.now
end
update_rates_if_expired() click to toggle source
# File lib/money/bank/coin_market_cap.rb, line 88
def update_rates_if_expired
  update_rates if rates_expired?
end