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/status.rb,
lib/http/response/parser.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

Constructor Details

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

rubocop:disable ParameterLists


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

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

#bodyObject (readonly)

Returns the value of attribute body


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

def body
  @body
end

#statusObject (readonly)

Returns the value of attribute status


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

def status
  @status
end

#uriObject (readonly)

Returns the value of attribute uri


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

def uri
  @uri
end

Instance Method Details

#charsetString?

Charset of response (if any)

Returns:

  • (String, nil)

81
82
83
# File 'lib/http/response.rb', line 81

def charset
  @charset ||= content_type.charset
end

#content_typeHTTP::ContentType

Parsed Content-Type header

Returns:


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

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

#flushResponse

Flushes body and returns self-reference

Returns:


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

def flush
  body.to_s
  self
end

#inspectObject

Inspect a response


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

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

#mime_typeString?

MIME type of response (if any)

Returns:

  • (String, nil)

74
75
76
# File 'lib/http/response.rb', line 74

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


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

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

#to_aArray(Fixnum, Hash, String)

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

Returns:

  • (Array(Fixnum, Hash, String))

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

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

#to_sString Also known as: to_str

Return the response body as a string

Returns:

  • (String)

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

def to_s
  body.to_s
end