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 #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 #finish.

Namespace
Methods
#
C
D
E
F
G
H
N
R
S
T
W
Included Modules
Constants
CHUNKED = 'chunked'.freeze
 
Attributes
[RW] body
[R] header
[R] headers
[RW] length
[RW] status
Class Public methods
new(body=[], status=200, header={})
# File lib/rack/response.rb, line 28
def initialize(body=[], status=200, header={})
  @status = status.to_i
  @header = Utils::HeaderHash.new.merge(header)

  @writer  = lambda { |x| @body << x }
  @block   = nil
  @length  = 0

  @body = []

  if body.respond_to? :to_str
    write body.to_str
  elsif body.respond_to?(:each)
    body.each { |part|
      write part.to_s
    }
  else
    raise TypeError, "stringable or iterable required"
  end

  yield self  if block_given?
end
Instance Public methods
[](key)
Alias for: get_header
[]=(key, v)
Alias for: set_header
chunked?()
# File lib/rack/response.rb, line 56
def chunked?
  CHUNKED == get_header(TRANSFER_ENCODING)
end
close()
# File lib/rack/response.rb, line 94
def close
  body.close if body.respond_to?(:close)
end
delete_header(key)
# File lib/rack/response.rb, line 105
def delete_header(key); headers.delete key; end
each(&callback)
# File lib/rack/response.rb, line 75
def each(&callback)
  @body.each(&callback)
  @writer = callback
  @block.call(self)  if @block
end
empty?()
# File lib/rack/response.rb, line 98
def empty?
  @block == nil && @body.empty?
end
finish(&block)
Also aliased as: to_a, to_ary
# File lib/rack/response.rb, line 60
def finish(&block)
  @block = block

  if [204, 304].include?(status.to_i)
    delete_header CONTENT_TYPE
    delete_header CONTENT_LENGTH
    close
    [status.to_i, header, []]
  else
    [status.to_i, header, BodyProxy.new(self){}]
  end
end
get_header(key)
Also aliased as: []
# File lib/rack/response.rb, line 103
def get_header(key);    headers[key];       end
has_header?(key)
# File lib/rack/response.rb, line 102
def has_header?(key);   headers.key? key;   end
redirect(target, status=302)
# File lib/rack/response.rb, line 51
def redirect(target, status=302)
  self.status = status
  self.location = target
end
set_header(key, v)
Also aliased as: []=
# File lib/rack/response.rb, line 104
def set_header(key, v); headers[key] = v;   end
to_a(&block)
Alias for: finish
to_ary(&block)
Alias for: finish
write(str)

Append to body and update Content-Length.

NOTE: Do not mix write and direct body access!

# File lib/rack/response.rb, line 85
def write(str)
  s = str.to_s
  @length += s.bytesize unless chunked?
  @writer.call s

  set_header(CONTENT_LENGTH, @length.to_s) unless chunked?
  str
end