Class: HTTPX::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/response.rb

Defined Under Namespace

Classes: Body

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, status, version, headers) ⇒ Response

Returns a new instance of Response.



24
25
26
27
28
29
30
31
32
# File 'lib/httpx/response.rb', line 24

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, threshold_size: @options.body_threshold_size,
                                                 window_size: @options.window_size)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/httpx/response.rb', line 12

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/httpx/response.rb', line 12

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/httpx/response.rb', line 12

def status
  @status
end

#versionObject (readonly)

Returns the value of attribute version.



12
13
14
# File 'lib/httpx/response.rb', line 12

def version
  @version
end

Instance Method Details

#<<(data) ⇒ Object



38
39
40
# File 'lib/httpx/response.rb', line 38

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

#bodyless?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/httpx/response.rb', line 42

def bodyless?
  @request.verb == :head ||
    no_data?
end

#complete?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/httpx/response.rb', line 51

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

#content_typeObject



47
48
49
# File 'lib/httpx/response.rb', line 47

def content_type
  ContentType.parse(@headers["content-type"])
end

#inspectObject



55
56
57
# File 'lib/httpx/response.rb', line 55

def inspect
  "#<Response:#{object_id} @status=#{@status} @headers=#{@headers}>"
end

#merge_headers(h) ⇒ Object



34
35
36
# File 'lib/httpx/response.rb', line 34

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

#raise_for_statusObject

Raises:



59
60
61
62
63
# File 'lib/httpx/response.rb', line 59

def raise_for_status
  return if @status < 400

  raise HTTPError, self
end