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, implements (indirectly, via the body) the IO write protocol to internally buffer payloads, 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, #only

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.



52
53
54
55
56
57
58
59
60
61
# File 'lib/httpx/response.rb', line 52

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.



25
26
27
# File 'lib/httpx/response.rb', line 25

def body
  @body
end

#headersObject (readonly)

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



22
23
24
# File 'lib/httpx/response.rb', line 22

def headers
  @headers
end

#statusObject (readonly)

the HTTP response status code



19
20
21
# File 'lib/httpx/response.rb', line 19

def status
  @status
end

#versionObject (readonly)

The HTTP protocol version used to fetch the response.



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

def version
  @version
end

Instance Method Details

#<<(data) ⇒ Object

writes data chunk into the response body.



69
70
71
# File 'lib/httpx/response.rb', line 69

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

#bodyless?Boolean

returns whether the response contains body payload.

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/httpx/response.rb', line 93

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

#complete?Boolean

Returns:

  • (Boolean)


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

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"


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

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


125
126
127
128
129
# File 'lib/httpx/response.rb', line 125

def error
  return if @status < 400

  HTTPError.new(self)
end

#finish!Object

marks the response as finished, freezes the headers.



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

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

#finished?Boolean

returns whether the response has been fully fetched.

Returns:

  • (Boolean)


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

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



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

def form
  decode(Transcoder::Form)
end

#inspectObject

:nocov:



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

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


145
146
147
# File 'lib/httpx/response.rb', line 145

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

#merge_headers(h) ⇒ Object

merges headers defined in h into the response headers.



64
65
66
# File 'lib/httpx/response.rb', line 64

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


135
136
137
138
139
# File 'lib/httpx/response.rb', line 135

def raise_for_status
  return self unless (err = error)

  raise err
end

#xmlObject

decodes the response payload into a Nokogiri::XML::Node object if the payload is valid “application/xml” (requires the “nokogiri” gem).



157
158
159
# File 'lib/httpx/response.rb', line 157

def xml
  decode(Transcoder::Xml)
end