Class: HTTP::Response::Status

Inherits:
Delegator
  • Object
show all
Defined in:
lib/http/response/status.rb,
lib/http/response/status/reasons.rb

Constant Summary collapse

SYMBOLS =

Code to Symbol map

Examples:

Usage


SYMBOLS[400] # => :bad_request
SYMBOLS[414] # => :request_uri_too_long
SYMBOLS[418] # => :im_a_teapot

Returns:

  • (Hash<Fixnum => Symbol>)
Hash[REASONS.map { |k, v| [k, symbolize(v)] }].freeze
SYMBOL_CODES =

Reversed SYMBOLS map.

Examples:

Usage


SYMBOL_CODES[:bad_request]           # => 400
SYMBOL_CODES[:request_uri_too_long]  # => 414
SYMBOL_CODES[:im_a_teapot]           # => 418

Returns:

  • (Hash<Symbol => Fixnum>)
Hash[SYMBOLS.map { |k, v| [v, k] }].freeze
REASONS =

Code to Reason map

Examples:

Usage


REASONS[400] # => "Bad Request"
REASONS[414] # => "Request-URI Too Long"
REASONS[418] # => "I'm a Teapot"

Returns:

  • (Hash<Fixnum => String>)
{
  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',
  308 => 'Permanent 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'
}.each { |_, v| v.freeze }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Status

Returns a new instance of Status.

Parameters:

  • code (#to_i)


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

def initialize(code)
  super __setobj__ code
end

Instance Attribute Details

#codeFixnum (readonly)

Returns status code.

Returns:

  • (Fixnum)

    status code



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

def code
  @code
end

Class Method Details

.coerce(object) ⇒ Status Also known as: []

Coerces given value to Status.

Examples:


Status.coerce(:bad_request) # => Status.new(400)
Status.coerce("400")        # => Status.new(400)
Status.coerce(true)         # => raises HTTP::Error

Parameters:

  • object (Symbol, #to_i)

Returns:

Raises:

  • (Error)

    if coercion is impossible



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/http/response/status.rb', line 20

def coerce(object)
  code = case
         when object.is_a?(String)  then SYMBOL_CODES[symbolize object]
         when object.is_a?(Symbol)  then SYMBOL_CODES[object]
         when object.is_a?(Numeric) then object.to_i
         else                            nil
         end

  return new code if code

  fail Error, "Can't coerce #{object.class}(#{object}) to #{self}"
end

Instance Method Details

#__getobj__Object



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

def __getobj__
  @code
end

#__setobj__(obj) ⇒ Object



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

def __setobj__(obj)
  @code = obj.to_i
end

#inspectObject

Printable version of HTTP Status, surrounded by quote marks, with special characters escaped.

(see String#inspect)



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

def inspect
  "#{code} #{reason}".inspect
end

#reasonString?

Returns status message.

Returns:

  • (String, nil)

    status message

See Also:



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

def reason
  REASONS[code]
end

#symbolizenil, Symbol

Symbolized #reason

Returns:

  • (nil)

    unless code is well-known (see REASONS)

  • (Symbol)


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

def symbolize
  SYMBOLS[code]
end