class Money::Bank::FixerIo

Attributes

rates_expiration[R]

@return [Time] Returns the time when the rates expire.

ttl_in_seconds[R]

@return [Integer] Returns the Time To Live (TTL) in seconds.

rates[R]

@return [Hash] Stores the currently known rates.

Public Class Methods

refresh_rates_expiration!() click to toggle source

Set the rates expiration TTL seconds from the current time.

@return [Time] The next expiration.

# File lib/money-fixer-io.rb, line 28
def refresh_rates_expiration!
  @rates_expiration = Time.now + ttl_in_seconds
end
ttl_in_seconds=(value) click to toggle source

Set the Time To Live (TTL) in seconds.

@param [Integer] the seconds between an expiration and another.

# File lib/money-fixer-io.rb, line 19
def ttl_in_seconds=(value)
  @ttl_in_seconds = value
  refresh_rates_expiration! if ttl_in_seconds
end

Public Instance Methods

cache() click to toggle source
# File lib/money-fixer-io.rb, line 33
def cache
  @@money_bank_fixer_io_cache ||= {} # rubocop:disable Style/ClassVars
end
expire_rates() click to toggle source

Flushes all the rates if they are expired.

@return [Boolean]

# File lib/money-fixer-io.rb, line 107
def expire_rates
  if self.class.ttl_in_seconds && self.class.rates_expiration <= Time.now
    flush_rates
    self.class.refresh_rates_expiration!
    true
  else
    false
  end
end
flush_rate(from, to) click to toggle source

Clears the specified rate stored in @rates.

@param [String, Symbol, Currency] from Currency to convert from (used

for key into @rates).

@param [String, Symbol, Currency] to Currency to convert to (used for

key into @rates).

@return [Float] The flushed rate.

@example

@bank = GoogleCurrency.new    #=> <Money::Bank::GoogleCurrency...>
@bank.get_rate(:USD, :EUR)    #=> 0.776337241
@bank.flush_rate(:USD, :EUR)  #=> 0.776337241
# File lib/money-fixer-io.rb, line 64
def flush_rate(from, to)
  store.remove_rate(from, to)
end
flush_rates() click to toggle source

Clears all rates stored in @rates

@return [Hash] The empty @rates Hash.

@example

@bank = GoogleCurrency.new  #=> <Money::Bank::GoogleCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241
@bank.flush_rates           #=> {}
# File lib/money-fixer-io.rb, line 46
def flush_rates
  store.clear_rates
end
get_rate(from, to, args = {}) click to toggle source

Returns the requested rate.

It also flushes all the rates when and if they are expired.

@param [String, Symbol, Currency] from Currency to convert from @param [String, Symbol, Currency] to Currency to convert to

@return [Float] The requested rate.

@example

@bank = GoogleCurrency.new  #=> <Money::Bank::GoogleCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241
# File lib/money-fixer-io.rb, line 81
def get_rate(from, to, args = {})
  expire_rates

  if args[:exchanged_at]
    get_rate_with_exchange_rate(from, to, args)
  else
    store.get_rate(from, to) || store.add_rate(from, to, fetch_rate(from, to))
  end
end
get_rate_with_exchange_rate(from, to, args = {}) click to toggle source
# File lib/money-fixer-io.rb, line 91
def get_rate_with_exchange_rate(from, to, args = {})
  exchanged_at = args.fetch(:exchanged_at).strftime("%Y-%m-%d")

  if cache[from] && cache[from][to] && cache[from][to][exchanged_at]
    cache[from][to][exchanged_at]
  else
    cache[from] ||= {}
    cache[from][to] ||= {}
    cache[from][to][exchanged_at] = fetch_rate(from, to, exchanged_at: exchanged_at)
  end
end

Private Instance Methods

build_uri(from, _to, args = {}) click to toggle source

Build a URI for the given arguments.

@param [Currency] from The currency to convert from. @param [Currency] to The currency to convert to.

@return [URI::HTTP]

# File lib/money-fixer-io.rb, line 145
def build_uri(from, _to, args = {})
  if args[:exchanged_at]
    path = "/api/#{args.fetch(:exchanged_at)}"
  else
    path = "/api/latest"
  end

  query = {base: from.iso_code}
  query[:access_key] = ENV["FIXER_IO_API_KEY"] if ENV["FIXER_IO_API_KEY"]

  URI::HTTP.build(
    host: "data.fixer.io",
    path: path,
    query: URI.encode_www_form(query)
  )
end
fetch_rate(from, to, args = {}) click to toggle source

Queries for the requested rate and returns it.

@param [String, Symbol, Currency] from Currency to convert from @param [String, Symbol, Currency] to Currency to convert to

@return [BigDecimal] The requested rate.

# File lib/money-fixer-io.rb, line 126
def fetch_rate(from, to, args = {})
  from = Money::Currency.wrap(from)
  to = Money::Currency.wrap(to)
  uri = build_uri(from, to, args)

  data = JSON.parse(uri.read)

  rate = data.fetch("rates").fetch(to.iso_code)
  rate = 1 / extract_rate(build_uri(to, from).read) if rate < 0.1
  rate
end