Class: RestClient::CacheableResource
- Inherits:
-
Resource
- Object
- Resource
- RestClient::CacheableResource
- Defined in:
- lib/cacheability/restclient.rb
Constant Summary collapse
- CACHE_DEFAULT_OPTIONS =
{:verbose => true}.freeze
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Follows the SPEC of Rack 1.0: the returned body is always an array of string.
- #debeautify_headers(headers = {}) ⇒ Object
- #get(additional_headers = {}, pass_through_cache = true) ⇒ Object
-
#initialize(*args) ⇒ CacheableResource
constructor
A new instance of CacheableResource.
Constructor Details
#initialize(*args) ⇒ CacheableResource
Returns a new instance of CacheableResource.
29 30 31 32 33 |
# File 'lib/cacheability/restclient.rb', line 29 def initialize(*args) super(*args) # rack-cache creates a singleton, so that there is only one instance of the cache at any time @cache = Rack::Cache.new(self, [:cache] || CACHE_DEFAULT_OPTIONS) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
26 27 28 |
# File 'lib/cacheability/restclient.rb', line 26 def cache @cache end |
Instance Method Details
#call(env) ⇒ Object
Follows the SPEC of Rack 1.0: the returned body is always an array of string
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cacheability/restclient.rb', line 76 def call(env) http_headers = env.inject({}) do |out, (header, value)| if header =~ /HTTP_/ out[header.gsub("HTTP_", '')] = value unless value.nil? || value.empty? end out end response = get(debeautify_headers(http_headers), pass_through_cache=false) response.headers.delete(:x_content_digest) # don't know why, but it seems to make the validation fail if kept... [response.code, debeautify_headers( response.headers ), [response.to_s]] rescue RestClient::NotModified => e # e is a Net::HTTPResponse response = RestClient::Response.new("", e.response) [304, debeautify_headers( response.headers ), [response.to_s]] end |
#debeautify_headers(headers = {}) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/cacheability/restclient.rb', line 67 def debeautify_headers(headers = {}) headers.inject({}) do |out, (key, value)| out[key.to_s.gsub(/_/, '-')] = value out end end |
#get(additional_headers = {}, pass_through_cache = true) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cacheability/restclient.rb', line 35 def get(additional_headers = {}, pass_through_cache = true) if pass_through_cache && cache uri = URI.parse(url) uri_path_split = uri.path.split("/") path_info = (last_part = uri_path_split.pop) ? "/"+last_part : "" script_name = uri_path_split.join("/") # minimal rack spec env = { "REQUEST_METHOD" => 'GET', "SCRIPT_NAME" => script_name, "PATH_INFO" => path_info, "QUERY_STRING" => uri.query, "SERVER_NAME" => uri.host, "SERVER_PORT" => uri.port.to_s, "rack.version" => Rack::VERSION, "rack.run_once" => false, "rack.multithread" => true, "rack.multiprocess" => true, "rack.url_scheme" => uri.scheme, "rack.input" => StringIO.new, "rack.errors" => StringIO.new # Rack-Cache writes errors into this field } debeautify_headers(additional_headers).each do |key, value| env.merge!("HTTP_"+key.to_s.gsub("-", "_").upcase => value) end response = MockHTTPResponse.new(cache.call(env)) RestClient::Response.new(response.body, response) else super(additional_headers) end end |