module Apilayer::Currency

Constants

CHANGE_SLUG
CONVERT_SLUG
HISTORICAL_SLUG
INVALID_OPTIONS_MSG
INVALID_TIMEFRAME_MSG
LIST_SLUG
LIVE_SLUG
TIMEFRAME_SLUG

Public Class Methods

add_options_to_params(opts, params) click to toggle source

Adds currencies and source to query string if they have been provided with options-hash

# File lib/apilayer/currency.rb, line 135
def self.add_options_to_params(opts, params)
  if opts[:currencies] && opts[:currencies].any?
    params[:currencies] = join_by_commas(opts[:currencies])
  end
  if opts[:source]
    params[:source] = opts[:source]
  end
  params
end
change(start_date=nil, end_date=nil, opts={}) click to toggle source

Api-Method: Calls the /change endpoint. start_date and end_date are optional, but can't provide one without the other :currencies and :source are optional Examples:

Apilayer::Currency.change
Apilayer::Currency.change("2016-01-01", "2016-03-01")
Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR")
Apilayer::Currency.change("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR", :currencies => %w[GBP CHF])
Apilayer::Currency.change(nil, nil, {:source => "EUR"})
Apilayer::Currency.change(nil, nil, {:currencies => %w[GBP CHF]})
Apilayer::Currency.change(nil, nil, {:source => "EUR", :currencies => %w[GBP CHF]})
# File lib/apilayer/currency.rb, line 117
def self.change(start_date=nil, end_date=nil, opts={})
  validate_options(opts)
  validate_timeframe_completeness(start_date,end_date)
  params = {:start_date => start_date, 
    :end_date => end_date
  }.reject{ |k,v| v.nil? }
  get_and_parse_with_options(CHANGE_SLUG, opts, params)
end
convert(from, to, amount, date=nil) click to toggle source

Api-Method: Calls the /convert endpoint, requires :from, :to and :amount When :date hasn't been passed, the latest available exchange rates will be used for your conversion. Examples:

Apilayer::Currency.convert("EUR", "CHF", 100)
Apilayer::Currency.convert("EUR", "CHF", 100, "2015-03-01")
# File lib/apilayer/currency.rb, line 83
def self.convert(from, to, amount, date=nil)
  params = {:from => from, :to => to, :amount => amount}
  params.merge!(:date => date) if date
  get_and_parse(CONVERT_SLUG, params)
end
get_and_parse_with_options(slug, opts, params={}) click to toggle source
# File lib/apilayer/currency.rb, line 128
def self.get_and_parse_with_options(slug, opts, params={})
  params = add_options_to_params(opts, params)
  get_and_parse(slug, params)
end
historical(date, opts={}) click to toggle source

Api-Method: Calls the /historical endpoint to get exchange rates for a specific date. When no currency-codes are specified, it will return all exchange rates for your source-currency. Examples:

Apilayer::Currency.historical("2016-01-01")
Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF])
Apilayer::Currency.historical(:source => "EUR")
Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF], :source => "EUR")
# File lib/apilayer/currency.rb, line 66
def self.historical(date, opts={})
  validate_options(opts)
  params = {:date => date}

  if opts.empty?
    get_and_parse(HISTORICAL_SLUG, params)
  else
    get_and_parse_with_options(HISTORICAL_SLUG, opts, params)
  end
end
join_by_commas(currencies) click to toggle source

Joins currencies in an array as a comma-separated-string

# File lib/apilayer/currency.rb, line 147
def self.join_by_commas(currencies)
  currencies.map(&:strip).join(",")
end
list() click to toggle source

Api-Method: Calls the /list endpoint to display all supported currencies

# File lib/apilayer/currency.rb, line 36
def self.list
  get_and_parse LIST_SLUG
end
live(opts={}) click to toggle source

Api-Method: Calls the /live endpoint to get real-time exchange rates. When no currency-codes are specified, it will return all exchange rates for your source-currency. Examples:

Apilayer::Currency.live
Apilayer::Currency.live(:currencies => %w[GBP, CHF])
Apilayer::Currency.live(:source => "EUR")
Apilayer::Currency.live(:source => "EUR", :currencies => %w[GBP, CHF])
# File lib/apilayer/currency.rb, line 48
def self.live(opts={})
  validate_options(opts)

  if opts.empty?
    get_and_parse LIVE_SLUG
  else
    get_and_parse_with_options(LIVE_SLUG, opts)
  end
end
timeframe(start_date, end_date, opts={}) click to toggle source

Api-Method: Calls the /timeframe endpoint. Requires :start_date and :end_date If :currencies hasn't been provided as an option, it will return all exchange-rates for that period of your source-currency :source can be provided as option to change the source-currency, which is USD by default The difference between start_date and end_date can be maximum 365 days Examples:

Apilayer::Currency.timeframe("2016-01-01", "2016-03-01")
Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF], :source => "EUR")
# File lib/apilayer/currency.rb, line 98
def self.timeframe(start_date, end_date, opts={})
  validate_options(opts)
  params = {:start_date => start_date, :end_date => end_date}
  get_and_parse_with_options(TIMEFRAME_SLUG, opts, params)
end
validate_options(options) click to toggle source

Validations

# File lib/apilayer/currency.rb, line 18
def self.validate_options(options)
  options.keys.each do |key|
    unless [:currencies, :source].include? key
      raise Apilayer::Error.new(INVALID_OPTIONS_MSG)
    end
  end
end
validate_timeframe_completeness(start_date,end_date) click to toggle source
# File lib/apilayer/currency.rb, line 26
def self.validate_timeframe_completeness(start_date,end_date)
  if [start_date,end_date].compact.size == 1
    raise Apilayer::Error.new(INVALID_TIMEFRAME_MSG)
  end
end