module Roda::RodaPlugins::Base::ResponseMethods

  1. lib/roda/response.rb

Instance methods for RodaResponse

Constants

DEFAULT_HEADERS = {"Content-Type" => "text/html".freeze}.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/response.rb
41 def initialize
42   @headers = {}
43   @body    = []
44   @length  = 0
45 end

Public Instance methods

[](key)

Return the response header with the given key. Example:

response['Content-Type'] # => 'text/html'
[show source]
   # File lib/roda/response.rb
50 def [](key)
51   @headers[key]
52 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/response.rb
57 def []=(key, value)
58   @headers[key] = value
59 end
default_headers()

The default headers to use for responses.

[show source]
   # File lib/roda/response.rb
62 def default_headers
63   DEFAULT_HEADERS
64 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/response.rb
124 def default_status
125   200
126 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/response.rb
73 def empty?
74   @body.empty?
75 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/response.rb
 89 def finish
 90   b = @body
 91   set_default_headers
 92   h = @headers
 93 
 94   if b.empty?
 95     s = @status || 404
 96     if (s == 304 || s == 204 || (s >= 100 && s <= 199))
 97       h.delete("Content-Type")
 98     elsif s == 205
 99       h.delete("Content-Type")
100       h["Content-Length"] = '0'
101     else
102       h["Content-Length"] ||= '0'
103     end
104   else
105     s = @status || default_status
106     h["Content-Length"] ||= @length.to_s
107   end
108 
109   [s, h, b]
110 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/response.rb
116 def finish_with_body(body)
117   set_default_headers
118   [@status || default_status, @headers, body]
119 end
inspect()

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

[show source]
    # File lib/roda/response.rb
129 def inspect
130   "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>"
131 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/response.rb
138 def redirect(path, status = 302)
139   @headers["Location"] = path
140   @status  = status
141   nil
142 end
roda_class()

Return the Roda class related to this response.

[show source]
    # File lib/roda/response.rb
145 def roda_class
146   self.class.roda_class
147 end
write(str)

Write to the response body. Returns nil.

response.write('foo')
[show source]
    # File lib/roda/response.rb
152 def write(str)
153   s = str.to_s
154   @length += s.bytesize
155   @body << s
156   nil
157 end