Sha256: be428fd77f7498f86af87f95d3383ad9db50b5b206bb0fd6cb787e1e044bd748

Contents?: true

Size: 991 Bytes

Versions: 1

Compression:

Stored size: 991 Bytes

Contents

module Pupa
  class Refinements
    # A refinement for the Faraday caching middleware to cache all requests, not
    # only GET requests.
    module FaradayMiddleware
      refine ::FaradayMiddleware::Caching do
        def call(env)
          # Remove if-statement to cache any request, not only GET.
          if env[:parallel_manager]
            # callback mode
            cache_on_complete(env)
          else
            # synchronous mode
            response = cache.fetch(cache_key(env)) { @app.call(env) }
            finalize_response(response, env)
          end
        end

        def cache_key(env)
          url = env[:url].dup
          if url.query && params_to_ignore.any?
            params = parse_query url.query
            params.reject! {|k,| params_to_ignore.include? k }
            url.query = build_query params
          end
          url.normalize!
          url.request_uri + env[:body].to_s # Add for POST requests.
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pupa-0.0.1 lib/pupa/refinements/faraday_middleware.rb