Class: HTTP::Response
- Inherits:
-
Object
- Object
- HTTP::Response
- Extended by:
- Forwardable
- Includes:
- Headers::Mixin
- Defined in:
- lib/http/response.rb,
lib/http/response/body.rb,
lib/http/response/parser.rb,
lib/http/response/status.rb,
lib/http/response/caching.rb,
lib/http/response/io_body.rb,
lib/http/response/string_body.rb,
lib/http/response/status/reasons.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Body, Caching, IoBody, Parser, Status, StringBody
Constant Summary collapse
- STATUS_CODES =
Deprecated.
Will be removed in 1.0.0 Use Status::REASONS
Status::REASONS
- SYMBOL_TO_STATUS_CODE =
Deprecated.
Will be removed in 1.0.0
Hash[STATUS_CODES.map { |k, v| [v.downcase.gsub(/\s|-/, "_").to_sym, k] }].freeze
Instance Attribute Summary collapse
- #body ⇒ Body readonly
- #status ⇒ Status readonly
- #uri ⇒ URI? readonly
Attributes included from Headers::Mixin
Instance Method Summary collapse
- #caching ⇒ HTTP::Response::Caching
-
#charset ⇒ String?
Charset of response (if any).
-
#code ⇒ Fixnum
(also: #status_code)
Status code.
-
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header.
- #cookies ⇒ Object
-
#flush ⇒ Response
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.
- #readpartial ⇒ Object
-
#reason ⇒ String?
Status message.
-
#to_a ⇒ Array(Fixnum, Hash, String)
Returns an Array ala Rack:
[status, headers, body]
. -
#to_s ⇒ String
(also: #to_str)
Eagerly consume the entire body as a string.
Methods included from Headers::Mixin
Constructor Details
#initialize(status, version, headers, body, uri = nil) ⇒ Response
rubocop:disable ParameterLists
34 35 36 37 38 39 40 |
# File 'lib/http/response.rb', line 34 def initialize(status, version, headers, body, uri = nil) # rubocop:disable ParameterLists @version = version @body = body @uri = uri && HTTP::URI.parse(uri) @status = HTTP::Response::Status.new status @headers = HTTP::Headers.coerce(headers || {}) end |
Instance Attribute Details
Instance Method Details
#caching ⇒ HTTP::Response::Caching
120 121 122 |
# File 'lib/http/response.rb', line 120 def caching Caching.new self end |
#charset ⇒ String?
Charset of response (if any)
94 95 96 |
# File 'lib/http/response.rb', line 94 def charset @charset ||= content_type.charset end |
#code ⇒ Fixnum Also known as: status_code
Returns status code.
48 |
# File 'lib/http/response.rb', line 48 def_delegator :status, :code |
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header
80 81 82 |
# File 'lib/http/response.rb', line 80 def content_type @content_type ||= ContentType.parse headers["Content-Type"] end |
#cookies ⇒ Object
98 99 100 101 102 |
# File 'lib/http/response.rb', line 98 def @cookies ||= headers.each_with_object CookieJar.new do |(k, v), jar| jar.parse(v, uri) if k == Headers::SET_COOKIE end end |
#flush ⇒ Response
Flushes body and returns self-reference
72 73 74 75 |
# File 'lib/http/response.rb', line 72 def flush body.to_s self end |
#inspect ⇒ Object
Inspect a response
115 116 117 |
# File 'lib/http/response.rb', line 115 def inspect "#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>" end |
#mime_type ⇒ String?
MIME type of response (if any)
87 88 89 |
# File 'lib/http/response.rb', line 87 def mime_type @mime_type ||= content_type.mime_type end |
#parse(as = nil) ⇒ Object
Parse response body with corresponding MIME type adapter.
110 111 112 |
# File 'lib/http/response.rb', line 110 def parse(as = nil) MimeType[as || mime_type].decode to_s end |
#readpartial ⇒ Object
60 |
# File 'lib/http/response.rb', line 60 def_delegator :body, :readpartial |
#reason ⇒ String?
Returns status message.
44 |
# File 'lib/http/response.rb', line 44 def_delegator :status, :reason |
#to_a ⇒ Array(Fixnum, Hash, String)
Returns an Array ala Rack: [status, headers, body]
65 66 67 |
# File 'lib/http/response.rb', line 65 def to_a [status.to_i, headers.to_h, body.to_s] end |
#to_s ⇒ String Also known as: to_str
Returns eagerly consume the entire body as a string.
55 |
# File 'lib/http/response.rb', line 55 def_delegator :body, :to_s |