Class: FaradayStack::Caching
- Inherits:
- 
      Faraday::Middleware
      
        - Object
- Faraday::Middleware
- FaradayStack::Caching
 
- Defined in:
- lib/faraday_stack/caching.rb
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
  
  
  
  
  
  
  
    A new instance of Caching. 
- #params_to_strip ⇒ Object
Constructor Details
#initialize(app, cache = nil, options = {}) ⇒ Caching
Returns a new instance of Caching.
| 5 6 7 8 9 | # File 'lib/faraday_stack/caching.rb', line 5 def initialize(app, cache = nil, = {}) super(app) @cache = cache || Proc.new.call = end | 
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
| 3 4 5 | # File 'lib/faraday_stack/caching.rb', line 3 def cache @cache end | 
Instance Method Details
#cache_key(env) ⇒ Object
| 26 27 28 29 30 31 32 33 | # File 'lib/faraday_stack/caching.rb', line 26 def cache_key(env) url = env[:url].dup if url.query && params_to_strip.any? url.query_values = url.query_values.reject { |k,| params_to_strip.include? k } end url.normalize! url.request_uri end | 
#cache_on_complete(env) ⇒ Object
| 39 40 41 42 43 44 45 46 47 | # File 'lib/faraday_stack/caching.rb', line 39 def cache_on_complete(env) key = cache_key(env) if cached_response = cache.read(key) finalize_response(cached_response, env) else response = @app.call(env) response.on_complete { cache.write(key, response) } end end | 
#call(env) ⇒ Object
| 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # File 'lib/faraday_stack/caching.rb', line 11 def call(env) if :get == env[:method] 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 else @app.call(env) end end | 
#finalize_response(response, env) ⇒ Object
| 49 50 51 52 53 | # File 'lib/faraday_stack/caching.rb', line 49 def finalize_response(response, env) response = env[:response] = response.dup if response.frozen? response.apply_request env unless response.env[:method] response end | 
#params_to_strip ⇒ Object
| 35 36 37 | # File 'lib/faraday_stack/caching.rb', line 35 def params_to_strip @params_to_strip ||= Array([:strip_params]).map { |p| p.to_s } end |