lib/billy/cache.rb in puffing-billy-0.2.3 vs lib/billy/cache.rb in puffing-billy-0.3.0

- old
+ new

@@ -1,12 +1,15 @@ require 'resolv' require 'uri' require 'yaml' require 'billy/json_utils' +require 'singleton' module Billy class Cache + include Singleton + attr_reader :scope def initialize reset end @@ -28,11 +31,11 @@ def fetch_from_persistence(key) begin @cache[key] = YAML.load(File.open(cache_file(key))) if persisted?(key) rescue ArgumentError => e - puts "Could not parse YAML: #{e.message}" + Billy.log :error, "Could not parse YAML: #{e.message}" nil end end def store(method, url, request_headers, body, response_headers, status, content) @@ -81,17 +84,30 @@ Billy.log(:info, "puffing-billy: CACHE KEY for '#{orig_url}#{body_msg}' is '#{key}'") if log_key key end - def format_url(url, ignore_params=false) + def format_url(url, ignore_params=false, dynamic_jsonp=Billy.config.dynamic_jsonp) url = URI(url) port_to_include = Billy.config.ignore_cache_port ? '' : ":#{url.port}" formatted_url = url.scheme+'://'+url.host+port_to_include+url.path - unless ignore_params - formatted_url += '?'+url.query if url.query - formatted_url += '#'+url.fragment if url.fragment + + return formatted_url if ignore_params + + if url.query + query_string = if dynamic_jsonp + query_hash = Rack::Utils.parse_query(url.query) + Billy.config.dynamic_jsonp_keys.each{|k| query_hash.delete(k) } + Rack::Utils.build_query(query_hash) + else + url.query + end + + formatted_url += "?#{query_string}" end + + formatted_url += '#'+url.fragment if url.fragment + formatted_url end def cache_file(key) File.join(Billy.config.cache_path, "#{key}.yml")