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 Enabled: false
Metrics/BlockLength: Metrics/BlockLength:
ExcludedMethods: ['describe', 'helpers'] ExcludedMethods: ['describe', 'helpers']
Metrics/AbcSize:
Max: 20.45
Metrics/MethodLength: Metrics/MethodLength:
Max: 11 Max: 13

View File

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

View File

@ -34,11 +34,9 @@ module Quote
end end
describe 'when given data' do describe 'when given data' do
let(:klass) do before do
Class.new(Base) do def quote.fetch_data
def fetch_data []
[]
end
end end
end end
@ -51,5 +49,26 @@ module Quote
refute quote.perform refute quote.perform
end end
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
end end