2024-02-10 16:18:56 -06:00
|
|
|
namespace :currencies do
|
|
|
|
desc "Seed Currencies"
|
|
|
|
task seed: :environment do
|
|
|
|
currencies = ENV["CURRENCIES"].split(",")
|
|
|
|
|
2024-03-04 10:26:20 -06:00
|
|
|
if currencies.count > 1 && ENV["SYNTH_API_KEY"].present?
|
|
|
|
url = "https://api.synthfinance.com/currencies"
|
2024-02-10 16:18:56 -06:00
|
|
|
|
|
|
|
response = Faraday.get(url) do |req|
|
2024-03-04 10:26:20 -06:00
|
|
|
req.headers["Authorization"] = "Bearer #{ENV["SYNTH_API_KEY"]}"
|
2024-02-10 16:18:56 -06:00
|
|
|
end
|
|
|
|
|
2024-03-04 10:26:20 -06:00
|
|
|
synth_currencies = JSON.parse(response.body)
|
2024-02-10 16:18:56 -06:00
|
|
|
|
|
|
|
currencies.each do |iso_code|
|
|
|
|
Currency.find_or_create_by(iso_code: iso_code) do |c|
|
2024-03-04 10:26:20 -06:00
|
|
|
c.name = synth_currencies["data"].find { |currency| currency["iso_code"] == iso_code.downcase }["name"]
|
2024-02-10 16:18:56 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Currencies created: #{Currency.count}"
|
|
|
|
elsif currencies.count == 1
|
|
|
|
Currency.find_or_create_by(iso_code: currencies.first)
|
|
|
|
else
|
|
|
|
puts "No currencies found in ENV['CURRENCIES']"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|