Class: Rack::Cache::Response

Inherits:
Object
  • Object
show all
Includes:
Headers, ResponseHeaders, Response::Helpers
Defined in:
lib/rack/cache/response.rb

Overview

Provides access to the response generated by the downstream application. The response, original_response, and entry objects exposed by the Core caching engine are instances of this class.

Response objects respond to a variety of convenience methods, including those defined in Rack::Response::Helpers, Rack::Cache::Headers, and Rack::Cache::ResponseHeaders.

Note that Rack::Cache::Response is not a subclass of Rack::Response and does not perform many of the same initialization and finalization tasks. For example, the body is not slurped during initialization and there are no facilities for generating response output.

Constant Summary

Constants included from ResponseHeaders

Rack::Cache::ResponseHeaders::CACHEABLE_RESPONSE_CODES, Rack::Cache::ResponseHeaders::NOT_MODIFIED_OMIT_HEADERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResponseHeaders

#age, #cacheable?, #client_ttl=, #date, #etag_matches?, #expires_at, #fresh?, #freshness_information?, #last_modified, #last_modified_at?, #max_age, #max_age=, #must_revalidate?, #no_cache?, #no_store?, #not_modified!, #private=, #private?, #public=, #public?, #shared_max_age=, #stale?, #ttl, #ttl=, #validateable?, #vary, #vary?, #vary_header_names

Methods included from Headers

#cache_control, #cache_control=, #etag, #header?

Constructor Details

#initialize(status, headers, body) ⇒ Response

Create a Response instance given the response status code, header hash, and body.



35
36
37
38
39
40
41
# File 'lib/rack/cache/response.rb', line 35

def initialize(status, headers, body)
  @status = status
  @headers = Rack::Utils::HeaderHash.new(headers)
  @body = body
  @now = Time.now
  @headers['Date'] ||= now.httpdate
end

Instance Attribute Details

#bodyObject

The response body. See the Rack spec for information on the behavior required by this object.



28
29
30
# File 'lib/rack/cache/response.rb', line 28

def body
  @body
end

#headersObject (readonly)

The response headers.



31
32
33
# File 'lib/rack/cache/response.rb', line 31

def headers
  @headers
end

#statusObject

The response’s status code (integer).



24
25
26
# File 'lib/rack/cache/response.rb', line 24

def status
  @status
end

Instance Method Details

#[](header_name) ⇒ Object

Return the value of the named response header.



49
50
51
# File 'lib/rack/cache/response.rb', line 49

def [](header_name)
  headers[header_name]
end

#[]=(header_name, header_value) ⇒ Object

Set a response header value.



54
55
56
# File 'lib/rack/cache/response.rb', line 54

def []=(header_name, header_value)
  headers[header_name] = header_value
end

#activate!Object

Called immediately after an object is loaded from the cache.



59
60
61
# File 'lib/rack/cache/response.rb', line 59

def activate!
  headers['Age'] = age.to_i.to_s
end

#freezeObject

Freezes



69
70
71
72
# File 'lib/rack/cache/response.rb', line 69

def freeze
  @headers.freeze
  super
end

#initialize_copy(other) ⇒ Object



43
44
45
46
# File 'lib/rack/cache/response.rb', line 43

def initialize_copy(other)
  super
  @headers = other.headers.dup
end

#to_aObject

Return the status, headers, and body in a three-tuple.



64
65
66
# File 'lib/rack/cache/response.rb', line 64

def to_a
  [status, headers.to_hash, body]
end