Class: Resourceful::CacheEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/resourceful/cache_manager.rb

Overview

Represents a previous request and cached response with enough detail to determine construct a cached response to a matching request in the future. It also understands what a matching request means.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ CacheEntry

Returns a new instance of CacheEntry.

Parameters:



204
205
206
207
208
209
# File 'lib/resourceful/cache_manager.rb', line 204

def initialize(request, response)
  @request_uri = request.uri
  @request_time = request.request_time
  @request_vary_headers = select_request_headers(request, response)
  @response = response
end

Instance Attribute Details

#request_timeObject (readonly)

The time at which the client believes the request was made.



192
193
194
# File 'lib/resourceful/cache_manager.rb', line 192

def request_time
  @request_time
end

#request_uriObject (readonly)

The URI of the request



195
196
197
# File 'lib/resourceful/cache_manager.rb', line 195

def request_uri
  @request_uri
end

#request_vary_headersObject (readonly)

request_vary_headers is a HttpHeader with keys from the Vary header of the response, plus the values from the matching fields in the request



189
190
191
# File 'lib/resourceful/cache_manager.rb', line 189

def request_vary_headers
  @request_vary_headers
end

#responseObject (readonly)

The response to that we are caching



198
199
200
# File 'lib/resourceful/cache_manager.rb', line 198

def response
  @response
end

Instance Method Details

#select_request_headers(request, response) ⇒ Object

Selects the headers from the request named by the response’s Vary header www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.6

Parameters:



228
229
230
231
232
233
234
235
236
# File 'lib/resourceful/cache_manager.rb', line 228

def select_request_headers(request, response)
  header = Resourceful::Header.new

  response.header['Vary'].each do |name|
    header[name] = request.header[name]
  end if response.header['Vary']

  header
end

#valid_for?(request) ⇒ Boolean

Returns true if this entry may be used to fullfil the given request, according to the vary headers.

Parameters:

Returns:

  • (Boolean)


216
217
218
219
# File 'lib/resourceful/cache_manager.rb', line 216

def valid_for?(request)
  request.uri == @request_uri and 
    @request_vary_headers.all? {|key, value| request.header[key] == value}
end