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