module Roda::RodaPlugins::Base::ResponseMethods

  1. lib/roda.rb

Instance methods for RodaResponse

Constants

CONTENT_LENGTH = "Content-Length".freeze  
CONTENT_TYPE = "Content-Type".freeze  
DEFAULT_HEADERS = {"Content-Type" => "text/html".freeze}.freeze  
LOCATION = "Location".freeze  

Attributes

body [R]

The body for the current response.

headers [R]

The hash of response headers for the current response.

status [RW]

The status code to use for the response. If none is given, will use 200 code for non-empty responses and a 404 code for empty responses.

Public Class methods

new ()

Set the default headers when creating a response.

[show source]
# File lib/roda.rb, line 974
def initialize
  @status  = nil
  @headers = {}
  @body    = []
  @length  = 0
end

Public Instance methods

[] (key)

Return the response header with the given key. Example:

response['Content-Type'] # => 'text/html'
[show source]
# File lib/roda.rb, line 984
def [](key)
  @headers[key]
end
[]= (key, value)

Set the response header with the given key to the given value.

response['Content-Type'] = 'application/json'
[show source]
# File lib/roda.rb, line 991
def []=(key, value)
  @headers[key] = value
end
default_headers ()

The default headers to use for responses.

[show source]
# File lib/roda.rb, line 996
def default_headers
  DEFAULT_HEADERS
end
default_status ()

Return the default response status to be used when the body has been written to. This is split out to make overriding easier in plugins.

[show source]
# File lib/roda.rb, line 1051
def default_status
  200
end
empty? ()

Whether the response body has been written to yet. Note that writing an empty string to the response body marks the response as not empty. Example:

response.empty? # => true
response.write('a')
response.empty? # => false
[show source]
# File lib/roda.rb, line 1007
def empty?
  @body.empty?
end
finish ()

Return the rack response array of status, headers, and body for the current response. If the status has not been set, uses the return value of #default_status if the body has been written to, otherwise uses a 404 status. Adds the Content-Length header to the size of the response body.

Example:

response.finish
#  => [200,
#      {'Content-Type'=>'text/html', 'Content-Length'=>'0'},
#      []]
[show source]
# File lib/roda.rb, line 1023
def finish
  b = @body
  empty = b.empty?
  s = (@status ||= empty ? 404 : default_status)
  set_default_headers
  h = @headers

  if empty && (s == 304 || s == 204 || s == 205 || (s >= 100 && s <= 199))
    h.delete("Content-Type")
  else
    h["Content-Length"] ||= @length.to_s
  end

  [s, h, b]
end
finish_with_body (body)

Return the rack response array using a given body. Assumes a 200 response status unless status has been explicitly set, and doesn't add the Content-Length header or use the existing body.

[show source]
# File lib/roda.rb, line 1043
def finish_with_body(body)
  set_default_headers
  [@status || default_status, @headers, body]
end
inspect ()

Show response class, status code, response headers, and response body

[show source]
# File lib/roda.rb, line 1056
def inspect
  "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>"
end
redirect (path, status = 302)

Set the Location header to the given path, and the status to the given status. Example:

response.redirect('foo', 301)
response.redirect('bar')
[show source]
# File lib/roda.rb, line 1065
def redirect(path, status = 302)
  @headers["Location"] = path
  @status  = status
  nil
end
roda_class ()

Return the Roda class related to this response.

[show source]
# File lib/roda.rb, line 1072
def roda_class
  self.class.roda_class
end
write (str)

Write to the response body. Returns nil.

response.write('foo')
[show source]
# File lib/roda.rb, line 1079
def write(str)
  s = str.to_s
  @length += s.bytesize
  @body << s
  nil
end