Sha256: 99509d27a8578e8aea79c00d5179313536582681fb0286be11cbb28043f5b6c6

Contents?: true

Size: 1.6 KB

Versions: 26

Compression:

Stored size: 1.6 KB

Contents

require 'billy/handlers/handler'
require 'addressable/uri'
require 'cgi'

module Billy
  class CacheHandler
    extend Forwardable
    include Handler

    attr_reader :cache

    def_delegators :cache, :reset, :cached?

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

    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

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

    private

    def replace_response_callback(response, url)
      request_uri = Addressable::URI.parse(url)
      if request_uri.query
        params = CGI.parse(request_uri.query)
        callback_name = Billy.config.dynamic_jsonp_callback_name
        if params[callback_name].any? && response[:content].match(/\w+\(/)
          response[:content].sub!(/\w+\(/, params[callback_name].first + '(')
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
puffing-billy-4.0.0 lib/billy/handlers/cache_handler.rb
puffing-billy-3.2.0 lib/billy/handlers/cache_handler.rb
puffing-billy-3.1.0 lib/billy/handlers/cache_handler.rb
puffing-billy-3.0.4 lib/billy/handlers/cache_handler.rb
puffing-billy-3.0.3 lib/billy/handlers/cache_handler.rb
puffing-billy-3.0.2 lib/billy/handlers/cache_handler.rb
puffing-billy-3.0.1 lib/billy/handlers/cache_handler.rb
puffing-billy-3.0.0 lib/billy/handlers/cache_handler.rb
puffing-billy-2.4.1 lib/billy/handlers/cache_handler.rb
puffing-billy-2.4.0 lib/billy/handlers/cache_handler.rb
puffing-billy-2.3.1 lib/billy/handlers/cache_handler.rb
puffing-billy-2.3.0 lib/billy/handlers/cache_handler.rb
puffing-billy-2.2.0 lib/billy/handlers/cache_handler.rb
puffing-billy-2.1.1 lib/billy/handlers/cache_handler.rb
puffing-billy-2.1.0 lib/billy/handlers/cache_handler.rb
puffing-billy-2.0.0 lib/billy/handlers/cache_handler.rb
puffing-billy-1.1.3 lib/billy/handlers/cache_handler.rb
puffing-billy-1.1.2 lib/billy/handlers/cache_handler.rb
puffing-billy-1.1.1 lib/billy/handlers/cache_handler.rb
puffing-billy-1.1.0 lib/billy/handlers/cache_handler.rb