Class: HTTPX::Response

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

Defined Under Namespace

Classes: Body

Instance Attribute Summary collapse

Instance Method Summary collapse

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.options
  @version = version
  @status = Integer(status)
  @headers = @options.headers_class.new(headers)
  @body = @options.response_body_class.new(self, @options)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

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

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/httpx/response.rb', line 44

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

#complete?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/httpx/response.rb', line 53

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

#content_typeObject



49
50
51
# File 'lib/httpx/response.rb', line 49

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

#errorObject

:nocov:



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

def error
  return if @status < 400

  HTTPError.new(self)
end

#formObject



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

def form
  decode("form")
end

#inspectObject

: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(options = nil)
  decode("json", options)
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_statusObject



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

def raise_for_status
  return self unless (err = error)

  raise err
end