Don't return empty hashes

This happened when rebasing and converting to unavailable currencies. For instance, the following was returning empty hashes for dates prior to the release of the new Turkish Lira:

https://frankfurter.app/1999-01-01..?from=USD&to=TRY
This commit is contained in:
Hakan Ensari 2018-09-21 20:20:56 +01:00
parent 82a5c59cb6
commit 4df1d794c2
3 changed files with 37 additions and 17 deletions

View File

@ -4,5 +4,7 @@ Documentation:
Enabled: false
Metrics/BlockLength:
ExcludedMethods: ['describe', 'helpers']
Metrics/AbcSize:
Max: 20.45
Metrics/MethodLength:
Max: 11
Max: 13

View File

@ -64,19 +64,18 @@ module Quote
def rebase_rates
result.each do |date, rates|
add_euro(rates)
rates['EUR'] = amount if symbols.nil? || symbols.include?('EUR')
divisor = rates.delete(base)
result[date] = rates.sort
.map! do |iso_code, rate|
[iso_code, round(amount * rate / divisor)]
end
.to_h
if rates.empty?
result.delete(date)
else
result[date] = rates.sort
.map! do |iso_code, rate|
[iso_code, round(amount * rate / divisor)]
end
.to_h
end
end
end
def add_euro(rates)
rates['EUR'] = amount if symbols.nil? || symbols.include?('EUR')
end
end
end

View File

@ -34,11 +34,9 @@ module Quote
end
describe 'when given data' do
let(:klass) do
Class.new(Base) do
def fetch_data
[]
end
before do
def quote.fetch_data
[]
end
end
@ -51,5 +49,26 @@ module Quote
refute quote.perform
end
end
describe 'when rebasing and converting to an unavailable currency' do
let(:date) do
Date.today
end
let(:quote) do
klass.new(date: date, base: 'USD', symbols: ['FOO'])
end
before do
def quote.fetch_data
[{ date: date, iso_code: 'USD', rate: 1.2 }]
end
end
it 'finds nothing' do
quote.perform
quote.not_found?.must_equal true
end
end
end
end