module Roda::RodaPlugins::Base::ResponseMethods

  1. lib/roda.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.rb
1260 def initialize
1261   @headers = {}
1262   @body    = []
1263   @length  = 0
1264 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
1269 def [](key)
1270   @headers[key]
1271 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
1276 def []=(key, value)
1277   @headers[key] = value
1278 end
default_headers ()

The default headers to use for responses.

[show source]
     # File lib/roda.rb
1281 def default_headers
1282   DEFAULT_HEADERS
1283 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
1343 def default_status
1344   200
1345 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
1292 def empty?
1293   @body.empty?
1294 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
1308 def finish
1309   b = @body
1310   set_default_headers
1311   h = @headers
1312 
1313   if b.empty?
1314     s = @status || 404
1315     if (s == 304 || s == 204 || (s >= 100 && s <= 199))
1316       h.delete("Content-Type")
1317     elsif s == 205
1318       h.delete("Content-Type")
1319       h["Content-Length"] = '0'
1320     else
1321       h["Content-Length"] ||= '0'
1322     end
1323   else
1324     s = @status || default_status
1325     h["Content-Length"] ||= @length.to_s
1326   end
1327 
1328   [s, h, b]
1329 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
1335 def finish_with_body(body)
1336   set_default_headers
1337   [@status || default_status, @headers, body]
1338 end
inspect ()

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

[show source]
     # File lib/roda.rb
1348 def inspect
1349   "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>"
1350 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
1357 def redirect(path, status = 302)
1358   @headers["Location"] = path
1359   @status  = status
1360   nil
1361 end
roda_class ()

Return the Roda class related to this response.

[show source]
     # File lib/roda.rb
1364 def roda_class
1365   self.class.roda_class
1366 end
write (str)

Write to the response body. Returns nil.

response.write('foo')
[show source]
     # File lib/roda.rb
1371 def write(str)
1372   s = str.to_s
1373   @length += s.bytesize
1374   @body << s
1375   nil
1376 end