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/status/reasons.rb
Defined Under Namespace
Instance Attribute Summary collapse
- #body ⇒ Body readonly
- #proxy_headers ⇒ Hash readonly
- #status ⇒ Status readonly
- #uri ⇒ URI? readonly
Attributes included from Headers::Mixin
Instance Method Summary collapse
-
#charset ⇒ String?
Charset of response (if any).
-
#code ⇒ Fixnum
Status code.
-
#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(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(opts) ⇒ Response
Inits a new instance
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/http/response.rb', line 40 def initialize(opts) @version = opts.fetch(:version) @uri = HTTP::URI.parse(opts.fetch(:uri)) if opts.include? :uri @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?(:connection) connection = opts.fetch(:connection) encoding = opts[:encoding] || charset || Encoding::BINARY @body = Response::Body.new(connection, encoding) else @body = opts.fetch(:body) end end |
Instance Attribute Details
#proxy_headers ⇒ Hash (readonly)
28 29 30 |
# File 'lib/http/response.rb', line 28 def proxy_headers @proxy_headers end |
Instance Method Details
#charset ⇒ String?
Charset of response (if any)
120 |
# File 'lib/http/response.rb', line 120 def_delegator :content_type, :charset |
#code ⇒ Fixnum
Returns status code.
63 |
# File 'lib/http/response.rb', line 63 def_delegator :status, :code |
#content_length ⇒ nil, Integer
Value of the Content-Length header.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/http/response.rb', line 94 def content_length value = @headers[Headers::CONTENT_LENGTH] return unless value begin Integer(value) rescue ArgumentError nil end end |
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header
108 109 110 |
# File 'lib/http/response.rb', line 108 def content_type @content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE] end |
#cookies ⇒ Object
122 123 124 125 126 |
# File 'lib/http/response.rb', line 122 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
84 85 86 87 |
# File 'lib/http/response.rb', line 84 def flush body.to_s self end |
#inspect ⇒ Object
Inspect a response
139 140 141 |
# File 'lib/http/response.rb', line 139 def inspect "#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>" end |
#mime_type ⇒ String?
MIME type of response (if any)
115 |
# File 'lib/http/response.rb', line 115 def_delegator :content_type, :mime_type |
#parse(as = nil) ⇒ Object
Parse response body with corresponding MIME type adapter.
134 135 136 |
# File 'lib/http/response.rb', line 134 def parse(as = nil) MimeType[as || mime_type].decode to_s end |
#readpartial ⇒ Object
72 |
# File 'lib/http/response.rb', line 72 def_delegator :body, :readpartial |
#reason ⇒ String?
Returns status message.
59 |
# File 'lib/http/response.rb', line 59 def_delegator :status, :reason |
#to_a ⇒ Array(Fixnum, Hash, String)
Returns an Array ala Rack: [status, headers, body]
77 78 79 |
# File 'lib/http/response.rb', line 77 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.
67 |
# File 'lib/http/response.rb', line 67 def_delegator :body, :to_s |