class Fxer::Exchange::Data
Attributes
Public Class Methods
initalize instantiates dates as an array to contain its Date
objects.
# File lib/fxer/exchange/data.rb, line 14 def initialize(opts = {}) @options = opts @dates = [] end
Public Instance Methods
at, from and to are convenience methods to set the transaction date, base currency, and counter currency, respectively. They return self so that they can be chained. arguments are defined in Fxer::Exchange
# File lib/fxer/exchange/data.rb, line 45 def at(d) @transaction_date = d self end
rates sifts the available data for the nearest date, then sifts through the currencies to find the base and counter rates.
Note @transaction_date, @to, @from must be set before `rates` is called. Convenient methods exist for this purpose. For instance:
rate_set = Fxer::Exchange::DataSource::Ecb.new(options).rate_sets rates = rate_set.at("2017-07-21").from("USD").to("GBP").rates
rates returns an array of two Currency objects, [counter, base].
# File lib/fxer/exchange/data.rb, line 31 def rates date = nearest_date [ date.currencies.find{ |d| d.key == @to }, date.currencies.find{ |d| d.key == @from } ] end
transaction_date
takes one argument, defined in Fxer::Exchange
and normalizes it to a Date
object
# File lib/fxer/exchange/data.rb, line 62 def transaction_date=(raw_date) @transaction_date = Date.normalize_date(d) end
Private Instance Methods
nearest_date
chooses the date object matching the transaction date.
If no date is matched, the next date can be chosen if the permissive flag evaluates to true. Otherwise it raises an error.
# File lib/fxer/exchange/data.rb, line 74 def nearest_date d = @dates.find { |d| "#{d.date}" == "#{@transaction_date}" } return d if d raise "#{DATE_ERROR_MESSAGE} #{@transaction_date}" unless @options[:permissive] dates.sort_by{ |d| d.date }.reverse.first end