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:
parent
b611dfdf37
commit
aa3342b0dc
10 changed files with 187 additions and 1 deletions
27
app/models/security/importer.rb
Normal file
27
app/models/security/importer.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue