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
Constant Summary collapse
- CHUNKED =
'chunked'.freeze
- TRANSFER_ENCODING =
'Transfer-Encoding'.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.
Attributes included from Helpers
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #close ⇒ Object
- #delete_cookie(key, value = {}) ⇒ Object
- #each(&callback) ⇒ Object
- #empty? ⇒ Boolean
- #finish(&block) ⇒ Object (also: #to_a, #to_ary)
-
#initialize(body = [], status = 200, header = {}) {|_self| ... } ⇒ Response
constructor
A new instance of Response.
- #redirect(target, status = 302) ⇒ Object
- #set_cookie(key, value) ⇒ Object
-
#write(str) ⇒ Object
Append to body and update Content-Length.
Methods included from Helpers
#accepted?, #bad_request?, #client_error?, #content_length, #content_type, #created?, #forbidden?, #i_m_a_teapot?, #include?, #informational?, #invalid?, #location, #method_not_allowed?, #not_found?, #ok?, #redirect?, #redirection?, #server_error?, #successful?, #unauthorized?, #unprocessable?
Constructor Details
#initialize(body = [], status = 200, header = {}) {|_self| ... } ⇒ Response
Returns a new instance of Response.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rack/response.rb', line 25 def initialize(body=[], status=200, header={}) @status = status.to_i @header = Utils::HeaderHash.new.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
#body ⇒ Object
Returns the value of attribute body.
50 51 52 |
# File 'lib/rack/response.rb', line 50 def body @body end |
#header ⇒ Object (readonly) Also known as: headers
Returns the value of attribute header.
49 50 51 |
# File 'lib/rack/response.rb', line 49 def header @header end |
#length ⇒ Object
Returns the value of attribute length.
21 22 23 |
# File 'lib/rack/response.rb', line 21 def length @length end |
#status ⇒ Object
Returns the value of attribute status.
50 51 52 |
# File 'lib/rack/response.rb', line 50 def status @status end |
Instance Method Details
#[](key) ⇒ Object
52 53 54 |
# File 'lib/rack/response.rb', line 52 def [](key) header[key] end |
#[]=(key, value) ⇒ Object
56 57 58 |
# File 'lib/rack/response.rb', line 56 def []=(key, value) header[key] = value end |
#close ⇒ Object
107 108 109 |
# File 'lib/rack/response.rb', line 107 def close body.close if body.respond_to?(:close) end |
#delete_cookie(key, value = {}) ⇒ Object
64 65 66 |
# File 'lib/rack/response.rb', line 64 def (key, value={}) Utils.(header, key, value) end |
#each(&callback) ⇒ Object
88 89 90 91 92 |
# File 'lib/rack/response.rb', line 88 def each(&callback) @body.each(&callback) @writer = callback @block.call(self) if @block end |
#empty? ⇒ Boolean
111 112 113 |
# File 'lib/rack/response.rb', line 111 def empty? @block == nil && @body.empty? end |
#finish(&block) ⇒ Object Also known as: to_a, to_ary
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rack/response.rb', line 73 def finish(&block) @block = block if [204, 205, 304].include?(status.to_i) header.delete CONTENT_TYPE header.delete CONTENT_LENGTH close [status.to_i, header, []] else [status.to_i, header, BodyProxy.new(self){}] end end |
#redirect(target, status = 302) ⇒ Object
68 69 70 71 |
# File 'lib/rack/response.rb', line 68 def redirect(target, status=302) self.status = status self["Location"] = target end |
#set_cookie(key, value) ⇒ Object
60 61 62 |
# File 'lib/rack/response.rb', line 60 def (key, value) Utils.(header, key, value) end |
#write(str) ⇒ Object
Append to body and update Content-Length.
NOTE: Do not mix #write and direct #body access!
98 99 100 101 102 103 104 105 |
# File 'lib/rack/response.rb', line 98 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 |