Sha256: 336d28e324b16f0764d2159879100db87fee8dd6f242d9db3cec5636d481a483

Contents?: true

Size: 854 Bytes

Versions: 6

Compression:

Stored size: 854 Bytes

Contents

# A refinement for the Faraday caching middleware to cache all requests, not
# only GET requests. Using Ruby's refinements doesn't seem to work, possibly
# because Faraday caches middlewares.
class FaradayMiddleware::Caching
  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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pupa-0.0.7 lib/pupa/refinements/faraday_middleware.rb
pupa-0.0.6 lib/pupa/refinements/faraday_middleware.rb
pupa-0.0.5 lib/pupa/refinements/faraday_middleware.rb
pupa-0.0.4 lib/pupa/refinements/faraday_middleware.rb
pupa-0.0.3 lib/pupa/refinements/faraday_middleware.rb
pupa-0.0.2 lib/pupa/refinements/faraday_middleware.rb