class Rack::Response
Rack::Response
provides a convenient interface to create a Rack
response.
It allows setting of headers and cookies, and provides useful defaults (an OK response with empty headers and body).
You can use Response#write
to iteratively generate your response, but note that this is buffered by Rack::Response
until you call finish
. finish
however can take a block inside which calls to write
are synchronous with the Rack
response.
Your application's call
should end returning Response#finish
.
Constants
- CHUNKED
- STATUS_WITH_NO_ENTITY_BODY
Attributes
body[RW]
header[R]
headers[R]
length[RW]
status[RW]
Public Class Methods
new(body = nil, status = 200, header = {}) { |self| ... }
click to toggle source
# File lib/rack/response.rb, line 31 def initialize(body = nil, status = 200, header = {}) @status = status.to_i @header = Utils::HeaderHash.new(header) @writer = self.method(:append) @block = nil @length = 0 # Keep track of whether we have expanded the user supplied body. if body.nil? @body = [] @buffered = true elsif body.respond_to?(:to_str) @body = [body] @buffered = true else @body = body @buffered = false end yield self if block_given? end
Public Instance Methods
chunked?()
click to toggle source
# File lib/rack/response.rb, line 60 def chunked? CHUNKED == get_header(TRANSFER_ENCODING) end
close()
click to toggle source
# File lib/rack/response.rb, line 102 def close @body.close if @body.respond_to?(:close) end
delete_header(key)
click to toggle source
# File lib/rack/response.rb, line 113 def delete_header(key); headers.delete key; end
each(&callback)
click to toggle source
# File lib/rack/response.rb, line 82 def each(&callback) @body.each(&callback) @buffered = true if @block @writer = callback @block.call(self) end end
empty?()
click to toggle source
# File lib/rack/response.rb, line 106 def empty? @block == nil && @body.empty? end
finish(&block)
click to toggle source
# File lib/rack/response.rb, line 64 def finish(&block) if STATUS_WITH_NO_ENTITY_BODY[status.to_i] delete_header CONTENT_TYPE delete_header CONTENT_LENGTH close [status.to_i, header, []] else if block_given? @block = block [status.to_i, header, self] else [status.to_i, header, @body] end end end
Also aliased as: to_a
get_header(key)
click to toggle source
# File lib/rack/response.rb, line 111 def get_header(key); headers[key]; end
Also aliased as: []
has_header?(key)
click to toggle source
# File lib/rack/response.rb, line 110 def has_header?(key); headers.key? key; end
redirect(target, status = 302)
click to toggle source
# File lib/rack/response.rb, line 55 def redirect(target, status = 302) self.status = status self.location = target end
set_header(key, v)
click to toggle source
# File lib/rack/response.rb, line 112 def set_header(key, v); headers[key] = v; end
Also aliased as: []=