Class: HTTP::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Headers::Mixin
Defined in:
lib/http/response.rb,
lib/http/response/body.rb,
lib/http/response/parser.rb,
lib/http/response/status.rb,
lib/http/response/status/reasons.rb

Defined Under Namespace

Classes: Body, Parser, Status

Instance Attribute Summary collapse

Attributes included from Headers::Mixin

#headers

Instance Method Summary collapse

Methods included from Headers::Mixin

#[], #[]=

Constructor Details

#initialize(opts) ⇒ Response

Inits a new instance

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :status (Integer)

    Status code

  • :version (String)

    HTTP version

  • :headers (Hash)
  • :proxy_headers (Hash)
  • :connection (HTTP::Connection)
  • :encoding (String)

    Encoding to use when reading body

  • :body (String)
  • :uri (String)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/http/response.rb', line 39

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

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

Instance Attribute Details

#bodyBody (readonly)

Returns:



21
22
23
# File 'lib/http/response.rb', line 21

def body
  @body
end

#proxy_headersHash (readonly)

Returns:

  • (Hash)


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

def proxy_headers
  @proxy_headers
end

#statusStatus (readonly)

Returns:



18
19
20
# File 'lib/http/response.rb', line 18

def status
  @status
end

#uriURI? (readonly)

Returns:



24
25
26
# File 'lib/http/response.rb', line 24

def uri
  @uri
end

Instance Method Details

#charsetString?

Charset of response (if any)

Returns:

  • (String, nil)


103
# File 'lib/http/response.rb', line 103

def_delegator :content_type, :charset

#codeFixnum

Returns status code.

Returns:

  • (Fixnum)

    status code



62
# File 'lib/http/response.rb', line 62

def_delegator :status, :code

#content_typeHTTP::ContentType

Parsed Content-Type header

Returns:



91
92
93
# File 'lib/http/response.rb', line 91

def content_type
  @content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE]
end

#cookiesObject



105
106
107
108
109
# File 'lib/http/response.rb', line 105

def cookies
  @cookies ||= headers.each_with_object CookieJar.new do |(k, v), jar|
    jar.parse(v, uri) if k == Headers::SET_COOKIE
  end
end

#flushResponse

Flushes body and returns self-reference

Returns:



83
84
85
86
# File 'lib/http/response.rb', line 83

def flush
  body.to_s
  self
end

#inspectObject

Inspect a response



122
123
124
# File 'lib/http/response.rb', line 122

def inspect
  "#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>"
end

#mime_typeString?

MIME type of response (if any)

Returns:

  • (String, nil)


98
# File 'lib/http/response.rb', line 98

def_delegator :content_type, :mime_type

#parse(as = nil) ⇒ Object

Parse response body with corresponding MIME type adapter.

Parameters:

  • as (#to_s) (defaults to: nil)

    Parse as given MIME type instead of the one determined from headers

Returns:

  • (Object)

Raises:

  • (Error)

    if adapter not found



117
118
119
# File 'lib/http/response.rb', line 117

def parse(as = nil)
  MimeType[as || mime_type].decode to_s
end

#readpartialObject



71
# File 'lib/http/response.rb', line 71

def_delegator :body, :readpartial

#reasonString?

Returns status message.

Returns:

  • (String, nil)

    status message



58
# File 'lib/http/response.rb', line 58

def_delegator :status, :reason

#to_aArray(Fixnum, Hash, String)

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

Returns:

  • (Array(Fixnum, Hash, String))


76
77
78
# File 'lib/http/response.rb', line 76

def to_a
  [status.to_i, headers.to_h, body.to_s]
end

#to_sString Also known as: to_str

Returns eagerly consume the entire body as a string.

Returns:

  • (String)

    eagerly consume the entire body as a string



66
# File 'lib/http/response.rb', line 66

def_delegator :body, :to_s