Class: Billy::CacheHandler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Handler
Defined in:
lib/billy/handlers/cache_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Handler

#reset

Constructor Details

#initializeCacheHandler

Returns a new instance of CacheHandler.



14
15
16
# File 'lib/billy/handlers/cache_handler.rb', line 14

def initialize
  @cache = Billy::Cache.instance
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/billy/handlers/cache_handler.rb', line 10

def cache
  @cache
end

Instance Method Details

#handle_request(method, url, headers, body) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/billy/handlers/cache_handler.rb', line 18

def handle_request(method, url, headers, body)
  method = method.downcase
  if handles_request?(method, url, headers, body)
    if (response = cache.fetch(method, url, body))
      Billy.log(:info, "puffing-billy: CACHE #{method} for '#{url}'")

      if Billy.config.dynamic_jsonp
        replace_response_callback(response, url)
      end

      if Billy.config.after_cache_handles_request
        request = { method: method, url: url, headers: headers, body: body }
        Billy.config.after_cache_handles_request.call(request, response)
      end

      if Billy.config.cache_simulates_network_delays
        Kernel.sleep(Billy.config.cache_simulates_network_delay_time)
      end

      return response
    end
  end
  nil
end

#handles_request?(method, url, _headers, body) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/billy/handlers/cache_handler.rb', line 43

def handles_request?(method, url, _headers, body)
  cached?(method, url, body)
end