module Roda::RodaPlugins::Base::ResponseMethods
def [](key)
Return the response header with the given key. Example:
def [](key) @headers[key] end
def []=(key, value)
Set the response header with the given key to the given value.
def []=(key, value) @headers[key] = value end
def _initialize_headers
def _initialize_headers Rack::Headers.new end
def _initialize_headers
def _initialize_headers {} end
def default_headers
def default_headers DEFAULT_HEADERS end
def default_status
has been written to. This is split out to make overriding
Return the default response status to be used when the body
def default_status 200 end
def empty?
response.write('a')
response.empty? # => true
the response as not empty. Example:
that writing an empty string to the response body marks
Whether the response body has been written to yet. Note
def empty? @body.empty? end
def empty_205_headers(headers)
Don't use a content length for empty 205 responses on
def empty_205_headers(headers) headers.delete(RodaResponseHeaders::CONTENT_TYPE) headers.delete(RodaResponseHeaders::CONTENT_LENGTH) end
def empty_205_headers(headers)
def empty_205_headers(headers) headers.delete(RodaResponseHeaders::CONTENT_TYPE) headers[RodaResponseHeaders::CONTENT_LENGTH] = '0' end
def finish
# {'Content-Type'=>'text/html', 'Content-Length'=>'0'},
# => [200,
response.finish
Example:
Adds the Content-Length header to the size of the response body.
been written to, otherwise uses a 404 status.
uses the return value of default_status if the body has
for the current response. If the status has not been set,
Return the rack response array of status, headers, and body
def finish b = @body set_default_headers h = @headers if b.empty? s = @status || 404 if (s == 304 || s == 204 || (s >= 100 && s <= 199)) h.delete(RodaResponseHeaders::CONTENT_TYPE) elsif s == 205 empty_205_headers(h) else h[RodaResponseHeaders::CONTENT_LENGTH] ||= '0' end else s = @status || default_status h[RodaResponseHeaders::CONTENT_LENGTH] ||= @length.to_s end [s, h, b] end
def finish_with_body(body)
and doesn't add the Content-Length header or use the existing
200 response status unless status has been explicitly set,
Return the rack response array using a given body. Assumes a
def finish_with_body(body) set_default_headers [@status || default_status, @headers, body] end
def initialize
def initialize @headers = _initialize_headers @body = [] @length = 0 end
def inspect
def inspect "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>" end
def redirect(path, status = 302)
response.redirect('foo', 301)
to the given status. Example:
Set the Location header to the given path, and the status
def redirect(path, status = 302) @headers[RodaResponseHeaders::LOCATION] = path @status = status nil end
def roda_class
def roda_class self.class.roda_class end
def set_default_headers
For each default header, if a header has not already been set for the
def set_default_headers h = @headers default_headers.each do |k,v| h[k] ||= v end end
def write(str)
Write to the response body. Returns nil.
def write(str) s = str.to_s @length += s.bytesize @body << s nil end