Class Rack::Response
In: lib/rack/response.rb
Parent: Object

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.

Methods

[]   []=   close   delete_cookie   each   empty?   finish   new   set_cookie   to_a   write  

Included Modules

Helpers

Classes and Modules

Module Rack::Response::Helpers

External Aliases

header -> headers

Attributes

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

Public Class methods

[Source]

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

Public Instance methods

[Source]

    # File lib/rack/response.rb, line 46
46:     def [](key)
47:       header[key]
48:     end

[Source]

    # File lib/rack/response.rb, line 50
50:     def []=(key, value)
51:       header[key] = value
52:     end

[Source]

     # File lib/rack/response.rb, line 122
122:     def close
123:       body.close if body.respond_to?(:close)
124:     end

[Source]

    # File lib/rack/response.rb, line 81
81:     def delete_cookie(key, value={})
82:       unless Array === self["Set-Cookie"]
83:         self["Set-Cookie"] = [self["Set-Cookie"]].compact
84:       end
85: 
86:       self["Set-Cookie"].reject! { |cookie|
87:         cookie =~ /\A#{Utils.escape(key)}=/
88:       }
89: 
90:       set_cookie(key,
91:                  {:value => '', :path => nil, :domain => nil,
92:                    :expires => Time.at(0) }.merge(value))
93:     end

[Source]

     # File lib/rack/response.rb, line 109
109:     def each(&callback)
110:       @body.each(&callback)
111:       @writer = callback
112:       @block.call(self)  if @block
113:     end

[Source]

     # File lib/rack/response.rb, line 126
126:     def empty?
127:       @block == nil && @body.empty?
128:     end

[Source]

     # File lib/rack/response.rb, line 96
 96:     def finish(&block)
 97:       @block = block
 98: 
 99:       if [204, 304].include?(status.to_i)
100:         header.delete "Content-Type"
101:         [status.to_i, header.to_hash, []]
102:       else
103:         header["Content-Length"] ||= @length.to_s
104:         [status.to_i, header.to_hash, self]
105:       end
106:     end

[Source]

    # File lib/rack/response.rb, line 54
54:     def set_cookie(key, value)
55:       case value
56:       when Hash
57:         domain  = "; domain="  + value[:domain]    if value[:domain]
58:         path    = "; path="    + value[:path]      if value[:path]
59:         # According to RFC 2109, we need dashes here.
60:         # N.B.: cgi.rb uses spaces...
61:         expires = "; expires=" + value[:expires].clone.gmtime.
62:           strftime("%a, %d-%b-%Y %H:%M:%S GMT")    if value[:expires]
63:         secure = "; secure"  if value[:secure]
64:         value = value[:value]
65:       end
66:       value = [value]  unless Array === value
67:       cookie = Utils.escape(key) + "=" +
68:         value.map { |v| Utils.escape v }.join("&") +
69:         "#{domain}#{path}#{expires}#{secure}"
70: 
71:       case self["Set-Cookie"]
72:       when Array
73:         self["Set-Cookie"] << cookie
74:       when String
75:         self["Set-Cookie"] = [self["Set-Cookie"], cookie]
76:       when nil
77:         self["Set-Cookie"] = cookie
78:       end
79:     end
to_a(&block)

Alias for finish

[Source]

     # File lib/rack/response.rb, line 115
115:     def write(str)
116:       s = str.to_s
117:       @length += s.size
118:       @writer.call s
119:       str
120:     end

[Validate]