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]

(Not documented)

header[R]

(Not documented)

status[RW]

(Not documented)

body[RW]

(Not documented)

Public Class Methods

new(body=[], status=200, header={}, &block) click to toggle source

(Not documented)

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

Public Instance Methods

[](key) click to toggle source

(Not documented)

    # File lib/rack/response.rb, line 48
48:     def [](key)
49:       header[key]
50:     end
[]=(key, value) click to toggle source

(Not documented)

    # File lib/rack/response.rb, line 52
52:     def []=(key, value)
53:       header[key] = value
54:     end
close() click to toggle source

(Not documented)

     # File lib/rack/response.rb, line 134
134:     def close
135:       body.close if body.respond_to?(:close)
136:     end
each(&callback) click to toggle source

(Not documented)

     # File lib/rack/response.rb, line 115
115:     def each(&callback)
116:       @body.each(&callback)
117:       @writer = callback
118:       @block.call(self)  if @block
119:     end
empty?() click to toggle source

(Not documented)

     # File lib/rack/response.rb, line 138
138:     def empty?
139:       @block == nil && @body.empty?
140:     end
finish(&block) click to toggle source

(Not documented)

     # File lib/rack/response.rb, line 103
103:     def finish(&block)
104:       @block = block
105: 
106:       if [204, 304].include?(status.to_i)
107:         header.delete "Content-Type"
108:         [status.to_i, header.to_hash, []]
109:       else
110:         [status.to_i, header.to_hash, self]
111:       end
112:     end
Also aliased as: to_a
redirect(target, status=302) click to toggle source

(Not documented)

     # File lib/rack/response.rb, line 98
 98:     def redirect(target, status=302)
 99:       self.status = status
100:       self["Location"] = target
101:     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 write and direct body access!

     # File lib/rack/response.rb, line 125
125:     def write(str)
126:       s = str.to_s
127:       @length += Rack::Utils.bytesize(s)
128:       @writer.call s
129: 
130:       header["Content-Length"] = @length.to_s
131:       str
132:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.