Class: HTTP::Response
- Inherits:
-
Object
- Object
- HTTP::Response
- Includes:
- Headers::Mixin
- Defined in:
- lib/http/response.rb,
lib/http/response/body.rb,
lib/http/response/parser.rb
Defined Under Namespace
Constant Summary collapse
- STATUS_CODES =
{ 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-Status', 226 => 'IM Used', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => 'Reserved', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 418 => "I'm a Teapot", 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 426 => 'Upgrade Required', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 510 => 'Not Extended' }
- SYMBOL_TO_STATUS_CODE =
Hash[STATUS_CODES.map { |code, msg| [msg.downcase.gsub(/\s|-/, '_').to_sym, code]
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#status ⇒ Object
(also: #code, #status_code)
readonly
Returns the value of attribute status.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes included from Headers::Mixin
Instance Method Summary collapse
-
#charset ⇒ String?
Charset of response (if any).
-
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header.
-
#flush ⇒ Object
Flushes body and returns self-reference.
-
#initialize(status, version, headers, body, uri = nil) ⇒ Response
constructor
rubocop:disable ParameterLists.
-
#inspect ⇒ Object
Inspect a response.
-
#mime_type ⇒ String?
MIME type of response (if any).
-
#parse(as = nil) ⇒ Object
Parse response body with corresponding MIME type adapter.
-
#reason ⇒ Object
Obtain the ‘Reason-Phrase’ for the response.
-
#to_a ⇒ Object
Returns an Array ala Rack: ‘[status, headers, body]`.
-
#to_s ⇒ Object
(also: #to_str)
Return the response body as a string.
Constructor Details
#initialize(status, version, headers, body, uri = nil) ⇒ Response
rubocop:disable ParameterLists
77 78 79 80 |
# File 'lib/http/response.rb', line 77 def initialize(status, version, headers, body, uri = nil) # rubocop:disable ParameterLists @status, @version, @body, @uri = status, version, body, uri @headers = HTTP::Headers.coerce(headers || {}) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
70 71 72 |
# File 'lib/http/response.rb', line 70 def body @body end |
#status ⇒ Object (readonly) Also known as: code, status_code
Returns the value of attribute status.
69 70 71 |
# File 'lib/http/response.rb', line 69 def status @status end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
71 72 73 |
# File 'lib/http/response.rb', line 71 def uri @uri end |
Instance Method Details
#charset ⇒ String?
Charset of response (if any)
118 119 120 |
# File 'lib/http/response.rb', line 118 def charset @charset ||= content_type.charset end |
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header
106 107 108 |
# File 'lib/http/response.rb', line 106 def content_type @content_type ||= ContentType.parse headers['Content-Type'] end |
#flush ⇒ Object
Flushes body and returns self-reference
99 100 101 102 |
# File 'lib/http/response.rb', line 99 def flush body.to_s self end |
#inspect ⇒ Object
Inspect a response
133 134 135 |
# File 'lib/http/response.rb', line 133 def inspect "#<#{self.class}/#{@version} #{status} #{reason} headers=#{headers.inspect}>" end |
#mime_type ⇒ String?
MIME type of response (if any)
112 113 114 |
# File 'lib/http/response.rb', line 112 def mime_type @mime_type ||= content_type.mime_type end |
#parse(as = nil) ⇒ Object
Parse response body with corresponding MIME type adapter.
128 129 130 |
# File 'lib/http/response.rb', line 128 def parse(as = nil) MimeType[as || mime_type].decode to_s end |
#reason ⇒ Object
Obtain the ‘Reason-Phrase’ for the response
83 84 85 |
# File 'lib/http/response.rb', line 83 def reason STATUS_CODES[@status] end |
#to_a ⇒ Object
Returns an Array ala Rack: ‘[status, headers, body]`
88 89 90 |
# File 'lib/http/response.rb', line 88 def to_a [status, headers.to_h, body.to_s] end |
#to_s ⇒ Object Also known as: to_str
Return the response body as a string
93 94 95 |
# File 'lib/http/response.rb', line 93 def to_s body.to_s end |