Class: RestClient::CacheableResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/cacheability/restclient.rb

Constant Summary collapse

CACHE_DEFAULT_OPTIONS =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CacheableResource



25
26
27
28
29
# File 'lib/cacheability/restclient.rb', line 25

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, options[:cache] || CACHE_DEFAULT_OPTIONS)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



22
23
24
# File 'lib/cacheability/restclient.rb', line 22

def cache
  @cache
end

Instance Method Details

#call(env) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cacheability/restclient.rb', line 70

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
  [304, e.response.to_hash, ""]
end

#debeautify_headers(headers = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/cacheability/restclient.rb', line 63

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



31
32
33
34
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
# File 'lib/cacheability/restclient.rb', line 31

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