Sha256: b6e6d3d69a4c7cce5845aa00a0eab4b1a8d7efaaa03015321cc6a5dab2a470d1
Contents?: true
Size: 1.64 KB
Versions: 29
Compression:
Stored size: 1.64 KB
Contents
require "faraday/middleware" module ZendeskAPI module Middleware module Request # Request middleware that caches responses based on etags # can be removed once this is merged: https://github.com/pengwynn/faraday_middleware/pull/42 # @private class EtagCache < Faraday::Middleware def initialize(app, options = {}) @app = app @cache = options[:cache] || raise("need :cache option e.g. ActiveSupport::Cache::MemoryStore.new") @cache_key_prefix = options.fetch(:cache_key_prefix, :faraday_etags) end def cache_key(env) [@cache_key_prefix, env[:url].to_s] end def call(env) return @app.call(env) unless [:get, :head].include?(env[:method]) # send known etag cached = @cache.read(cache_key(env)) if cached env[:request_headers]["If-None-Match"] ||= cached[:response_headers]["Etag"] end @app.call(env).on_complete do if cached && env[:status] == 304 # not modified env[:body] = cached[:body] env[:response_headers].merge!( :etag => cached[:response_headers][:etag], :content_type => cached[:response_headers][:content_type], :content_length => cached[:response_headers][:content_length], :content_encoding => cached[:response_headers][:content_encoding] ) elsif env[:status] == 200 && env[:response_headers]["Etag"] # modified and cacheable @cache.write(cache_key(env), env) end end end end end end end
Version data entries
29 entries across 29 versions & 1 rubygems