Parent

Namespace

Included Modules

Class Index [+]

Quicksearch

Rack::Response

Rack::Response provides a convenient interface to create a Rack response.

It allows setting of headers and cookies, and provides useful defaults (a OK response containing HTML).

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 syncronous with the Rack response.

Your application’s call should end returning Response#finish.

Attributes

length[RW]
header[R]
status[RW]
body[RW]

Public Class Methods

new(body=[], status=200, header={}, &block) click to toggle source
    # File lib/rack/response.rb, line 22
22:     def initialize(body=[], status=200, header={}, &block)
23:       @status = status.to_i
24:       @header = Utils::HeaderHash.new({"Content-Type" => "text/html"}.
25:                                       merge(header))
26: 
27:       @writer = lambda { |x| @body << x }
28:       @block = nil
29:       @length = 0
30: 
31:       @body = []
32: 
33:       if body.respond_to? :to_str
34:         write body.to_str
35:       elsif body.respond_to?(:each)
36:         body.each { |part|
37:           write part.to_s
38:         }
39:       else
40:         raise TypeError, "stringable or iterable required"
41:       end
42: 
43:       yield self  if block_given?
44:     end

Public Instance Methods

[](key) click to toggle source
    # File lib/rack/response.rb, line 49
49:     def [](key)
50:       header[key]
51:     end
[]=(key, value) click to toggle source
    # File lib/rack/response.rb, line 53
53:     def []=(key, value)
54:       header[key] = value
55:     end
close() click to toggle source
     # File lib/rack/response.rb, line 101
101:     def close
102:       body.close if body.respond_to?(:close)
103:     end
each(&callback) click to toggle source
    # File lib/rack/response.rb, line 82
82:     def each(&callback)
83:       @body.each(&callback)
84:       @writer = callback
85:       @block.call(self)  if @block
86:     end
empty?() click to toggle source
     # File lib/rack/response.rb, line 105
105:     def empty?
106:       @block == nil && @body.empty?
107:     end
finish(&block) click to toggle source
    # File lib/rack/response.rb, line 70
70:     def finish(&block)
71:       @block = block
72: 
73:       if [204, 304].include?(status.to_i)
74:         header.delete "Content-Type"
75:         [status.to_i, header, []]
76:       else
77:         [status.to_i, header, self]
78:       end
79:     end
Also aliased as: to_a
redirect(target, status=302) click to toggle source
    # File lib/rack/response.rb, line 65
65:     def redirect(target, status=302)
66:       self.status = status
67:       self["Location"] = target
68:     end
to_a(&block) click to toggle source
Alias for: finish
write(str) click to toggle source

Append to body and update Content-Length.

NOTE: Do not mix # and direct # access!

    # File lib/rack/response.rb, line 92
92:     def write(str)
93:       s = str.to_s
94:       @length += Rack::Utils.bytesize(s)
95:       @writer.call s
96: 
97:       header["Content-Length"] = @length.to_s
98:       str
99:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.