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

Constant Summary collapse

STATUS_CODES =
Deprecated.

Will be removed in 1.0.0 Use Status::REASONS

Status::REASONS
SYMBOL_TO_STATUS_CODE =
Deprecated.

Will be removed in 1.0.0

Hash[STATUS_CODES.map { |k, v| [v.downcase.gsub(/\s|-/, "_").to_sym, k] }].freeze

Instance Attribute Summary collapse

Attributes included from Headers::Mixin

#headers

Instance Method Summary collapse

Methods included from Headers::Mixin

#[], #[]=

Constructor Details

#initialize(status, version, headers, body, uri = nil) ⇒ Response

rubocop:disable ParameterLists



33
34
35
36
37
38
39
# File 'lib/http/response.rb', line 33

def initialize(status, version, headers, body, uri = nil) # rubocop:disable ParameterLists
  @version = version
  @body    = body
  @uri     = uri && HTTP::URI.parse(uri)
  @status  = HTTP::Response::Status.new status
  @headers = HTTP::Headers.coerce(headers || {})
end

Instance Attribute Details

#bodyBody (readonly)

Returns:



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

def body
  @body
end

#statusStatus (readonly)

Returns:



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

def status
  @status
end

#uriURI? (readonly)

Returns:



31
32
33
# File 'lib/http/response.rb', line 31

def uri
  @uri
end

Instance Method Details

#charsetString?

Charset of response (if any)

Returns:

  • (String, nil)


93
94
95
# File 'lib/http/response.rb', line 93

def charset
  @charset ||= content_type.charset
end

#codeFixnum Also known as: status_code

Returns status code.

Returns:

  • (Fixnum)

    status code



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

def_delegator :status, :code

#content_typeHTTP::ContentType

Parsed Content-Type header

Returns:



79
80
81
# File 'lib/http/response.rb', line 79

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

#cookiesObject



97
98
99
100
101
# File 'lib/http/response.rb', line 97

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:



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

def flush
  body.to_s
  self
end

#inspectObject

Inspect a response



114
115
116
# File 'lib/http/response.rb', line 114

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

#mime_typeString?

MIME type of response (if any)

Returns:

  • (String, nil)


86
87
88
# File 'lib/http/response.rb', line 86

def mime_type
  @mime_type ||= content_type.mime_type
end

#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



109
110
111
# File 'lib/http/response.rb', line 109

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

#readpartialObject



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

def_delegator :body, :readpartial

#reasonString?

Returns status message.

Returns:

  • (String, nil)

    status message



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

def_delegator :status, :reason

#to_aArray(Fixnum, Hash, String)

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

Returns:

  • (Array(Fixnum, Hash, String))


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

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



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

def_delegator :body, :to_s