Module: Roda::RodaPlugins::Base::ResponseMethods
- Defined in:
- lib/roda/response.rb
Overview
Instance methods for RodaResponse
Constant Summary collapse
- DEFAULT_HEADERS =
{"Content-Type" => "text/html".freeze}.freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
The body for the current response.
-
#headers ⇒ Object
readonly
The hash of response headers for the current response.
-
#status ⇒ Object
The status code to use for the response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the response header with the given key.
-
#[]=(key, value) ⇒ Object
Set the response header with the given key to the given value.
-
#default_headers ⇒ Object
The default headers to use for responses.
-
#default_status ⇒ Object
Return the default response status to be used when the body has been written to.
-
#empty? ⇒ Boolean
Whether the response body has been written to yet.
-
#finish ⇒ Object
Return the rack response array of status, headers, and body for the current response.
-
#finish_with_body(body) ⇒ Object
Return the rack response array using a given body.
-
#initialize ⇒ Object
Set the default headers when creating a response.
-
#inspect ⇒ Object
Show response class, status code, response headers, and response body.
-
#redirect(path, status = 302) ⇒ Object
Set the Location header to the given path, and the status to the given status.
-
#roda_class ⇒ Object
Return the Roda class related to this response.
-
#write(str) ⇒ Object
Write to the response body.
Instance Attribute Details
#body ⇒ Object (readonly)
The body for the current response.
31 32 33 |
# File 'lib/roda/response.rb', line 31 def body @body end |
#headers ⇒ Object (readonly)
The hash of response headers for the current response.
34 35 36 |
# File 'lib/roda/response.rb', line 34 def headers @headers end |
#status ⇒ Object
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.
38 39 40 |
# File 'lib/roda/response.rb', line 38 def status @status end |
Instance Method Details
#[](key) ⇒ Object
Return the response header with the given key. Example:
response['Content-Type'] # => 'text/html'
50 51 52 |
# File 'lib/roda/response.rb', line 50 def [](key) @headers[key] end |
#[]=(key, value) ⇒ Object
Set the response header with the given key to the given value.
response['Content-Type'] = 'application/json'
57 58 59 |
# File 'lib/roda/response.rb', line 57 def []=(key, value) @headers[key] = value end |
#default_headers ⇒ Object
The default headers to use for responses.
62 63 64 |
# File 'lib/roda/response.rb', line 62 def default_headers DEFAULT_HEADERS end |
#default_status ⇒ Object
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.
124 125 126 |
# File 'lib/roda/response.rb', line 124 def default_status 200 end |
#empty? ⇒ Boolean
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
73 74 75 |
# File 'lib/roda/response.rb', line 73 def empty? @body.empty? end |
#finish ⇒ Object
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'},
# []]
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/roda/response.rb', line 89 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("Content-Type") elsif s == 205 h.delete("Content-Type") h["Content-Length"] = '0' else h["Content-Length"] ||= '0' end else s = @status || default_status h["Content-Length"] ||= @length.to_s end [s, h, b] end |
#finish_with_body(body) ⇒ Object
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.
116 117 118 119 |
# File 'lib/roda/response.rb', line 116 def finish_with_body(body) set_default_headers [@status || default_status, @headers, body] end |
#initialize ⇒ Object
Set the default headers when creating a response.
41 42 43 44 45 |
# File 'lib/roda/response.rb', line 41 def initialize @headers = {} @body = [] @length = 0 end |
#inspect ⇒ Object
Show response class, status code, response headers, and response body
129 130 131 |
# File 'lib/roda/response.rb', line 129 def inspect "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>" end |
#redirect(path, status = 302) ⇒ Object
Set the Location header to the given path, and the status to the given status. Example:
response.redirect('foo', 301)
response.redirect('bar')
138 139 140 141 142 |
# File 'lib/roda/response.rb', line 138 def redirect(path, status = 302) @headers["Location"] = path @status = status nil end |
#roda_class ⇒ Object
Return the Roda class related to this response.
145 146 147 |
# File 'lib/roda/response.rb', line 145 def roda_class self.class.roda_class end |
#write(str) ⇒ Object
Write to the response body. Returns nil.
response.write('foo')
152 153 154 155 156 157 |
# File 'lib/roda/response.rb', line 152 def write(str) s = str.to_s @length += s.bytesize @body << s nil end |