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