class HTTP::Response

Attributes

body[R]

@return [Body]

proxy_headers[R]

@return [Hash]

request[R]

@return [Request]

status[R]

@return [Status]

version[R]

@return [String]

Public Class Methods

new(opts) click to toggle source

Inits a new instance

@option opts [Integer] :status Status code @option opts [String] :version HTTP version @option opts [Hash] :headers @option opts [Hash] :proxy_headers @option opts [HTTP::Connection] :connection @option opts [String] :encoding Encoding to use when reading body @option opts [String] :body @option opts [HTTP::Request] request The request this is in response to. @option opts [String] :uri (DEPRECATED) used to populate a missing request

# File lib/http/response.rb, line 45
def initialize(opts)
  @version       = opts.fetch(:version)
  @request       = init_request(opts)
  @status        = HTTP::Response::Status.new(opts.fetch(:status))
  @headers       = HTTP::Headers.coerce(opts[:headers] || {})
  @proxy_headers = HTTP::Headers.coerce(opts[:proxy_headers] || {})

  if opts.include?(:body)
    @body = opts.fetch(:body)
  else
    connection = opts.fetch(:connection)
    encoding = opts[:encoding] || charset || default_encoding

    @body = Response::Body.new(connection, :encoding => encoding)
  end
end

Public Instance Methods

chunked?() click to toggle source
# File lib/http/response.rb, line 146
def chunked?
  return false unless @headers.include?(Headers::TRANSFER_ENCODING)

  encoding = @headers.get(Headers::TRANSFER_ENCODING)

  # TODO: "chunked" is frozen in the request writer. How about making it accessible?
  encoding.last == "chunked"
end
content_length() click to toggle source

Value of the Content-Length header.

@return [nil] if Content-Length was not given, or it’s value was invalid

(not an integer, e.g. empty string or string with non-digits).

@return [Integer] otherwise

# File lib/http/response.rb, line 107
def content_length
  # http://greenbytes.de/tech/webdav/rfc7230.html#rfc.section.3.3.3
  # Clause 3: "If a message is received with both a Transfer-Encoding
  # and a Content-Length header field, the Transfer-Encoding overrides the Content-Length.
  return nil if @headers.include?(Headers::TRANSFER_ENCODING)

  value = @headers[Headers::CONTENT_LENGTH]
  return nil unless value

  begin
    Integer(value)
  rescue ArgumentError
    nil
  end
end
content_type() click to toggle source

Parsed Content-Type header

@return [HTTP::ContentType]

# File lib/http/response.rb, line 126
def content_type
  @content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE]
end
cookies() click to toggle source
# File lib/http/response.rb, line 140
def cookies
  @cookies ||= headers.get(Headers::SET_COOKIE).each_with_object CookieJar.new do |v, jar|
    jar.parse(v, uri)
  end
end
flush() click to toggle source

Flushes body and returns self-reference

@return [Response]

# File lib/http/response.rb, line 97
def flush
  body.to_s
  self
end
inspect() click to toggle source

Inspect a response

# File lib/http/response.rb, line 165
def inspect
  "#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>"
end
parse(type = nil) click to toggle source

Parse response body with corresponding MIME type adapter.

@param type [#to_s] Parse as given MIME type. @raise (see MimeType.[]) @return [Object]

# File lib/http/response.rb, line 160
def parse(type = nil)
  MimeType[type || mime_type].decode to_s
end
to_a() click to toggle source

Returns an Array ala Rack: ‘[status, headers, body]`

@return [Array(Fixnum, Hash, String)]

# File lib/http/response.rb, line 90
def to_a
  [status.to_i, headers.to_h, body.to_s]
end

Private Instance Methods

default_encoding() click to toggle source
# File lib/http/response.rb, line 171
def default_encoding
  return Encoding::UTF_8 if mime_type == "application/json"

  Encoding::BINARY
end
init_request(opts) click to toggle source

Initialize an HTTP::Request from options.

@return [HTTP::Request]

# File lib/http/response.rb, line 180
def init_request(opts)
  raise ArgumentError, ":uri is for backwards compatibilty and conflicts with :request" \
    if opts[:request] && opts[:uri]

  # For backwards compatibilty
  if opts[:uri]
    HTTP::Request.new(:uri => opts[:uri], :verb => :get)
  else
    opts.fetch(:request)
  end
end