Skip to content

Instantly share code, notes, and snippets.

@JoelBeasley
Created February 28, 2015 21:20
Show Gist options
  • Save JoelBeasley/393996095a01a8629605 to your computer and use it in GitHub Desktop.
Save JoelBeasley/393996095a01a8629605 to your computer and use it in GitHub Desktop.
Financial Update Service
class FinancialUpdateService
def initialize(stock)
@stock = stock
end
def run
set_last_updated_on
fetch_latest_financial_data
remove_duplicate_data
store_latest_financial_data
end
private
def set_last_updated_on
@last_updated_on = @stock.financials.first.date.to_date
end
def fetch_latest_financial_data
@latest_financial_data = StockDataApi.new(@stock.symbol, {start_date: @last_updated_on, end_date: Date.today-1}).financial_history
end
def remove_duplicate_data
@latest_financial_data.delete_if { |data| data[:date].to_date <= @last_updated_on}
end
def store_latest_financial_data
@latest_financial_data.each do |d|
@stock.financials.create(adj_close: d.fetch(:adj_close), close: d.fetch(:close),
date: d.fetch(:date).to_time, high: d.fetch(:high), low: d.fetch(:low), open: d.fetch(:open),
volume: d.fetch(:volume))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment