Class: FaradayMiddleware::Caching
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- FaradayMiddleware::Caching
- Extended by:
- Forwardable
- Defined in:
- lib/faraday_middleware/response/caching.rb
Overview
Public: Caches GET responses and pulls subsequent ones from the cache.
Constant Summary collapse
- CACHEABLE_STATUS_CODES =
Internal: List of status codes that can be cached:
-
200 - ‘OK’
-
203 - ‘Non-Authoritative Information’
-
300 - ‘Multiple Choices’
-
301 - ‘Moved Permanently’
-
302 - ‘Found’
-
404 - ‘Not Found’
-
410 - ‘Gone’
-
[200, 203, 300, 301, 302, 404, 410]
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
- #cache_key(env) ⇒ Object
- #cache_on_complete(env) ⇒ Object
- #call(env) ⇒ Object
- #finalize_response(response, env) ⇒ Object
-
#initialize(app, cache = nil, options = {}) ⇒ Caching
constructor
Public: initialize the middleware.
- #params_to_ignore ⇒ Object
Constructor Details
#initialize(app, cache = nil, options = {}) ⇒ Caching
Public: initialize the middleware.
cache - An object that responds to read, write and fetch (default: nil). options - An options Hash (default: {}):
:ignore_params - String name or Array names of query params
that should be ignored when forming the cache
key (default: []).
Yields if no cache is given. The block should return a cache object.
32 33 34 35 36 37 |
# File 'lib/faraday_middleware/response/caching.rb', line 32 def initialize(app, cache = nil, = {}) super(app) , cache = cache, nil if cache.is_a? Hash and block_given? @cache = cache || yield @options = end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
9 10 11 |
# File 'lib/faraday_middleware/response/caching.rb', line 9 def cache @cache end |
Instance Method Details
#cache_key(env) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/faraday_middleware/response/caching.rb', line 61 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 = params.any? ? build_query(params) : nil end url.normalize! url.request_uri end |
#cache_on_complete(env) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/faraday_middleware/response/caching.rb', line 76 def cache_on_complete(env) key = cache_key(env) if cached_response = cache.read(key) finalize_response(cached_response, env) else # response.status is nil at this point, any checks need to be done inside on_complete block @app.call(env).on_complete do |response_env| if CACHEABLE_STATUS_CODES.include?(response_env.status) cache.write(key, response_env.response) end response_env end end end |
#call(env) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/faraday_middleware/response/caching.rb', line 39 def call(env) if :get == env[:method] if env[:parallel_manager] # callback mode cache_on_complete(env) else # synchronous mode key = cache_key(env) unless response = cache.read(key) and response response = @app.call(env) if CACHEABLE_STATUS_CODES.include?(response.status) cache.write(key, response) end end finalize_response(response, env) end else @app.call(env) end end |
#finalize_response(response, env) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/faraday_middleware/response/caching.rb', line 91 def finalize_response(response, env) response = response.dup if response.frozen? env[:response] = response unless env[:response_headers] env.update response.env # FIXME: omg hax response.instance_variable_set('@env', env) end response end |
#params_to_ignore ⇒ Object
72 73 74 |
# File 'lib/faraday_middleware/response/caching.rb', line 72 def params_to_ignore @params_to_ignore ||= Array(@options[:ignore_params]).map { |p| p.to_s } end |