mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-22 02:52:49 +01:00
Merge pull request #100 from nilsding/correct-sort-order
Sort rates by the currency ISO code
This commit is contained in:
commit
9d0d22e504
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user