class Faraday::CacheAdvanced

Constants

VERSION

Attributes

store[RW]

Public Class Methods

new(app, cache = nil, opts = {}) { || ... } click to toggle source
# File lib/faraday/cache-advanced/cache-advanced.rb, line 8
def initialize(app, cache = nil, opts = {})
  @app = app
  if(storename = opts.delete(:store))
    @store = lookup_store(storename)
  else
    @store = cache || yield
  end
end

Public Instance Methods

cache_key(env) click to toggle source
# File lib/faraday/cache-advanced/cache-advanced.rb, line 34
def cache_key(env)
  body = Hash(env[:body])
  body_string = body.keys.sort.map {|key| "#{key}=#{body[key]}"}.join("&")
  "#{env[:url]}/#{body_string}"
end
call(env) click to toggle source
# File lib/faraday/cache-advanced/cache-advanced.rb, line 17
def call(env)
  key = cache_key(env)
  response = store.read(key)

  if(response.nil? || env[:request_headers].delete(:must_revalidate))
    response = @app.call(env)
    store.write(key, response) if response.success?
  end

  env[:response] = response
  env.update response.env unless env[:response_headers]
  response.env[:method] = env[:method]
  response.env[:url] = env[:url]

  response
end
lookup_store(name, opts) click to toggle source
# File lib/faraday/cache-advanced/cache-advanced.rb, line 40
def lookup_store(name, opts)
  ActiveSupport::Cache.lookup_store(name.to_sym, opts)
end