1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 07:39:39 +02:00
Maybe/lib/tasks/exchange_rates.rake
Josh Pigford 4843cf22c6
Use Synth for exchange rates (#514)
* Switch currency seeding over to Synth

* Switch all exchange rates over to Synth
2024-03-04 10:26:20 -06:00

28 lines
1.1 KiB
Ruby

namespace :exchange_rates do
desc "Fetch exchange rates from Synth API"
task sync: :environment do
Currency.all.each do |currency|
(Date.today - 30.days).upto(Date.today) do |date|
response = Faraday.get("https://api.synthfinance.com/rates/historical") do |req|
req.headers["Authorization"] = "Bearer #{ENV["SYNTH_API_KEY"]}"
req.params["date"] = date.to_s
req.params["from"] = currency.iso_code
req.params["to"] = Currency.where.not(iso_code: currency.iso_code).pluck(:iso_code).join(",")
end
if response.success?
rates = JSON.parse(response.body)["data"]["rates"]
rates.each do |currency_iso_code, value|
ExchangeRate.find_or_create_by(date: date, base_currency: currency.iso_code, converted_currency: currency_iso_code) do |exchange_rate|
exchange_rate.rate = value
end
puts "#{currency.iso_code} to #{currency_iso_code} on #{date}: #{value}"
end
else
puts "Failed to fetch exchange rates for #{currency.iso_code} on #{date}: #{response.status}"
end
end
end
end
end