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/inflater.rb,
lib/http/response/status/reasons.rb
Defined Under Namespace
Classes: Body, Inflater, Parser, Status
Instance Attribute Summary collapse
-
#body ⇒ Body
readonly
-
#proxy_headers ⇒ Hash
readonly
-
#request ⇒ Request
readonly
-
#status ⇒ Status
readonly
-
#version ⇒ String
readonly
Attributes included from Headers::Mixin
Instance Method Summary collapse
-
#charset ⇒ String?
Charset of response (if any).
-
#chunked? ⇒ Boolean
-
#code ⇒ Fixnum
Status code.
-
#connection ⇒ HTTP::Connection
The connection object used to make the corresponding request.
-
#content_length ⇒ nil, Integer
Value of the Content-Length header.
-
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header.
-
#cookies ⇒ Object
-
#flush ⇒ Response
Flushes body and returns self-reference.
-
#initialize(opts) ⇒ Response
constructor
Inits a new instance.
-
#inspect ⇒ Object
Inspect a response.
-
#mime_type ⇒ String?
MIME type of response (if any).
-
#parse(type = 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.
-
#uri ⇒ Object
Methods included from Headers::Mixin
Constructor Details
#initialize(opts) ⇒ Response
Inits a new instance
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/http/response.rb', line 45 def initialize(opts) @version = opts.fetch(:version) @request = init_request(opts) @status = HTTP::Response::Status.new(opts.fetch(:status)) @headers = HTTP::Headers.coerce(opts[:headers] || {}) @proxy_headers = HTTP::Headers.coerce(opts[:proxy_headers] || {}) if opts.include?(:body) @body = opts.fetch(:body) else connection = opts.fetch(:connection) encoding = opts[:encoding] || charset || default_encoding @body = Response::Body.new(connection, encoding: encoding) end end |
Instance Attribute Details
#proxy_headers ⇒ Hash (readonly)
32 33 34 |
# File 'lib/http/response.rb', line 32 def proxy_headers @proxy_headers end |
#request ⇒ Request (readonly)
29 30 31 |
# File 'lib/http/response.rb', line 29 def request @request end |
#version ⇒ String (readonly)
23 24 25 |
# File 'lib/http/response.rb', line 23 def version @version end |
Instance Method Details
#charset ⇒ String?
Charset of response (if any)
138 |
# File 'lib/http/response.rb', line 138 def_delegator :content_type, :charset |
#chunked? ⇒ Boolean
146 147 148 149 150 151 152 153 |
# File 'lib/http/response.rb', line 146 def chunked? return false unless @headers.include?(Headers::TRANSFER_ENCODING) encoding = @headers.get(Headers::TRANSFER_ENCODING) # TODO: "chunked" is frozen in the request writer. How about making it accessible? encoding.last == "chunked" end |
#code ⇒ Fixnum
Returns status code.
68 |
# File 'lib/http/response.rb', line 68 def_delegator :@status, :code |
#connection ⇒ HTTP::Connection
The connection object used to make the corresponding request.
81 |
# File 'lib/http/response.rb', line 81 def_delegator :@body, :connection |
#content_length ⇒ nil, Integer
Value of the Content-Length header.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/http/response.rb', line 107 def content_length # http://greenbytes.de/tech/webdav/rfc7230.html#rfc.section.3.3.3 # Clause 3: "If a message is received with both a Transfer-Encoding # and a Content-Length header field, the Transfer-Encoding overrides the Content-Length. return nil if @headers.include?(Headers::TRANSFER_ENCODING) value = @headers[Headers::CONTENT_LENGTH] return nil unless value begin Integer(value) rescue ArgumentError nil end end |
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header
126 127 128 |
# File 'lib/http/response.rb', line 126 def content_type @content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE] end |
#cookies ⇒ Object
140 141 142 143 144 |
# File 'lib/http/response.rb', line 140 def @cookies ||= headers.get(Headers::SET_COOKIE).each_with_object CookieJar.new do |v, jar| jar.parse(v, uri) end end |
#flush ⇒ Response
Flushes body and returns self-reference
97 98 99 100 |
# File 'lib/http/response.rb', line 97 def flush body.to_s self end |
#inspect ⇒ Object
Inspect a response
165 166 167 |
# File 'lib/http/response.rb', line 165 def inspect "#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>" end |
#mime_type ⇒ String?
MIME type of response (if any)
133 |
# File 'lib/http/response.rb', line 133 def_delegator :content_type, :mime_type |
#parse(type = nil) ⇒ Object
Parse response body with corresponding MIME type adapter.
160 161 162 |
# File 'lib/http/response.rb', line 160 def parse(type = nil) MimeType[type || mime_type].decode to_s end |
#readpartial ⇒ Object
77 |
# File 'lib/http/response.rb', line 77 def_delegator :@body, :readpartial |
#reason ⇒ String?
Returns status message.
64 |
# File 'lib/http/response.rb', line 64 def_delegator :@status, :reason |
#to_a ⇒ Array(Fixnum, Hash, String)
Returns an Array ala Rack: [status, headers, body]
90 91 92 |
# File 'lib/http/response.rb', line 90 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.
72 |
# File 'lib/http/response.rb', line 72 def_delegator :@body, :to_s |
#uri ⇒ Object
85 |
# File 'lib/http/response.rb', line 85 def_delegator :@request, :uri |