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 (an OK response with empty headers and body).
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
Constant Summary collapse
- CHUNKED =
'chunked'.freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#header ⇒ Object
(also: #headers)
readonly
Returns the value of attribute header.
-
#length ⇒ Object
Returns the value of attribute length.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #chunked? ⇒ Boolean
- #close ⇒ Object
- #delete_header(key) ⇒ Object
- #each(&callback) ⇒ Object
- #empty? ⇒ Boolean
- #finish(&block) ⇒ Object (also: #to_a, #to_ary)
- #get_header(key) ⇒ Object (also: #[])
- #has_header?(key) ⇒ Boolean
-
#initialize(body = [], status = 200, header = {}) {|_self| ... } ⇒ Response
constructor
A new instance of Response.
- #redirect(target, status = 302) ⇒ Object
- #set_header(key, v) ⇒ Object (also: #[]=)
-
#write(str) ⇒ Object
Append to body and update Content-Length.
Methods included from Helpers
#accepted?, #add_header, #bad_request?, #cache_control, #cache_control=, #client_error?, #content_length, #content_type, #created?, #delete_cookie, #etag, #etag=, #forbidden?, #include?, #informational?, #invalid?, #location, #location=, #media_type, #media_type_params, #method_not_allowed?, #moved_permanently?, #no_content?, #not_found?, #ok?, #precondition_failed?, #redirect?, #redirection?, #server_error?, #set_cookie, #set_cookie_header, #set_cookie_header=, #successful?, #unauthorized?, #unprocessable?
Constructor Details
permalink #initialize(body = [], status = 200, header = {}) {|_self| ... } ⇒ Response
Returns a new instance of Response.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rack/response.rb', line 28 def initialize(body=[], status=200, header={}) @status = status.to_i @header = Utils::HeaderHash.new.merge(header) @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
permalink #body ⇒ Object
Returns the value of attribute body.
22 23 24 |
# File 'lib/rack/response.rb', line 22 def body @body end |
permalink #header ⇒ Object (readonly) Also known as: headers
Returns the value of attribute header.
23 24 25 |
# File 'lib/rack/response.rb', line 23 def header @header end |
permalink #length ⇒ Object
Returns the value of attribute length.
22 23 24 |
# File 'lib/rack/response.rb', line 22 def length @length end |
permalink #status ⇒ Object
Returns the value of attribute status.
22 23 24 |
# File 'lib/rack/response.rb', line 22 def status @status end |
Instance Method Details
permalink #chunked? ⇒ Boolean
56 57 58 |
# File 'lib/rack/response.rb', line 56 def chunked? CHUNKED == get_header(TRANSFER_ENCODING) end |
permalink #close ⇒ Object
[View source]
94 95 96 |
# File 'lib/rack/response.rb', line 94 def close body.close if body.respond_to?(:close) end |
permalink #delete_header(key) ⇒ Object
[View source]
105 |
# File 'lib/rack/response.rb', line 105 def delete_header(key); headers.delete key; end |
permalink #each(&callback) ⇒ Object
[View source]
75 76 77 78 79 |
# File 'lib/rack/response.rb', line 75 def each(&callback) @body.each(&callback) @writer = callback @block.call(self) if @block end |
permalink #empty? ⇒ Boolean
98 99 100 |
# File 'lib/rack/response.rb', line 98 def empty? @block == nil && @body.empty? end |
permalink #finish(&block) ⇒ Object Also known as: to_a, to_ary
[View source]
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rack/response.rb', line 60 def finish(&block) @block = block if [204, 205, 304].include?(status.to_i) delete_header CONTENT_TYPE delete_header CONTENT_LENGTH close [status.to_i, header, []] else [status.to_i, header, BodyProxy.new(self){}] end end |
permalink #get_header(key) ⇒ Object Also known as: []
[View source]
103 |
# File 'lib/rack/response.rb', line 103 def get_header(key); headers[key]; end |
permalink #has_header?(key) ⇒ Boolean
102 |
# File 'lib/rack/response.rb', line 102 def has_header?(key); headers.key? key; end |
permalink #redirect(target, status = 302) ⇒ Object
[View source]
51 52 53 54 |
# File 'lib/rack/response.rb', line 51 def redirect(target, status=302) self.status = status self.location = target end |
permalink #set_header(key, v) ⇒ Object Also known as: []=
[View source]
104 |
# File 'lib/rack/response.rb', line 104 def set_header(key, v); headers[key] = v; end |
permalink #write(str) ⇒ Object
Append to body and update Content-Length.
NOTE: Do not mix #write and direct #body access!
85 86 87 88 89 90 91 92 |
# File 'lib/rack/response.rb', line 85 def write(str) s = str.to_s @length += s.bytesize unless chunked? @writer.call s set_header(CONTENT_LENGTH, @length.to_s) unless chunked? str end |