From 4df1d794c2493393d27784d482b6bb4c3dca5a3a Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Fri, 21 Sep 2018 20:20:56 +0100 Subject: [PATCH] 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 --- .rubocop.yml | 4 +++- lib/quote/base.rb | 21 ++++++++++----------- spec/quote/base_spec.rb | 29 ++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f92cf18..46540ed 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,5 +4,7 @@ Documentation: Enabled: false Metrics/BlockLength: ExcludedMethods: ['describe', 'helpers'] +Metrics/AbcSize: + Max: 20.45 Metrics/MethodLength: - Max: 11 + Max: 13 diff --git a/lib/quote/base.rb b/lib/quote/base.rb index cf78abf..d178369 100644 --- a/lib/quote/base.rb +++ b/lib/quote/base.rb @@ -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 diff --git a/spec/quote/base_spec.rb b/spec/quote/base_spec.rb index 249d7a0..039b1d2 100644 --- a/spec/quote/base_spec.rb +++ b/spec/quote/base_spec.rb @@ -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