Class: Rack::Response
- Includes:
- Helpers
- Defined in:
- lib/gems/rack-0.9.1/lib/rack/response.rb
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 syncronous 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
-
#body ⇒ Object
Returns the value of attribute body.
-
#header ⇒ Object
(also: #headers)
readonly
Returns the value of attribute header.
-
#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)
-
#initialize(body = [], status = 200, header = {}) {|_self| ... } ⇒ Response
constructor
A new instance of Response.
- #set_cookie(key, value) ⇒ Object
- #write(str) ⇒ Object
Methods included from Helpers
#client_error?, #content_length, #content_type, #forbidden?, #include?, #informational?, #invalid?, #location, #not_found?, #ok?, #redirect?, #redirection?, #server_error?, #successful?
Constructor Details
#initialize(body = [], status = 200, header = {}) {|_self| ... } ⇒ Response
Returns a new instance of Response.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 19 def initialize(body=[], status=200, header={}, &block) @status = status @header = Utils::HeaderHash.new({"Content-Type" => "text/html"}. 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
#body ⇒ Object
Returns the value of attribute body.
44 45 46 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 44 def body @body end |
#header ⇒ Object (readonly) Also known as: headers
Returns the value of attribute header.
43 44 45 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 43 def header @header end |
#status ⇒ Object
Returns the value of attribute status.
44 45 46 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 44 def status @status end |
Instance Method Details
#[](key) ⇒ Object
46 47 48 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 46 def [](key) header[key] end |
#[]=(key, value) ⇒ Object
50 51 52 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 50 def []=(key, value) header[key] = value end |
#close ⇒ Object
122 123 124 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 122 def close body.close if body.respond_to?(:close) end |
#delete_cookie(key, value = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 81 def (key, value={}) unless Array === self["Set-Cookie"] self["Set-Cookie"] = [self["Set-Cookie"]].compact end self["Set-Cookie"].reject! { || =~ /\A#{Utils.escape(key)}=/ } (key, {:value => '', :path => nil, :domain => nil, :expires => Time.at(0) }.merge(value)) end |
#each(&callback) ⇒ Object
109 110 111 112 113 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 109 def each(&callback) @body.each(&callback) @writer = callback @block.call(self) if @block end |
#empty? ⇒ Boolean
126 127 128 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 126 def empty? @block == nil && @body.empty? end |
#finish(&block) ⇒ Object Also known as: to_a
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 96 def finish(&block) @block = block if [204, 304].include?(status.to_i) header.delete "Content-Type" [status.to_i, header.to_hash, []] else header["Content-Length"] ||= @length.to_s [status.to_i, header.to_hash, self] end end |
#set_cookie(key, value) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 54 def (key, value) case value when Hash domain = "; domain=" + value[:domain] if value[:domain] path = "; path=" + value[:path] if value[:path] # According to RFC 2109, we need dashes here. # N.B.: cgi.rb uses spaces... expires = "; expires=" + value[:expires].clone.gmtime. strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires] secure = "; secure" if value[:secure] value = value[:value] end value = [value] unless Array === value = Utils.escape(key) + "=" + value.map { |v| Utils.escape v }.join("&") + "#{domain}#{path}#{expires}#{secure}" case self["Set-Cookie"] when Array self["Set-Cookie"] << when String self["Set-Cookie"] = [self["Set-Cookie"], ] when nil self["Set-Cookie"] = end end |
#write(str) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/gems/rack-0.9.1/lib/rack/response.rb', line 115 def write(str) s = str.to_s @length += s.size @writer.call s str end |