1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-02 20:15:22 +02:00

Fetch exchange rates in bulk from synth (#1069)

* Fetch exchnage rates in bulk

* Handle paginated response

* Rename method and improve tests

* Change argument names

* Use standard date format
This commit is contained in:
Tony Vincent 2024-08-09 16:57:33 +02:00 committed by GitHub
parent f315370512
commit 6fa40e0fa2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 297 additions and 11 deletions

View file

@ -6,12 +6,31 @@ module ExchangeRate::Provided
class_methods do
private
def fetch_rates_from_provider(from:, to:, dates:, cache: false)
def fetch_rates_from_provider(from:, to:, start_date:, end_date: Date.current, cache: false)
return [] unless exchange_rates_provider.present?
dates.map do |date|
fetch_rate_from_provider from:, to:, date:, cache:
end.compact
response = exchange_rates_provider.fetch_exchange_rates \
from: from,
to: to,
start_date: start_date,
end_date: end_date
if response.success?
response.rates.map do |exchange_rate|
rate = ExchangeRate.new \
from_currency: from,
to_currency: to,
date: exchange_rate.dig(:date).to_date,
rate: exchange_rate.dig(:rate)
rate.save! if cache
rate
rescue ActiveRecord::RecordNotUnique
next
end
else
[]
end
end
def fetch_rate_from_provider(from:, to:, date:, cache: false)