class HTTP::Response

Attributes

body[R]

@return [Body]

proxy_headers[R]

@return [Hash]

status[R]

@return [Status]

uri[R]

@return [URI, nil]

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 [String] :uri

# File lib/http/response.rb, line 41
def initialize(opts)
  @version       = opts.fetch(:version)
  @uri           = HTTP::URI.parse(opts.fetch(:uri)) if opts.include? :uri
  @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?(:connection)
    connection = opts.fetch(:connection)
    encoding   = opts[:encoding] || charset || Encoding::BINARY
    stream     = body_stream_for(connection, opts)

    @body = Response::Body.new(stream, encoding)
  else
    @body = opts.fetch(:body)
  end
end

Public Instance Methods

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 100
def content_length
  value = @headers[Headers::CONTENT_LENGTH]
  return 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 114
def content_type
  @content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE]
end
cookies() click to toggle source
# File lib/http/response.rb, line 128
def cookies
  @cookies ||= headers.each_with_object CookieJar.new do |(k, v), jar|
    jar.parse(v, uri) if k == Headers::SET_COOKIE
  end
end
flush() click to toggle source

Flushes body and returns self-reference

@return [Response]

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

Inspect a response

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

Parse response body with corresponding MIME type adapter.

@param [#to_s] as Parse as given MIME type

instead of the one determined from headers

@raise [HTTP::Error] if adapter not found @return [Object]

# File lib/http/response.rb, line 140
def parse(as = nil)
  MimeType[as || 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 83
def to_a
  [status.to_i, headers.to_h, body.to_s]
end

Private Instance Methods

body_stream_for(connection, opts) click to toggle source
# File lib/http/response.rb, line 151
def body_stream_for(connection, opts)
  if opts[:auto_inflate]
    opts[:auto_inflate].stream_for(connection, self)
  else
    connection
  end
end