Class: HTTP::Response

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

Defined Under Namespace

Classes: Body, Parser

Constant Summary collapse

STATUS_CODES =
{
  100 => 'Continue',
  101 => 'Switching Protocols',
  102 => 'Processing',
  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  207 => 'Multi-Status',
  226 => 'IM Used',
  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  306 => 'Reserved',
  307 => 'Temporary Redirect',
  400 => 'Bad Request',
  401 => 'Unauthorized',
  402 => 'Payment Required',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  406 => 'Not Acceptable',
  407 => 'Proxy Authentication Required',
  408 => 'Request Timeout',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Request Entity Too Large',
  414 => 'Request-URI Too Long',
  415 => 'Unsupported Media Type',
  416 => 'Requested Range Not Satisfiable',
  417 => 'Expectation Failed',
  418 => "I'm a Teapot",
  422 => 'Unprocessable Entity',
  423 => 'Locked',
  424 => 'Failed Dependency',
  426 => 'Upgrade Required',
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Timeout',
  505 => 'HTTP Version Not Supported',
  506 => 'Variant Also Negotiates',
  507 => 'Insufficient Storage',
  510 => 'Not Extended'
}
SYMBOL_TO_STATUS_CODE =
Hash[STATUS_CODES.map { |code, msg| [msg.downcase.gsub(/\s|-/, '_').to_sym, code]

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



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

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#statusObject (readonly) Also known as: code, status_code

Returns the value of attribute status.



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

def status
  @status
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#charsetString?

Charset of response (if any)

Returns:

  • (String, nil)


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

def charset
  @charset ||= content_type.charset
end

#content_typeHTTP::ContentType

Parsed Content-Type header

Returns:



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

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

#flushObject

Flushes body and returns self-reference



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

def flush
  body.to_s
  self
end

#inspectObject

Inspect a response



133
134
135
# File 'lib/http/response.rb', line 133

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

#mime_typeString?

MIME type of response (if any)

Returns:

  • (String, nil)


112
113
114
# File 'lib/http/response.rb', line 112

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



128
129
130
# File 'lib/http/response.rb', line 128

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

#reasonObject

Obtain the ‘Reason-Phrase’ for the response



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

def reason
  STATUS_CODES[@status]
end

#to_aObject

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



88
89
90
# File 'lib/http/response.rb', line 88

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

#to_sObject Also known as: to_str

Return the response body as a string



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

def to_s
  body.to_s
end