class Response
A basic content response, including useful defaults for typical HTML5 content.
Definitions
def initialize
Initialize an empty successful HTML response.
Implementation
def initialize
@status = 200
@headers = {}
@body = []
# The default content type:
self.content_type = "text/html; charset=utf-8"
end
def content
Join the response body into rendered content.
Signature
-
returns
String The rendered content.
Implementation
def content
@body.join
end
def lookup(tag)
Decline tag lookup by default.
Signature
-
parameter
tagObject The tag.
-
returns
Nil No tag is resolved.
Implementation
def lookup(tag)
return nil
end
def to_a
Convert this value to a Rack response tuple.
Signature
-
returns
Array The status, headers, and body.
Implementation
def to_a
[@status, @headers, @body]
end
def do_not_cache!
Specifies that the content shouldn't be cached. Overrides cache! if already called.
Implementation
def do_not_cache!
@headers[CACHE_CONTROL] = "no-cache, must-revalidate"
@headers[EXPIRES] = Time.now.httpdate
end
def cache!(duration = 3600, access: "public")
Specify that the content could be cached.
Implementation
def cache!(duration = 3600, access: "public")
unless cache_control = @headers[CACHE_CONTROL] and cache_control.include?(NO_CACHE)
@headers[CACHE_CONTROL] = "#{access}, max-age=#{duration}"
@headers[EXPIRES] = (Time.now + duration).httpdate
end
end
def content_type=(value)
Specify the content type of the response data.
Implementation
def content_type= value
@headers[CONTENT_TYPE] = value
end