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.to_i
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 100
100:     def close
101:       body.close if body.respond_to?(:close)
102:     end
each(&callback) click to toggle source

(Not documented)

    # File lib/rack/response.rb, line 81
81:     def each(&callback)
82:       @body.each(&callback)
83:       @writer = callback
84:       @block.call(self)  if @block
85:     end
empty?() click to toggle source

(Not documented)

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

(Not documented)

    # File lib/rack/response.rb, line 69
69:     def finish(&block)
70:       @block = block
71: 
72:       if [204, 304].include?(status.to_i)
73:         header.delete "Content-Type"
74:         [status.to_i, header, []]
75:       else
76:         [status.to_i, header, self]
77:       end
78:     end
Also aliased as: to_a
redirect(target, status=302) click to toggle source

(Not documented)

    # File lib/rack/response.rb, line 64
64:     def redirect(target, status=302)
65:       self.status = status
66:       self["Location"] = target
67:     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 91
91:     def write(str)
92:       s = str.to_s
93:       @length += Rack::Utils.bytesize(s)
94:       @writer.call s
95: 
96:       header["Content-Length"] = @length.to_s
97:       str
98:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.