Class: HTTPX::Response

Inherits:
Object
  • Object
show all
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

Classes: Body, Buffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResponsePatternMatchExtensions

#deconstruct, #deconstruct_keys

Methods included from Callbacks

#callbacks_for?, #emit, #on, #once

Constructor Details

#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.options
  @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

#bodyObject (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

#headersObject (readonly)

an HTTPX::Headers object containing the response HTTP headers.


30
31
32
# File 'lib/httpx/response.rb', line 30

def headers
  @headers
end

#statusObject (readonly)

the HTTP response status code


27
28
29
# File 'lib/httpx/response.rb', line 27

def status
  @status
end

#versionObject (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

#<<(data) ⇒ Object

writes data chunk into the response body.


86
87
88
# File 'lib/httpx/response.rb', line 86

def <<(data)
  @body.write(data)
end

#bodyless?Boolean

returns whether the response contains body payload.

Returns:

  • (Boolean)

110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/httpx/response.rb', line 110

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

#closeObject

closes the respective @request and @body.


75
76
77
78
# File 'lib/httpx/response.rb', line 75

def close
  @request.close
  @body.close
end

#complete?Boolean

Returns:

  • (Boolean)

123
124
125
# File 'lib/httpx/response.rb', line 123

def complete?
  bodyless? || (@request.verb == "CONNECT" && @status == 200)
end

#content_typeObject

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"

94
95
96
# File 'lib/httpx/response.rb', line 94

def content_type
  @content_type ||= ContentType.new(@headers["content-type"])
end

#errorObject

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

142
143
144
145
146
# File 'lib/httpx/response.rb', line 142

def error
  return if @status < 400

  HTTPError.new(self)
end

#finish!Object

marks the response as finished, freezes the headers.


104
105
106
107
# File 'lib/httpx/response.rb', line 104

def finish!
  @finished = true
  @headers.freeze
end

#finished?Boolean

returns whether the response has been fully fetched.

Returns:

  • (Boolean)

99
100
101
# File 'lib/httpx/response.rb', line 99

def finished?
  @finished
end

#formObject

decodes the response payload into a ruby object if the payload is valid “application/x-www-urlencoded” or “multipart/form-data”.


168
169
170
# File 'lib/httpx/response.rb', line 168

def form
  decode(Transcoder::Form)
end

#inspectObject

:nocov:


128
129
130
131
132
133
134
# File 'lib/httpx/response.rb', line 128

def inspect
  "#<Response:#{object_id} " \
    "HTTP/#{version} " \
    "@status=#{@status} " \
    "@headers=#{@headers} " \
    "@body=#{@body.bytesize}>"
end

#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

162
163
164
# File 'lib/httpx/response.rb', line 162

def json(*args)
  decode(Transcoder::JSON, *args)
end

#merge_headers(h) ⇒ Object

merges headers defined in h into the response headers.


81
82
83
# File 'lib/httpx/response.rb', line 81

def merge_headers(h)
  @headers = @headers.merge(h)
end

#raise_for_statusObject

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

152
153
154
155
156
# File 'lib/httpx/response.rb', line 152

def raise_for_status
  return self unless (err = error)

  raise err
end

#xmlObject


172
173
174
175
176
177
178
# File 'lib/httpx/response.rb', line 172

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