Class: HTTPI::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/httpi/response.rb

Overview

HTTPI::Response

Represents an HTTP response and contains various response details.

Constant Summary collapse

SuccessfulResponseCodes =

Range of HTTP response codes considered to be successful.

200..299
RedirectResponseCodes =

HTTP response codes considered to be a redirect.

[301, 302, 303, 307, 308]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, headers, body) ⇒ Response

Initializer expects an HTTP response code, headers and body.



21
22
23
24
25
# File 'lib/httpi/response.rb', line 21

def initialize(code, headers, body)
  self.code = code.to_i
  self.headers = Rack::Utils::HeaderHash.new(headers)
  self.raw_body = body
end

Instance Attribute Details

#attachmentsObject

Returns any DIME attachments.



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

def attachments
  @attachments
end

#bodyObject

Returns the HTTP response body.



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

def body
  decode_body unless @body
  @body
end

#codeObject

Returns the value of attribute code.



27
28
29
# File 'lib/httpi/response.rb', line 27

def code
  @code
end

#headersObject

Returns the value of attribute headers.



27
28
29
# File 'lib/httpi/response.rb', line 27

def headers
  @headers
end

#raw_bodyObject

Returns the value of attribute raw_body.



27
28
29
# File 'lib/httpi/response.rb', line 27

def raw_body
  @raw_body
end

Instance Method Details

#cookiesObject

Returns a list of cookies from the response.



40
41
42
# File 'lib/httpi/response.rb', line 40

def cookies
  @cookies ||= Cookie.list_from_headers(headers)
end

#error?Boolean

Returns whether the HTTP response is considered successful.

Returns:

  • (Boolean)


30
31
32
# File 'lib/httpi/response.rb', line 30

def error?
  !SuccessfulResponseCodes.include? code.to_i
end

#multipart?Boolean

Returns whether the HTTP response is a multipart response.

Returns:

  • (Boolean)


35
36
37
# File 'lib/httpi/response.rb', line 35

def multipart?
  !!(headers["Content-Type"] =~ /^multipart/i)
end