class HTTPI::Response

HTTPI::Response

Represents an HTTP response and contains various response details.

Constants

SuccessfulResponseCodes

Range of HTTP response codes considered to be successful.

Attributes

attachments[RW]
body[W]
code[RW]
headers[RW]
raw_body[RW]

Public Class Methods

new(code, headers, body) click to toggle source

Initializer expects an HTTP response code, headers and body.

# File lib/httpi/response.rb, line 17
def initialize(code, headers, body)
  self.code = code.to_i
  self.headers = Rack::Utils::HeaderHash.new(headers)
  self.raw_body = body
end

Public Instance Methods

body() click to toggle source

Returns the HTTP response body.

# File lib/httpi/response.rb, line 42
def body
  decode_body unless @body
  @body
end
error?() click to toggle source

Returns whether the HTTP response is considered successful.

# File lib/httpi/response.rb, line 26
def error?
  !SuccessfulResponseCodes.include? code.to_i
end
multipart?() click to toggle source

Returns whether the HTTP response is a multipart response.

# File lib/httpi/response.rb, line 31
def multipart?
  !!(headers["Content-Type"] =~ /^multipart/i)
end

Private Instance Methods

decode_body() click to toggle source
# File lib/httpi/response.rb, line 51
def decode_body
  return @body = "" if !raw_body || raw_body.empty?

  body = gzipped_response? ? decoded_gzip_body : raw_body
  @body = dime_response? ? decoded_dime_body(body) : body
end
decoded_dime_body(body = nil) click to toggle source

Returns the DIME decoded response body.

# File lib/httpi/response.rb, line 78
def decoded_dime_body(body = nil)
  dime = Dime.new(body || raw_body)
  self.attachments = dime.binary_records
  dime.xml_records.first.data
end
decoded_gzip_body() click to toggle source

Returns the gzip decoded response body.

# File lib/httpi/response.rb, line 69
def decoded_gzip_body
  gzip = Zlib::GzipReader.new StringIO.new(raw_body)
  raise ArgumentError.new "couldn't create gzip reader" unless gzip
  gzip.read
ensure
  gzip.close if gzip
end
dime_response?() click to toggle source

Returns whether this is a DIME response.

# File lib/httpi/response.rb, line 64
def dime_response?
  headers["Content-Type"] == "application/dime"
end
gzipped_response?() click to toggle source

Returns whether the response is gzipped.

# File lib/httpi/response.rb, line 59
def gzipped_response?
  headers["Content-Encoding"] == "gzip"
end