2024-08-01 19:43:23 -04:00
|
|
|
module Security::Price::Provided
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
include Providable
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
private
|
|
|
|
|
2024-10-29 15:37:59 -04:00
|
|
|
def fetch_price_from_provider(security:, date:, cache: false)
|
2024-08-01 19:43:23 -04:00
|
|
|
return nil unless security_prices_provider.present?
|
|
|
|
|
|
|
|
response = security_prices_provider.fetch_security_prices \
|
2024-10-29 15:37:59 -04:00
|
|
|
ticker: security.ticker,
|
|
|
|
mic_code: security.exchange_mic,
|
2024-08-01 19:43:23 -04:00
|
|
|
start_date: date,
|
|
|
|
end_date: date
|
|
|
|
|
|
|
|
if response.success? && response.prices.size > 0
|
|
|
|
price = Security::Price.new \
|
2024-10-29 15:37:59 -04:00
|
|
|
security: security,
|
2024-08-01 19:43:23 -04:00
|
|
|
date: response.prices.first[:date],
|
|
|
|
price: response.prices.first[:price],
|
|
|
|
currency: response.prices.first[:currency]
|
|
|
|
|
|
|
|
price.save! if cache
|
|
|
|
price
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-29 15:37:59 -04:00
|
|
|
def fetch_prices_from_provider(security:, start_date:, end_date:, cache: false)
|
2024-08-01 19:43:23 -04:00
|
|
|
return [] unless security_prices_provider.present?
|
2024-10-29 15:37:59 -04:00
|
|
|
return [] unless security
|
2024-08-01 19:43:23 -04:00
|
|
|
|
|
|
|
response = security_prices_provider.fetch_security_prices \
|
2024-10-29 15:37:59 -04:00
|
|
|
ticker: security.ticker,
|
|
|
|
mic_code: security.exchange_mic,
|
2024-08-01 19:43:23 -04:00
|
|
|
start_date: start_date,
|
|
|
|
end_date: end_date
|
|
|
|
|
|
|
|
if response.success?
|
|
|
|
response.prices.map do |price|
|
2024-10-10 18:02:12 -04:00
|
|
|
new_price = Security::Price.find_or_initialize_by(
|
2024-10-29 15:37:59 -04:00
|
|
|
security: security,
|
2024-10-10 18:02:12 -04:00
|
|
|
date: price[:date]
|
|
|
|
) do |p|
|
|
|
|
p.price = price[:price]
|
|
|
|
p.currency = price[:currency]
|
|
|
|
end
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2024-10-10 18:02:12 -04:00
|
|
|
new_price.save! if cache && new_price.new_record?
|
2024-08-01 19:43:23 -04:00
|
|
|
new_price
|
|
|
|
end
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|