Merge pull request #100 from nilsding/correct-sort-order

Sort rates by the currency ISO code
This commit is contained in:
Hakan Ensari 2018-01-27 13:26:38 +00:00 committed by GitHub
commit 9d0d22e504
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -58,7 +58,7 @@ class Quote
end
def find_default_rates
Currency.where(date: date).reduce({}) do |rates, currency|
Currency.where(date: date).order(:iso_code).reduce({}) do |rates, currency|
rates.update(Hash[currency.to_h.map { |k, v| [k, round_rate(v * amount)] }])
end
end
@ -67,11 +67,10 @@ class Quote
rates = find_default_rates
denominator = rates.update(DEFAULT_BASE => amount).delete(base)
raise Invalid, 'Invalid base' unless denominator
rates.each do |iso_code, rate|
rates[iso_code] = round_rate(amount * rate / denominator)
end
rates
.map { |(iso_code, rate)| [iso_code, round_rate(amount * rate / denominator)] }
.sort_by(&:first)
.to_h
end
# I'm mimicking the apparent convention of the ECB here.

View File

@ -56,6 +56,12 @@ describe Quote do
rate.must_equal 1.4296
end
end
it 'returns the currencies sorted' do
stub_rates 'BAZ' => 1.5, 'FOO' => 2, 'BAR' => 3, 'QUX' => 4 do |quote|
quote.rates.keys.must_equal quote.rates.keys.sort
end
end
end
describe 'when given an invalid base' do