Class: RestClient::CacheableResource

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

Constant Summary collapse

CACHE_DEFAULT_OPTIONS =
{:verbose => false}

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#cacheObject (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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cacheability/restclient.rb', line 77

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



68
69
70
71
72
73
# File 'lib/cacheability/restclient.rb', line 68

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
66
# 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" => logger   # 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))
    env['rack.errors'].close if env['rack.errors'].kind_of?(File)
    RestClient::Response.new(response.body, response)
  else
    super(additional_headers)
  end
end

#loggerObject

Ugly, but waiting for RestClient to become smarter



95
96
97
98
99
100
101
102
103
# File 'lib/cacheability/restclient.rb', line 95

def logger
    return StringIO.new unless log_to = RestClient.log
    case log_to
    when 'stdout' then STDOUT
    when 'stderr' then STDERR
    else
      File.new(log_to, 'a')
    end
end