frankfurter/lib/converter.rb
Vincent Durand 46dbb66b0f Implement Converter
The API is fairly simple in order to get straight to the goal, convert
an amount from a currency to another.
I used the proposal from #25 which is similar to the google currency
tool.
2016-09-22 18:51:27 +02:00

33 lines
658 B
Ruby

# frozen_string_literal: true
require 'bigdecimal'
require 'quote'
class Converter
attr_reader :to, :from
def initialize(params = {})
@amount = params[:amount]
@from = params[:from]
@to = params[:to]
end
# Converts an amount into a new currency
# @param quote [Quote] the quote object
# @return amount [BigDecimal] the converted amount
def convert(quote = latest_quote)
return amount if quote.base == to
amount * quote.rates.fetch(to) { raise Quote::Invalid, 'Invalid to' }
end
def amount
BigDecimal(@amount.to_s)
end
private
def latest_quote
@latest_quote ||= Quote.new(base: from)
end
end