Class: Protocol::HTTP::Response
- Inherits:
-
Object
- Object
- Protocol::HTTP::Response
- Includes:
- Body::Reader
- Defined in:
- lib/protocol/http/response.rb
Overview
Represents an HTTP response which can be used both server and client-side.
~~~ ruby require ‘protocol/http’
# Long form: Protocol::HTTP::Response.new(“http/1.1”, 200, Protocol::HTTP::Headers[[“content-type”, “text/html”]], Protocol::HTTP::Body::Buffered.wrap(“Hello, World!”))
# Short form: Protocol::HTTP::Response[200, => “text/html”, [“Hello, World!”]] ~~~
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.
- #The HTTP status code, e.g. `200`, `404`, etc.(HTTPstatuscode, e.g.`200`, `404`, etc.) ⇒ Object readonly
- #The HTTP version, usually one of `"HTTP/1.1"`, `"HTTP/2"`, etc.(HTTPversion, usuallyoneof`"HTTP/1.1"`, `"HTTP/2"`, etc.) ⇒ Object readonly
- #The protocol, e.g. `"websocket"`, etc.(protocol, e.g.`"websocket"`, etc.) ⇒ Object readonly
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil) ⇒ Object
A short-cut method which exposes the main response variables that you’d typically care about.
-
.for_exception(exception) ⇒ Object
Create a response for the given exception.
Instance Method Summary collapse
-
#as_json ⇒ Object
Convert the response to a hash suitable for serialization.
-
#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
Whether the response is considered a hijack: the connection has been taken over by the application and the server should not send any more data.
-
#informational? ⇒ Boolean
Whether the status is considered informational.
-
#initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil) ⇒ Response
constructor
Create a new 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).
-
#peer ⇒ Object
A response that is generated by a client, may choose to include the peer (address) associated with the response.
-
#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.
- #The body, e.g. `"Hello, World!"`, etc.=(body, e.g.`"Hello, World!"`, etc. = (value)) ⇒ Object
- #The headers, e.g. `{"content-type" => "text/html"}`, etc.=(headers, e.g.`{"content-type" = > "text/html"}`, etc. = (value)) ⇒ Object
-
#to_ary ⇒ Object
Implicit conversion to an array.
-
#to_json ⇒ Object
Convert the response to JSON.
-
#to_s ⇒ Object
Summarise the response as a string.
Methods included from Body::Reader
#body?, #buffered!, #close, #discard, #each, #finish, #read, #save
Constructor Details
#initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil) ⇒ Response
Create a new response.
32 33 34 35 36 37 38 |
# File 'lib/protocol/http/response.rb', line 32 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.
50 51 52 |
# File 'lib/protocol/http/response.rb', line 50 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
47 48 49 |
# File 'lib/protocol/http/response.rb', line 47 def headers @headers end |
#protocol ⇒ Object
Returns the value of attribute protocol.
53 54 55 |
# File 'lib/protocol/http/response.rb', line 53 def protocol @protocol end |
#status ⇒ Object
Returns the value of attribute status.
44 45 46 |
# File 'lib/protocol/http/response.rb', line 44 def status @status end |
#The HTTP status code, e.g. `200`, `404`, etc.(HTTPstatuscode, e.g.`200`, `404`, etc.) ⇒ Object (readonly)
44 |
# File 'lib/protocol/http/response.rb', line 44 attr_accessor :status |
#The HTTP version, usually one of `"HTTP/1.1"`, `"HTTP/2"`, etc.(HTTPversion, usuallyoneof`"HTTP/1.1"`, `"HTTP/2"`, etc.) ⇒ Object (readonly)
41 |
# File 'lib/protocol/http/response.rb', line 41 attr_accessor :version |
#The protocol, e.g. `"websocket"`, etc.(protocol, e.g.`"websocket"`, etc.) ⇒ Object (readonly)
53 |
# File 'lib/protocol/http/response.rb', line 53 attr_accessor :protocol |
#version ⇒ Object
Returns the value of attribute version.
41 42 43 |
# File 'lib/protocol/http/response.rb', line 41 def version @version end |
Class Method Details
.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil) ⇒ Object
A short-cut method which exposes the main response variables that you’d typically care about. It follows the same order as the ‘Rack` response tuple, but also includes the protocol.
~~~ ruby Response[200, => “text/html”, [“Hello, World!”]] ~~~
140 141 142 143 144 145 |
# File 'lib/protocol/http/response.rb', line 140 def self.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil) body = Body::Buffered.wrap(body) headers = Headers[headers] self.new(nil, status, headers, body, protocol) end |
Instance Method Details
#as_json ⇒ Object
Convert the response to a hash suitable for serialization.
157 158 159 160 161 162 163 164 165 |
# File 'lib/protocol/http/response.rb', line 157 def as_json(...) { version: @version, status: @status, headers: @headers&.as_json, body: @body&.as_json, protocol: @protocol } end |
#bad_request? ⇒ Boolean
Whether the status is 400 (bad request).
119 120 121 |
# File 'lib/protocol/http/response.rb', line 119 def bad_request? @status == 400 end |
#continue? ⇒ Boolean
Whether the status is 100 (continue).
68 69 70 |
# File 'lib/protocol/http/response.rb', line 68 def continue? @status == 100 end |
#failure? ⇒ Boolean
Whether the status is considered a failure.
114 115 116 |
# File 'lib/protocol/http/response.rb', line 114 def failure? @status and @status >= 400 && @status < 600 end |
#final? ⇒ Boolean
Whether the status is considered final. Note that 101 is considered final.
78 79 80 81 |
# File 'lib/protocol/http/response.rb', line 78 def final? # 101 is effectively a final status. @status and @status >= 200 || @status == 101 end |
#hijack? ⇒ Boolean
Whether the response is considered a hijack: the connection has been taken over by the application and the server should not send any more data.
63 64 65 |
# File 'lib/protocol/http/response.rb', line 63 def hijack? false end |
#informational? ⇒ Boolean
Whether the status is considered informational.
73 74 75 |
# File 'lib/protocol/http/response.rb', line 73 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).
124 125 126 |
# File 'lib/protocol/http/response.rb', line 124 def internal_server_error? @status == 500 end |
#not_modified? ⇒ Boolean
Whether the status is 304 (not modified).
104 105 106 |
# File 'lib/protocol/http/response.rb', line 104 def not_modified? @status == 304 end |
#ok? ⇒ Boolean
Whether the status is 200 (ok).
84 85 86 |
# File 'lib/protocol/http/response.rb', line 84 def ok? @status == 200 end |
#partial? ⇒ Boolean
Whether the status is 206 (partial content).
94 95 96 |
# File 'lib/protocol/http/response.rb', line 94 def partial? @status == 206 end |
#peer ⇒ Object
A response that is generated by a client, may choose to include the peer (address) associated with the response. It should be implemented by a sub-class.
58 59 60 |
# File 'lib/protocol/http/response.rb', line 58 def peer nil end |
#preserve_method? ⇒ Boolean
Whether the status is 307 (temporary redirect) and should preserve the method of the request when following the redirect.
109 110 111 |
# File 'lib/protocol/http/response.rb', line 109 def preserve_method? @status == 307 || @status == 308 end |
#redirection? ⇒ Boolean
Whether the status is considered a redirection.
99 100 101 |
# File 'lib/protocol/http/response.rb', line 99 def redirection? @status and @status >= 300 && @status < 400 end |
#success? ⇒ Boolean
Whether the status is considered successful.
89 90 91 |
# File 'lib/protocol/http/response.rb', line 89 def success? @status and @status >= 200 && @status < 300 end |
#The body, e.g. `"Hello, World!"`, etc.=(body, e.g.`"Hello, World!"`, etc. = (value)) ⇒ Object
50 |
# File 'lib/protocol/http/response.rb', line 50 attr_accessor :body |
"text/html"}`, etc.=-instance_method"> #The headers, e.g. `{"content-type" => "text/html"}`, etc.=(headers, e.g.`{"content-type" = > "text/html"}`, etc. = (value)) ⇒ Object
47 |
# File 'lib/protocol/http/response.rb', line 47 attr_accessor :headers |
#to_ary ⇒ Object
Implicit conversion to an array.
184 185 186 |
# File 'lib/protocol/http/response.rb', line 184 def to_ary return @status, @headers, @body end |
#to_json ⇒ Object
Convert the response to JSON.
170 171 172 |
# File 'lib/protocol/http/response.rb', line 170 def to_json(...) as_json.to_json(...) end |
#to_s ⇒ Object
Summarise the response as a string.
177 178 179 |
# File 'lib/protocol/http/response.rb', line 177 def to_s "#{@status} #{@version}" end |