aggregator/stock.rb
# Hit the API for each symbol at instantiation instead
# of at runtime, then write to file.
Hit the API for each symbol at instantiation instead
of at runtime, then write to file.
Hit the API for each symbol at instantiation instead of at runtime then write to file .
def init_cache
    @cache = {}
    get_symbol_values = lambda {
      symbol_length = @symbols.length
      symbol_length.times do |index|
        if symbol_length > 5 && index > 4 
          Util.wait(STOCK_TIMEOUT) { quote_missing_symbol(@symbols[index]) }
        else
          read
        end
      end
    }
    overwrite_cache_file = lambda {
      File.open(Stock::CACHE_FILENAME, 'w') do |file|
        file.write(@cache.to_json)
      end
    }
    if File.exist?(Stock::CACHE_FILENAME)
      yesterday  = (Date.today - 1).to_time.to_i
      tomorrow   = (Date.today + 1).to_time.to_i
      file_ctime = File.mtime(Stock::CACHE_FILENAME).to_date.to_time.to_i
      cache_from_file = JSON.parse(File.read(Stock::CACHE_FILENAME))
      mismatched_keys = (cache_from_file.keys.length != @symbols.length) ||
                        cache_from_file.keys != @symbols
      old_cache = !(file_ctime > yesterday && file_ctime < tomorrow)
      if old_cache || mismatched_keys
        File.delete(Stock::CACHE_FILENAME)
        get_symbol_values.call
        overwrite_cache_file.call
      else
        @cache = cache_from_file
      end
    else
      get_symbol_values.call
      overwrite_cache_file.call
    end
  end
def init_cache @cache = { } get_symbol_values = lambda { symbol_length = @symbols . length symbol_length . times do | index | if symbol_length > 5 && index > 4 Util . wait ( STOCK_TIMEOUT ) { quote_missing_symbol ( @symbols [ index ] ) } else read end end } overwrite_cache_file = lambda { File . open ( Stock :: CACHE_FILENAME , 'w' ) do | file | file . write ( @cache . to_json ) end } if File . exist? ( Stock :: CACHE_FILENAME ) yesterday = ( Date . today - 1 ) . to_time . to_i tomorrow = ( Date . today + 1 ) . to_time . to_i file_ctime = File . mtime ( Stock :: CACHE_FILENAME ) . to_date . to_time . to_i cache_from_file = JSON . parse ( File . read ( Stock :: CACHE_FILENAME ) ) mismatched_keys = ( cache_from_file . keys . length != @symbols . length ) || cache_from_file . keys != @symbols old_cache = ! ( file_ctime > yesterday && file_ctime < tomorrow ) if old_cache || mismatched_keys File . delete ( Stock :: CACHE_FILENAME ) get_symbol_values . call overwrite_cache_file . call else @cache = cache_from_file end else get_symbol_values . call overwrite_cache_file . call end end
Hit the API for each symbol at instantiation instead
of at runtime, then write to file.
Hit the API for each symbol at instantiation instead of at runtime then write to file .
# API limits -- if you are tracking more than 5 symbols, I'll have to add a delay # TODO: Handle this in batches, so we can support more than 10 symbols, staggered # TODO: this fires even if we have it in a cached file. Not the end of the world, but optimally we want to redo this... #       works for now. # Attempt to read from file first # (over)write file if it doesn't exist, or is 24h+ old.