class Uktt::MonetaryExchangeRate

A MonetaryExchangeRate object for dealing with an API resource

Attributes

config[RW]
monetary_exchange_rate_id[RW]
response[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/uktt/monetary_exchange_rate.rb, line 6
def initialize(opts = {})
  @monetary_exchange_rate_id = opts[:monetary_exchange_rate_id] || nil
  Uktt.configure(opts)
  @config = Uktt.config
end

Public Instance Methods

config=(new_opts = {}) click to toggle source
# File lib/uktt/monetary_exchange_rate.rb, line 33
def config=(new_opts = {})
  merged_opts = Uktt.config.merge(new_opts)
  Uktt.configure merged_opts
  @monetary_exchange_rate_id = merged_opts[:monetary_exchange_rate_id] || @monetary_exchange_rate_id
  @config = Uktt.config
end
latest(currency) click to toggle source
# File lib/uktt/monetary_exchange_rate.rb, line 16
def latest(currency)
  retrieve_all unless @response

  case @config[:version]
  when 'v1'
    @response.select{ |obj| obj.child_monetary_unit_code == currency.upcase }
             .sort_by(&:validity_start_date)
             .last.exchange_rate.to_f
  when 'v2'
    @response.data.select{ |obj| obj.attributes.child_monetary_unit_code == currency.upcase }
             .sort_by{ |obj| obj.attributes.validity_start_date }
             .last.attributes.exchange_rate.to_f
  else
    raise StandardError.new "`#{@opts[:version]}` is not a supported API version. Supported API versions are: v1 and v2"
  end
end
retrieve_all() click to toggle source
# File lib/uktt/monetary_exchange_rate.rb, line 12
def retrieve_all
  fetch "#{M_X_RATE}.json"
end

Private Instance Methods

fetch(resource) click to toggle source
# File lib/uktt/monetary_exchange_rate.rb, line 42
def fetch(resource)
  @response = Uktt::Http.new(
    @config[:host],
    @config[:version],
    @config[:debug]
  ).retrieve(
    resource,
    @config[:return_json]
  )
end