1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-25 08:09:38 +02:00

Stock imports (#1363)

* Initial pass

* Marketstack data provider

* Marketstack data provider

* Refactor a bit
This commit is contained in:
Josh Pigford 2024-10-24 16:36:50 -05:00 committed by GitHub
parent b611dfdf37
commit aa3342b0dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 187 additions and 1 deletions

View file

@ -0,0 +1,27 @@
class Security::Importer
def initialize(provider, stock_exchange = nil)
@provider = provider
@stock_exchange = stock_exchange
end
def import
securities = @provider.fetch_tickers(exchange_mic: @stock_exchange)&.tickers
stock_exchanges = StockExchange.where(mic: securities.map { |s| s[:exchange] }).index_by(&:mic)
existing_securities = Security.where(ticker: securities.map { |s| s[:symbol] }, stock_exchange_id: stock_exchanges.values.map(&:id)).pluck(:ticker, :stock_exchange_id).to_set
securities_to_create = securities.map do |security|
stock_exchange_id = stock_exchanges[security[:exchange]]&.id
next if existing_securities.include?([ security[:symbol], stock_exchange_id ])
{
name: security[:name],
ticker: security[:symbol],
stock_exchange_id: stock_exchange_id,
country_code: security[:country_code]
}
end.compact
Security.insert_all(securities_to_create) unless securities_to_create.empty?
end
end