Class: HTTPX::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Callbacks, 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

Methods included from Callbacks

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

Constructor Details

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

Returns a new instance of Response.



28
29
30
31
32
33
34
35
36
# File 'lib/httpx/response.rb', line 28

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?
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#<<(data) ⇒ Object



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

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

#bodyless?Boolean

Returns:

  • (Boolean)


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

def bodyless?
  @request.verb == "HEAD" ||
    no_data?
end

#complete?Boolean

Returns:

  • (Boolean)


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

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

#content_typeObject



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

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

#errorObject

:nocov:



78
79
80
81
82
# File 'lib/httpx/response.rb', line 78

def error
  return if @status < 400

  HTTPError.new(self)
end

#finish!Object



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

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

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @finished
end

#formObject



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

def form
  decode(Transcoder::Form)
end

#inspectObject

:nocov:



69
70
71
72
73
74
75
# File 'lib/httpx/response.rb', line 69

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

#json(*args) ⇒ Object



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

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

#merge_headers(h) ⇒ Object



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

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

#raise_for_statusObject



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

def raise_for_status
  return self unless (err = error)

  raise err
end

#xmlObject



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

def xml
  decode(Transcoder::Xml)
end