Sha256: 2d53524485a799a9a76eedbff71013270c4be00e7bd1725f0dcd0643781f8b40

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'reviewed/cache'

module Faraday
  class Cache

    def initialize(app)
      @app = app
    end

    def store
      Reviewed::Cache.store
    end

    def call(env)
      @url = env[:url]

      @website_id = env[:request_headers]['x-reviewed-website']

      if serve_from_cache? && store.exist?(cache_key)
        begin
          Hashie::Mash.new(Marshal.load( store.read(cache_key) ))
        rescue => e
          raise e.message + ": #{cache_key}"
        end
      else
        @app.call(env).on_complete do |response|
          if store_response?(response)
            store.delete(cache_key)
            store.write(cache_key, Marshal.dump(response), write_options)
          end
        end
      end
    end

    private

    def serve_from_cache?
      @url.query.blank? || !@url.query.match(/\b(skip|reset)-cache\b/)
    end

    def store_response?(resp)
      return false if resp[:status] != 200
      @url.query.blank? || !@url.query.match(/\bskip-cache\b/)
    end

    def cache_key
      [@website_id, @url.request_uri].join(':')
    end

    def write_options
      { expires_in: Integer(ENV['REVIEWED_CACHE_TIMEOUT'] || 90).minutes }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reviewed-0.6.4 lib/faraday/cache.rb