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/caching.rb,
lib/http/response/io_body.rb,
lib/http/response/string_body.rb,
lib/http/response/status/reasons.rb

Direct Known Subclasses

Caching

Defined Under Namespace

Classes: Body, Caching, IoBody, Parser, Status, StringBody

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



32
33
34
35
36
# File 'lib/http/response.rb', line 32

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

Instance Attribute Details

#bodyBody (readonly)

Returns:



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

def body
  @body
end

#statusStatus (readonly)

Returns:



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

def status
  @status
end

#uriURI? (readonly)

Returns:



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

def uri
  @uri
end

Instance Method Details

#cachingHTTP::Response::Caching



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

def caching
  Caching.new self
end

#charsetString?

Charset of response (if any)

Returns:

  • (String, nil)


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

def charset
  @charset ||= content_type.charset
end

#codeFixnum Also known as: status_code

Returns status code.

Returns:

  • (Fixnum)

    status code



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

def_delegator :status, :code

#content_typeHTTP::ContentType

Parsed Content-Type header

Returns:



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

def content_type
  @content_type ||= ContentType.parse headers["Content-Type"]
end

#flushResponse

Flushes body and returns self-reference

Returns:



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

def flush
  body.to_s
  self
end

#inspectObject

Inspect a response



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

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

#mime_typeString?

MIME type of response (if any)

Returns:

  • (String, nil)


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

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



100
101
102
# File 'lib/http/response.rb', line 100

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

#readpartialObject



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

def_delegator :body, :readpartial

#reasonString?

Returns status message.

Returns:

  • (String, nil)

    status message



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

def_delegator :status, :reason

#to_aArray(Fixnum, Hash, String)

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

Returns:

  • (Array(Fixnum, Hash, String))


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

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



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

def_delegator :body, :to_s