aggregator/rss.rb
# Hit the feed for each feed URI at instantiation instead of
# at runtime, then write to file.
Hit the feed for each feed URI at instantiation instead of
at runtime, then write to file.
Hit the feed for each feed URI at instantiation instead of at runtime then write to file .
def init_cache
    @cache = {}
    get_feed_values = lambda {
      feed_length = @feeds.length
      feed_length.times do |_index|
        read
      end
    }
    overwrite_cache_file = lambda {
      File.open(Rss::CACHE_FILENAME, 'w') do |file|
        file.write(@cache.to_json)
      end
    }
    if File.exist?(Rss::CACHE_FILENAME)
      last_refresh = (Time.now - Rss::RSS_REFRESH_TIME_SECONDS).to_i
      next_refresh = (Time.now + Rss::RSS_REFRESH_TIME_SECONDS).to_i
      file_ctime   = File.mtime(Rss::CACHE_FILENAME).to_time.to_i
      cache_from_file = JSON.parse(File.read(Rss::CACHE_FILENAME))
      mismatched_keys = (cache_from_file.length != @feeds.length ||
                        cache_from_file.keys != @feeds.map(&:keys).flatten)
      old_cache = !(file_ctime > last_refresh && file_ctime < next_refresh)
      if old_cache || mismatched_keys
        File.delete(Rss::CACHE_FILENAME)
        get_feed_values.call
        overwrite_cache_file.call
      else
        @cache = cache_from_file
      end
    else
      get_feed_values.call
      overwrite_cache_file.call
    end
  end
def init_cache @cache = { } get_feed_values = lambda { feed_length = @feeds . length feed_length . times do | _index | read end } overwrite_cache_file = lambda { File . open ( Rss :: CACHE_FILENAME , 'w' ) do | file | file . write ( @cache . to_json ) end } if File . exist? ( Rss :: CACHE_FILENAME ) last_refresh = ( Time . now - Rss :: RSS_REFRESH_TIME_SECONDS ) . to_i next_refresh = ( Time . now + Rss :: RSS_REFRESH_TIME_SECONDS ) . to_i file_ctime = File . mtime ( Rss :: CACHE_FILENAME ) . to_time . to_i cache_from_file = JSON . parse ( File . read ( Rss :: CACHE_FILENAME ) ) mismatched_keys = ( cache_from_file . length != @feeds . length || cache_from_file . keys != @feeds . map ( & :keys ) . flatten ) old_cache = ! ( file_ctime > last_refresh && file_ctime < next_refresh ) if old_cache || mismatched_keys File . delete ( Rss :: CACHE_FILENAME ) get_feed_values . call overwrite_cache_file . call else @cache = cache_from_file end else get_feed_values . call overwrite_cache_file . call end end
Hit the feed for each feed URI at instantiation instead of
at runtime, then write to file.
Hit the feed for each feed URI at instantiation instead of at runtime then write to file .
# Attempt to read from file first # (over)write file if it doesn't exist, or is 1h+ old.