class HTTPI::Response
HTTPI::Response
¶ ↑
Represents an HTTP response and contains various response details.
Constants
- RedirectResponseCodes
HTTP response codes considered to be a redirect.
- SuccessfulResponseCodes
Range of HTTP response codes considered to be successful.
Attributes
attachments[W]
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 21 def initialize(code, headers, body) self.code = code.to_i self.headers = HTTPI::Utils::Headers.new.merge(headers) self.raw_body = body end
Public Instance Methods
attachments()
click to toggle source
Returns any DIME attachments.
# File lib/httpi/response.rb, line 46 def attachments @body ||= nil decode_body unless @body @attachments ||= [] end
body()
click to toggle source
Returns the HTTP response body.
# File lib/httpi/response.rb, line 53 def body @body ||= nil 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 31 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 36 def multipart? !!(headers["Content-Type"] =~ /^multipart/i) end
Private Instance Methods
decode_body()
click to toggle source
# File lib/httpi/response.rb, line 63 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 91 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 81 def decoded_gzip_body unless gzip = Zlib::GzipReader.new(StringIO.new(raw_body)) raise ArgumentError, "Unable to create Zlib::GzipReader" end 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 76 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 71 def gzipped_response? headers["Content-Encoding"] == "gzip" end