Class: HTTPX::Response
- Inherits:
-
Object
- Object
- HTTPX::Response
- Extended by:
- Forwardable
- Includes:
- ResponsePatternMatchExtensions
- Defined in:
- lib/httpx/response.rb
Defined Under Namespace
Classes: Body
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #bodyless? ⇒ Boolean
- #complete? ⇒ Boolean
- #content_type ⇒ Object
-
#error ⇒ Object
:nocov:.
- #form ⇒ Object
-
#initialize(request, status, version, headers) ⇒ Response
constructor
A new instance of Response.
-
#inspect ⇒ Object
:nocov:.
- #json(options = nil) ⇒ Object
- #merge_headers(h) ⇒ Object
- #raise_for_status ⇒ Object
Methods included from ResponsePatternMatchExtensions
#deconstruct, #deconstruct_keys
Constructor Details
#initialize(request, status, version, headers) ⇒ Response
Returns a new instance of Response.
27 28 29 30 31 32 33 34 |
# File 'lib/httpx/response.rb', line 27 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) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/httpx/response.rb', line 13 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
13 14 15 |
# File 'lib/httpx/response.rb', line 13 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
13 14 15 |
# File 'lib/httpx/response.rb', line 13 def status @status end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
13 14 15 |
# File 'lib/httpx/response.rb', line 13 def version @version end |
Instance Method Details
#<<(data) ⇒ Object
40 41 42 |
# File 'lib/httpx/response.rb', line 40 def <<(data) @body.write(data) end |
#bodyless? ⇒ Boolean
44 45 46 47 |
# File 'lib/httpx/response.rb', line 44 def bodyless? @request.verb == :head || no_data? end |
#complete? ⇒ Boolean
53 54 55 |
# File 'lib/httpx/response.rb', line 53 def complete? bodyless? || (@request.verb == :connect && @status == 200) end |
#content_type ⇒ Object
49 50 51 |
# File 'lib/httpx/response.rb', line 49 def content_type @content_type ||= ContentType.new(@headers["content-type"]) end |
#error ⇒ Object
:nocov:
67 68 69 70 71 |
# File 'lib/httpx/response.rb', line 67 def error return if @status < 400 HTTPError.new(self) end |
#form ⇒ Object
83 84 85 |
# File 'lib/httpx/response.rb', line 83 def form decode("form") end |
#inspect ⇒ Object
:nocov:
58 59 60 61 62 63 64 |
# File 'lib/httpx/response.rb', line 58 def inspect "#<Response:#{object_id} "\ "HTTP/#{version} " \ "@status=#{@status} " \ "@headers=#{@headers} " \ "@body=#{@body.bytesize}>" end |
#json(options = nil) ⇒ Object
79 80 81 |
# File 'lib/httpx/response.rb', line 79 def json( = nil) decode("json", ) end |
#merge_headers(h) ⇒ Object
36 37 38 |
# File 'lib/httpx/response.rb', line 36 def merge_headers(h) @headers = @headers.merge(h) end |
#raise_for_status ⇒ Object
73 74 75 76 77 |
# File 'lib/httpx/response.rb', line 73 def raise_for_status return self unless (err = error) raise err end |