Class: Protocol::HTTP::Response
- Inherits:
-
Object
- Object
- Protocol::HTTP::Response
- Includes:
- Body::Reader
- Defined in:
- lib/protocol/http/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#status ⇒ Object
Returns the value of attribute status.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
-
#bad_request? ⇒ Boolean
Whether the status is 400 (bad request).
-
#continue? ⇒ Boolean
Whether the status is 100 (continue).
-
#failure? ⇒ Boolean
Whether the status is considered a failure.
-
#final? ⇒ Boolean
Whether the status is considered final.
- #hijack? ⇒ Boolean
-
#informational? ⇒ Boolean
Whether the status is considered informational.
-
#initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil) ⇒ Response
constructor
A new instance of Response.
-
#internal_server_error? ⇒ Boolean
(also: #server_failure?)
Whether the status is 500 (internal server error).
-
#not_modified? ⇒ Boolean
Whether the status is 304 (not modified).
-
#ok? ⇒ Boolean
Whether the status is 200 (ok).
-
#partial? ⇒ Boolean
Whether the status is 206 (partial content).
-
#preserve_method? ⇒ Boolean
Whether the status is 307 (temporary redirect) and should preserve the method of the request when following the redirect.
-
#redirection? ⇒ Boolean
Whether the status is considered a redirection.
-
#success? ⇒ Boolean
Whether the status is considered successful.
- #to_ary ⇒ Object
- #to_s ⇒ Object
Methods included from Body::Reader
#body?, #close, #each, #finish, #read, #save
Constructor Details
#initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil) ⇒ Response
Returns a new instance of Response.
14 15 16 17 18 19 20 |
# File 'lib/protocol/http/response.rb', line 14 def initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil) @version = version @status = status @headers = headers @body = body @protocol = protocol end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
25 26 27 |
# File 'lib/protocol/http/response.rb', line 25 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
24 25 26 |
# File 'lib/protocol/http/response.rb', line 24 def headers @headers end |
#protocol ⇒ Object
Returns the value of attribute protocol.
26 27 28 |
# File 'lib/protocol/http/response.rb', line 26 def protocol @protocol end |
#status ⇒ Object
Returns the value of attribute status.
23 24 25 |
# File 'lib/protocol/http/response.rb', line 23 def status @status end |
#version ⇒ Object
Returns the value of attribute version.
22 23 24 |
# File 'lib/protocol/http/response.rb', line 22 def version @version end |
Class Method Details
.[](status, headers = nil, body = nil, protocol = nil) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/protocol/http/response.rb', line 96 def self.[](status, headers = nil, body = nil, protocol = nil) body = Body::Buffered.wrap(body) headers = ::Protocol::HTTP::Headers[headers] self.new(nil, status, headers, body, protocol) end |
.for_exception(exception) ⇒ Object
103 104 105 |
# File 'lib/protocol/http/response.rb', line 103 def self.for_exception(exception) Response[500, Headers['content-type' => 'text/plain'], ["#{exception.class}: #{exception.message}"]] end |
Instance Method Details
#bad_request? ⇒ Boolean
Whether the status is 400 (bad request).
84 85 86 |
# File 'lib/protocol/http/response.rb', line 84 def bad_request? @status == 400 end |
#continue? ⇒ Boolean
Whether the status is 100 (continue).
33 34 35 |
# File 'lib/protocol/http/response.rb', line 33 def continue? @status == 100 end |
#failure? ⇒ Boolean
Whether the status is considered a failure.
79 80 81 |
# File 'lib/protocol/http/response.rb', line 79 def failure? @status and @status >= 400 && @status < 600 end |
#final? ⇒ Boolean
Whether the status is considered final. Note that 101 is considered final.
43 44 45 46 |
# File 'lib/protocol/http/response.rb', line 43 def final? # 101 is effectively a final status. @status and @status >= 200 || @status == 101 end |
#hijack? ⇒ Boolean
28 29 30 |
# File 'lib/protocol/http/response.rb', line 28 def hijack? false end |
#informational? ⇒ Boolean
Whether the status is considered informational.
38 39 40 |
# File 'lib/protocol/http/response.rb', line 38 def informational? @status and @status >= 100 && @status < 200 end |
#internal_server_error? ⇒ Boolean Also known as: server_failure?
Whether the status is 500 (internal server error).
89 90 91 |
# File 'lib/protocol/http/response.rb', line 89 def internal_server_error? @status == 500 end |
#not_modified? ⇒ Boolean
Whether the status is 304 (not modified).
69 70 71 |
# File 'lib/protocol/http/response.rb', line 69 def not_modified? @status == 304 end |
#ok? ⇒ Boolean
Whether the status is 200 (ok).
49 50 51 |
# File 'lib/protocol/http/response.rb', line 49 def ok? @status == 200 end |
#partial? ⇒ Boolean
Whether the status is 206 (partial content).
59 60 61 |
# File 'lib/protocol/http/response.rb', line 59 def partial? @status == 206 end |
#preserve_method? ⇒ Boolean
Whether the status is 307 (temporary redirect) and should preserve the method of the request when following the redirect.
74 75 76 |
# File 'lib/protocol/http/response.rb', line 74 def preserve_method? @status == 307 || @status == 308 end |
#redirection? ⇒ Boolean
Whether the status is considered a redirection.
64 65 66 |
# File 'lib/protocol/http/response.rb', line 64 def redirection? @status and @status >= 300 && @status < 400 end |
#success? ⇒ Boolean
Whether the status is considered successful.
54 55 56 |
# File 'lib/protocol/http/response.rb', line 54 def success? @status and @status >= 200 && @status < 300 end |
#to_ary ⇒ Object
111 112 113 |
# File 'lib/protocol/http/response.rb', line 111 def to_ary return @status, @headers, @body end |
#to_s ⇒ Object
107 108 109 |
# File 'lib/protocol/http/response.rb', line 107 def to_s "#{@status} #{@version}" end |