Class: HTTPX::Response
- Inherits:
-
Object
- Object
- HTTPX::Response
- Extended by:
- Forwardable
- Includes:
- Callbacks, ResponsePatternMatchExtensions
- Defined in:
- lib/httpx/response.rb
Overview
Defines a HTTP response is handled internally, with a few properties exposed as attributes.
It delegates the following methods to the corresponding HTTPX::Request:
-
HTTPX::Request#uri
-
HTTPX::Request#peer_address
It implements (indirectly, via the body
) the IO write protocol to internally buffer payloads.
It implements the IO reader protocol in order for users to buffer/stream it, acts as an enumerable (of payload chunks).
Defined Under Namespace
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
a HTTPX::Response::Body object wrapping the response body.
-
#headers ⇒ Object
readonly
an HTTPX::Headers object containing the response HTTP headers.
-
#status ⇒ Object
readonly
the HTTP response status code.
-
#version ⇒ Object
readonly
The HTTP protocol version used to fetch the response.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
writes
data
chunk into the response body. -
#bodyless? ⇒ Boolean
returns whether the response contains body payload.
-
#close ⇒ Object
closes the respective @request and @body.
- #complete? ⇒ Boolean
-
#content_type ⇒ Object
returns the HTTPX::ContentType for the response, as per what’s declared in the content-type header.
-
#error ⇒ Object
returns an instance of HTTPX::HTTPError if the response has a 4xx or 5xx status code, or nothing.
-
#finish! ⇒ Object
marks the response as finished, freezes the headers.
-
#finished? ⇒ Boolean
returns whether the response has been fully fetched.
-
#form ⇒ Object
decodes the response payload into a ruby object if the payload is valid “application/x-www-urlencoded” or “multipart/form-data”.
-
#initialize(request, status, version, headers) ⇒ Response
constructor
inits the instance with the corresponding
request
to this response, an the response HTTPstatus
,version
and HTTPX::Headers instance ofheaders
. -
#initialize_dup(orig) ⇒ Object
dupped initialization.
-
#inspect ⇒ Object
:nocov:.
-
#json(*args) ⇒ Object
decodes the response payload into a ruby object if the payload is valid json.
-
#merge_headers(h) ⇒ Object
merges headers defined in
h
into the response headers. -
#raise_for_status ⇒ Object
it raises the exception returned by
error
, or itself otherwise. - #xml ⇒ Object
Methods included from ResponsePatternMatchExtensions
#deconstruct, #deconstruct_keys
Methods included from Callbacks
#callbacks_for?, #emit, #on, #once
Constructor Details
permalink #initialize(request, status, version, headers) ⇒ Response
inits the instance with the corresponding request
to this response, an the response HTTP status
, version
and HTTPX::Headers instance of headers
.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/httpx/response.rb', line 63 def initialize(request, status, version, headers) @request = request @options = request. @version = version @status = Integer(status) @headers = @options.headers_class.new(headers) @body = @options.response_body_class.new(self, @options) @finished = complete? @content_type = nil end |
Instance Attribute Details
permalink #body ⇒ Object (readonly)
a HTTPX::Response::Body object wrapping the response body. The following methods are delegated to it:
-
HTTPX::Response::Body#to_s
-
HTTPX::Response::Body#to_str
-
HTTPX::Response::Body#read
-
HTTPX::Response::Body#copy_to
-
HTTPX::Response::Body#close
39 40 41 |
# File 'lib/httpx/response.rb', line 39 def body @body end |
permalink #headers ⇒ Object (readonly)
an HTTPX::Headers object containing the response HTTP headers.
30 31 32 |
# File 'lib/httpx/response.rb', line 30 def headers @headers end |
permalink #status ⇒ Object (readonly)
the HTTP response status code
27 28 29 |
# File 'lib/httpx/response.rb', line 27 def status @status end |
permalink #version ⇒ Object (readonly)
The HTTP protocol version used to fetch the response.
42 43 44 |
# File 'lib/httpx/response.rb', line 42 def version @version end |
Instance Method Details
permalink #<<(data) ⇒ Object
writes data
chunk into the response body.
94 95 96 |
# File 'lib/httpx/response.rb', line 94 def <<(data) @body.write(data) end |
permalink #bodyless? ⇒ Boolean
returns whether the response contains body payload.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/httpx/response.rb', line 118 def bodyless? @request.verb == "HEAD" || @status < 200 || # informational response @status == 204 || @status == 205 || @status == 304 || begin content_length = @headers["content-length"] return false if content_length.nil? content_length == "0" end end |
permalink #close ⇒ Object
closes the respective @request and @body.
83 84 85 86 |
# File 'lib/httpx/response.rb', line 83 def close @request.close @body.close end |
permalink #complete? ⇒ Boolean
131 132 133 |
# File 'lib/httpx/response.rb', line 131 def complete? bodyless? || (@request.verb == "CONNECT" && @status == 200) end |
permalink #content_type ⇒ Object
returns the HTTPX::ContentType for the response, as per what’s declared in the content-type header.
response.content_type #=> #<HTTPX::ContentType:xxx @header_value="text/plain">
response.content_type.mime_type #=> "text/plain"
102 103 104 |
# File 'lib/httpx/response.rb', line 102 def content_type @content_type ||= ContentType.new(@headers["content-type"]) end |
permalink #error ⇒ Object
returns an instance of HTTPX::HTTPError if the response has a 4xx or 5xx status code, or nothing.
ok_response.error #=> nil
not_found_response.error #=> HTTPX::HTTPError instance, status 404
150 151 152 153 154 |
# File 'lib/httpx/response.rb', line 150 def error return if @status < 400 HTTPError.new(self) end |
permalink #finish! ⇒ Object
marks the response as finished, freezes the headers.
112 113 114 115 |
# File 'lib/httpx/response.rb', line 112 def finish! @finished = true @headers.freeze end |
permalink #finished? ⇒ Boolean
returns whether the response has been fully fetched.
107 108 109 |
# File 'lib/httpx/response.rb', line 107 def finished? @finished end |
permalink #form ⇒ Object
decodes the response payload into a ruby object if the payload is valid “application/x-www-urlencoded” or “multipart/form-data”.
176 177 178 |
# File 'lib/httpx/response.rb', line 176 def form decode(Transcoder::Form) end |
permalink #initialize_dup(orig) ⇒ Object
dupped initialization
75 76 77 78 79 80 |
# File 'lib/httpx/response.rb', line 75 def initialize_dup(orig) super # if a response gets dupped, the body handle must also get dupped to prevent # two responses from using the same file handle to read. @body = orig.body.dup end |
permalink #inspect ⇒ Object
:nocov:
136 137 138 139 140 141 142 |
# File 'lib/httpx/response.rb', line 136 def inspect "#<#{self.class}:#{object_id} " \ "HTTP/#{version} " \ "@status=#{@status} " \ "@headers=#{@headers} " \ "@body=#{@body.bytesize}>" end |
permalink #json(*args) ⇒ Object
decodes the response payload into a ruby object if the payload is valid json.
response.json #≈> { "foo" => "bar" } for "{\"foo\":\"bar\"}" payload
response.json(symbolize_names: true) #≈> { foo: "bar" } for "{\"foo\":\"bar\"}" payload
170 171 172 |
# File 'lib/httpx/response.rb', line 170 def json(*args) decode(Transcoder::JSON, *args) end |
permalink #merge_headers(h) ⇒ Object
merges headers defined in h
into the response headers.
89 90 91 |
# File 'lib/httpx/response.rb', line 89 def merge_headers(h) @headers = @headers.merge(h) end |
permalink #raise_for_status ⇒ Object
it raises the exception returned by error
, or itself otherwise.
ok_response.raise_for_status #=> ok_response
not_found_response.raise_for_status #=> raises HTTPX::HTTPError exception
160 161 162 163 164 |
# File 'lib/httpx/response.rb', line 160 def raise_for_status return self unless (err = error) raise err end |
permalink #xml ⇒ Object
[View source]
180 181 182 183 184 185 186 |
# File 'lib/httpx/response.rb', line 180 def xml # TODO: remove at next major version. warn "DEPRECATION WARNING: calling `.#{__method__}` on plain HTTPX responses is deprecated. " \ "Use HTTPX.plugin(:xml) sessions and call `.#{__method__}` in its responses instead." require "httpx/plugins/xml" decode(Plugins::XML::Transcoder) end |