Sha256: e0ab70ff5735973250012a579e853cb0987135446b26f0c6b103837e53d6afe3

Contents?: true

Size: 1.71 KB

Versions: 64

Compression:

Stored size: 1.71 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(environment)
          return @app.call(environment) unless [:get, :head].include?(environment[:method])

          # send known etag
          cached = @cache.read(cache_key(environment))

          if cached
            environment[:request_headers]["If-None-Match"] ||= cached[:response_headers]["Etag"]
          end

          @app.call(environment).on_complete do |env|
            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.to_hash)
            end
          end
        end
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
zendesk_api-1.7.1 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.7.0 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.6.3 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.6.2 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.6.1 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.6.0 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.5.1 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.5.0 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.4.6 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.4.5 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.4.4 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.4.3 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.4.2 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.4.0 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.3.9 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.3.8 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.3.7 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.3.5 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.3.4 lib/zendesk_api/middleware/request/etag_cache.rb
zendesk_api-1.3.2 lib/zendesk_api/middleware/request/etag_cache.rb