Class: Rack::Response
Overview
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 synchronous with the Rack response.
Your application's call should end returning Response#finish.
Direct Known Subclasses
Defined Under Namespace
Modules: Helpers
Instance Attribute Summary (collapse)
-
- (Object) body
Returns the value of attribute body.
-
- (Object) header
(also: #headers)
readonly
Returns the value of attribute header.
-
- (Object) length
Returns the value of attribute length.
-
- (Object) status
Returns the value of attribute status.
Attributes included from Helpers
Instance Method Summary (collapse)
- - (Object) [](key)
- - (Object) []=(key, value)
- - (Object) close
- - (Object) delete_cookie(key, value = {})
- - (Object) each(&callback)
- - (Boolean) empty?
- - (Object) finish(&block) (also: #to_a, #to_ary)
-
- (Response) initialize(body = [], status = 200, header = {}) {|_self| ... }
constructor
A new instance of Response.
- - (Object) redirect(target, status = 302)
- - (Object) set_cookie(key, value)
-
- (Object) write(str)
Append to body and update Content-Length.
Methods included from Helpers
#bad_request?, #client_error?, #content_length, #content_type, #forbidden?, #include?, #informational?, #invalid?, #location, #method_not_allowed?, #not_found?, #ok?, #redirect?, #redirection?, #server_error?, #successful?, #unprocessable?
Constructor Details
- (Response) initialize(body = [], status = 200, header = {}) {|_self| ... }
A new instance of Response
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rack/response.rb', line 22 def initialize(body=[], status=200, header={}) @status = status.to_i @header = Utils::HeaderHash.new("Content-Type" => "text/html"). merge(header) @chunked = "chunked" == @header['Transfer-Encoding'] @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 Attribute Details
- (Object) body
Returns the value of attribute body
48 49 50 |
# File 'lib/rack/response.rb', line 48 def body @body end |
- (Object) header (readonly) Also known as: headers
Returns the value of attribute header
47 48 49 |
# File 'lib/rack/response.rb', line 47 def header @header end |
- (Object) length
Returns the value of attribute length
20 21 22 |
# File 'lib/rack/response.rb', line 20 def length @length end |
- (Object) status
Returns the value of attribute status
48 49 50 |
# File 'lib/rack/response.rb', line 48 def status @status end |
Instance Method Details
- (Object) [](key)
50 51 52 |
# File 'lib/rack/response.rb', line 50 def [](key) header[key] end |
- (Object) []=(key, value)
54 55 56 |
# File 'lib/rack/response.rb', line 54 def []=(key, value) header[key] = value end |
- (Object) close
104 105 106 |
# File 'lib/rack/response.rb', line 104 def close body.close if body.respond_to?(:close) end |
- (Object) delete_cookie(key, value = {})
62 63 64 |
# File 'lib/rack/response.rb', line 62 def (key, value={}) Utils.(header, key, value) end |
- (Object) each(&callback)
85 86 87 88 89 |
# File 'lib/rack/response.rb', line 85 def each(&callback) @body.each(&callback) @writer = callback @block.call(self) if @block end |
- (Boolean) empty?
108 109 110 |
# File 'lib/rack/response.rb', line 108 def empty? @block == nil && @body.empty? end |
- (Object) finish(&block) Also known as: to_a, to_ary
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rack/response.rb', line 71 def finish(&block) @block = block if [204, 205, 304].include?(status.to_i) header.delete "Content-Type" header.delete "Content-Length" [status.to_i, header, []] else [status.to_i, header, self] end end |
- (Object) redirect(target, status = 302)
66 67 68 69 |
# File 'lib/rack/response.rb', line 66 def redirect(target, status=302) self.status = status self["Location"] = target end |
- (Object) set_cookie(key, value)
58 59 60 |
# File 'lib/rack/response.rb', line 58 def (key, value) Utils.(header, key, value) end |
- (Object) write(str)
Append to body and update Content-Length.
NOTE: Do not mix #write and direct #body access!
95 96 97 98 99 100 101 102 |
# File 'lib/rack/response.rb', line 95 def write(str) s = str.to_s @length += Rack::Utils.bytesize(s) unless @chunked @writer.call s header["Content-Length"] = @length.to_s unless @chunked str end |