Instance methods for RodaResponse
Methods
Public Class
Public Instance
Constants
DEFAULT_HEADERS | = | {RodaResponseHeaders::CONTENT_TYPE => "text/html".freeze}.freeze |
Attributes
Public Class methods
Set the default headers when creating a response.
# File lib/roda/response.rb 61 def initialize 62 @headers = _initialize_headers 63 @body = [] 64 @length = 0 65 end
Public Instance methods
Return the response header with the given key. Example:
response['Content-Type'] # => 'text/html'
# File lib/roda/response.rb 70 def [](key) 71 @headers[key] 72 end
Set the response header with the given key to the given value.
response['Content-Type'] = 'application/json'
# File lib/roda/response.rb 77 def []=(key, value) 78 @headers[key] = value 79 end
The default headers to use for responses.
# File lib/roda/response.rb 82 def default_headers 83 DEFAULT_HEADERS 84 end
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.
# File lib/roda/response.rb 143 def default_status 144 200 145 end
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
# File lib/roda/response.rb 93 def empty? 94 @body.empty? 95 end
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'}, # []]
# File lib/roda/response.rb 109 def finish 110 b = @body 111 set_default_headers 112 h = @headers 113 114 if b.empty? 115 s = @status || 404 116 if (s == 304 || s == 204 || (s >= 100 && s <= 199)) 117 h.delete(RodaResponseHeaders::CONTENT_TYPE) 118 elsif s == 205 119 empty_205_headers(h) 120 else 121 h[RodaResponseHeaders::CONTENT_LENGTH] ||= '0' 122 end 123 else 124 s = @status || default_status 125 h[RodaResponseHeaders::CONTENT_LENGTH] ||= @length.to_s 126 end 127 128 [s, h, b] 129 end
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.
# File lib/roda/response.rb 135 def finish_with_body(body) 136 set_default_headers 137 [@status || default_status, @headers, body] 138 end
Show response class, status code, response headers, and response body
# File lib/roda/response.rb 148 def inspect 149 "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>" 150 end
Set the Location header to the given path, and the status to the given status. Example:
response.redirect('foo', 301) response.redirect('bar')
# File lib/roda/response.rb 157 def redirect(path, status = 302) 158 @headers[RodaResponseHeaders::LOCATION] = path 159 @status = status 160 nil 161 end
Return the Roda
class related to this response.
# File lib/roda/response.rb 164 def roda_class 165 self.class.roda_class 166 end
Write to the response body. Returns nil.
response.write('foo')
# File lib/roda/response.rb 171 def write(str) 172 s = str.to_s 173 @length += s.bytesize 174 @body << s 175 nil 176 end