module HTTParty::DryIce
Caching for HTTParty
¶ ↑
See documentation in HTTParty::Icebox::ClassMethods.cache
Public Class Methods
cache_response?(response)
click to toggle source
returns falsy if the response should not be cached - otherwise returns the timeout in seconds to cache for
# File lib/dry_ice.rb, line 84 def self.cache_response?(response) return false if !response.body return false unless response.code.to_s == "200" timeout = response.headers['cache-control'] && response.headers['cache-control'][/max-age=(\d+)/, 1].to_i() return false unless timeout && timeout != 0 return timeout end
get(path, options={})
click to toggle source
Redefine original HTTParty
get
method to use cache
# File lib/dry_ice.rb, line 94 def self.get(path, options={}) self.get_with_caching(path, options) end
get_with_caching(path, options={})
click to toggle source
Get response from cache, if available
# File lib/dry_ice.rb, line 66 def self.get_with_caching(path, options={}) return get_without_caching(path, options) unless get_cache key = path.downcase # this makes a copy of path key << options[:query].to_s if defined? options[:query] if res = get_cache.read(key) return res else response = get_without_caching(path, options) if cache_for = self.cache_response?(response) get_cache.write(key,response, :expires_in => cache_for) return response else return response end end end
get_without_caching(path, options={})
click to toggle source
Get reponse from network
TODO: Why alias :new :old is not working here? Returns NoMethodError
# File lib/dry_ice.rb, line 60 def self.get_without_caching(path, options={}) perform_request Net::HTTP::Get, path, options end