class Vici::ExchangeRates

Constants

BASE_URI
CURRENCIES
CURRENCY_REGEX
DATE_REGEX
VERSION

Public Class Methods

new(json = false) click to toggle source
# File lib/vici/exchange_rates.rb, line 16
def initialize(json = false)
  @client = Faraday.new BASE_URI
  @options = {}
  @endpoint = 'latest'
  @rates = []
  @date = nil
  @date_from = nil
  @date_to = nil
  @base = nil
  @return_json = json
end

Public Instance Methods

at_date(date) click to toggle source
# File lib/vici/exchange_rates.rb, line 35
def at_date(date)
  @date = validate_date(date)
  self
end
base_currency(code) click to toggle source
# File lib/vici/exchange_rates.rb, line 28
def base_currency(code)
  currency_code = sanitize_curreny_code(code)
  verify_currency_code(currency_code)
  @base = code
  self
end
fetch() click to toggle source
# File lib/vici/exchange_rates.rb, line 65
def fetch
  if @date_from.present? && @date_to.present?
    @endpoint = 'history'
    @options.merge!(start_at: @date_from, end_at: @date_to)
  elsif @date.present?
    @endpoint = @date
  else
    @endpoint
  end

  @options['base'] = @base if @base.present?

  @options['symbols'] = @rates.join(',') if @rates.length.positive?

  request = @client.get(@endpoint, @options)
  response = JSON.parse(request.body)

  if @return_json
    JSON.generate(response)
  else
    response
  end
end
period(date_from:, date_to:) click to toggle source
# File lib/vici/exchange_rates.rb, line 59
def period(date_from:, date_to:)
  @date_from = validate_date(date_from)
  @date_to = validate_date(date_to)
  self
end
rates(currencies) click to toggle source
# File lib/vici/exchange_rates.rb, line 40
def rates(currencies)
  if currencies.is_a?(Array)
    currencies.each do |currency|
      currency_code = sanitize_curreny_code(currency)

      verify_currency_code(currency_code)

      @rates << currency_code
    end
  else
    currency_code = sanitize_curreny_code(currencies)

    verify_currency_code(currency_code)

    @rates << currency_code
  end
  self
end

Private Instance Methods

currency_supported?(code) click to toggle source
# File lib/vici/exchange_rates.rb, line 114
def currency_supported?(code)
  CURRENCIES.include?(code)
end
currency_valid?(code) click to toggle source
# File lib/vici/exchange_rates.rb, line 103
def currency_valid?(code)
  if code.present?
    return false if code.length != 3

    return false unless CURRENCY_REGEX.match?(code)

    return true
  end
  false
end
date_valid?(date) click to toggle source
# File lib/vici/exchange_rates.rb, line 126
def date_valid?(date)
  return DATE_REGEX.match?(date) if date.present?

  false
end
sanitize_curreny_code(code) click to toggle source
# File lib/vici/exchange_rates.rb, line 132
def sanitize_curreny_code(code)
  code.strip.upcase
end
validate_date(date) click to toggle source
# File lib/vici/exchange_rates.rb, line 118
def validate_date(date)
  unless date_valid?(date)
    raise ArgumentError, 'The specified date is invalid. Please use ISO 8601 notation (e.g. YYYY-MM-DD).'
  end

  date
end
verify_currency_code(code) click to toggle source
# File lib/vici/exchange_rates.rb, line 91
def verify_currency_code(code)
  currency_code = sanitize_curreny_code(code)

  unless currency_valid?(currency_code)
    raise 'The specified currency code is invalid. Please use ISO 4217 notation (e.g. IDR).'
  end

  unless currency_supported?(currency_code)
    raise 'The specified currency code is not currently supported.'
  end
end