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.

[View source]

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.

[View source]

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

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

#bodyless?Boolean

returns whether the response contains body payload.

Returns:

  • (Boolean)
[View source]

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

#closeObject

closes the respective @request and @body.

[View source]

83
84
85
86
# File 'lib/httpx/response.rb', line 83

def close
  @request.close
  @body.close
end

#complete?Boolean

Returns:

  • (Boolean)
[View source]

131
132
133
# File 'lib/httpx/response.rb', line 131

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"
[View source]

102
103
104
# File 'lib/httpx/response.rb', line 102

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
[View source]

150
151
152
153
154
# File 'lib/httpx/response.rb', line 150

def error
  return if @status < 400

  HTTPError.new(self)
end

#finish!Object

marks the response as finished, freezes the headers.

[View source]

112
113
114
115
# File 'lib/httpx/response.rb', line 112

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

#finished?Boolean

returns whether the response has been fully fetched.

Returns:

  • (Boolean)
[View source]

107
108
109
# File 'lib/httpx/response.rb', line 107

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”.

[View source]

176
177
178
# File 'lib/httpx/response.rb', line 176

def form
  decode(Transcoder::Form)
end

#initialize_dup(orig) ⇒ Object

dupped initialization

[View source]

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

#inspectObject

:nocov:

[View source]

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

#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
[View source]

170
171
172
# File 'lib/httpx/response.rb', line 170

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

#merge_headers(h) ⇒ Object

merges headers defined in h into the response headers.

[View source]

89
90
91
# File 'lib/httpx/response.rb', line 89

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
[View source]

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

def raise_for_status
  return self unless (err = error)

  raise err
end

#xmlObject

[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