Class: HTTP::Response::Status

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/http/response/status.rb,
lib/http/response/status/reasons.rb

Overview

Represents an HTTP response status code with reason phrase

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>)
REASONS.transform_values { |v| 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>)
SYMBOLS.to_h { |k, v| [v, k] }.freeze
REASONS =

Code to Reason map

Examples:

Usage


REASONS[400] # => "Bad Request"
REASONS[414] # => "Request-URI Too Long"

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",
  208 => "Already Reported",
  226 => "IM Used",
  300 => "Multiple Choices",
  301 => "Moved Permanently",
  302 => "Found",
  303 => "See Other",
  304 => "Not Modified",
  305 => "Use Proxy",
  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 => "Payload Too Large",
  414 => "URI Too Long",
  415 => "Unsupported Media Type",
  416 => "Range Not Satisfiable",
  417 => "Expectation Failed",
  421 => "Misdirected Request",
  422 => "Unprocessable Entity",
  423 => "Locked",
  424 => "Failed Dependency",
  426 => "Upgrade Required",
  428 => "Precondition Required",
  429 => "Too Many Requests",
  431 => "Request Header Fields Too Large",
  451 => "Unavailable For Legal Reasons",
  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",
  508 => "Loop Detected",
  510 => "Not Extended",
  511 => "Network Authentication Required"
}.each_value(&:freeze).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Status

Create a new Status from a value that responds to #to_i

Examples:

Status.new(200)

Parameters:

Raises:

  • (TypeError)


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

def initialize(obj)
  raise TypeError, "Expected #{obj.inspect} to respond to #to_i" unless obj.respond_to?(:to_i)

  @code = obj.to_i
end

Instance Attribute Details

#codeFixnum (readonly)

The numeric status code

Examples:

status.code # => 200

Returns:

  • (Fixnum)

    status code



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

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)

Parameters:

  • object (Symbol, #to_i)

Returns:

Raises:

  • (Error)

    if coercion is impossible



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/http/response/status.rb', line 24

def coerce(object)
  code = case object
         when String  then SYMBOL_CODES.fetch(symbolize(object), nil)
         when Symbol  then SYMBOL_CODES.fetch(object, nil)
         when Numeric then object
         end

  return new code if code

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

Instance Method Details

#<=>(other) ⇒ Integer?

Compare status codes for ordering

Examples:

Status.new(200) <=> Status.new(404) # => -1

Parameters:

Returns:

  • (Integer, nil)


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

def <=>(other)
  return nil unless other.respond_to?(:to_i)

  code <=> other.to_i
end

#client_error?Boolean

Check if status code is client error (4XX)

Examples:

status.client_error? # => false

Returns:

  • (Boolean)


197
198
199
# File 'lib/http/response/status.rb', line 197

def client_error?
  400 <= code && code < 500
end

#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}

Pattern matching interface for matching against status code and reason

Examples:

case response.status
in { code: 200..299 }
  "success"
in { code: 400.. }
  "error"
end

Parameters:

  • keys (Array<Symbol>, nil)

    keys to extract, or nil for all

Returns:

  • (Hash{Symbol => Object})


249
250
251
252
# File 'lib/http/response/status.rb', line 249

def deconstruct_keys(keys)
  hash = { code: code, reason: reason }
  keys ? hash.slice(*keys) : hash
end

#hashInteger

Hash value based on status code

Examples:

Status.new(200).hash

Returns:

  • (Integer)


130
131
132
# File 'lib/http/response/status.rb', line 130

def hash
  code.hash
end

#informational?Boolean

Check if status code is informational (1XX)

Examples:

status.informational? # => false

Returns:

  • (Boolean)


164
165
166
# File 'lib/http/response/status.rb', line 164

def informational?
  100 <= code && code < 200
end

#inspectString

Printable version of HTTP Status

(see String#inspect)

Examples:

status.inspect # => "#<HTTP::Response::Status 200 OK>"

Returns:

  • (String)


232
233
234
# File 'lib/http/response/status.rb', line 232

def inspect
  "#<#{self.class} #{self}>"
end

#reasonString?

Return the reason phrase for the status code

Examples:

status.reason # => "OK"

Returns:

  • (String, nil)

    status message

See Also:



142
143
144
# File 'lib/http/response/status.rb', line 142

def reason
  REASONS[code]
end

#redirect?Boolean

Check if status code is redirection (3XX)

Examples:

status.redirect? # => false

Returns:

  • (Boolean)


186
187
188
# File 'lib/http/response/status.rb', line 186

def redirect?
  300 <= code && code < 400
end

#server_error?Boolean

Check if status code is server error (5XX)

Examples:

status.server_error? # => false

Returns:

  • (Boolean)


208
209
210
# File 'lib/http/response/status.rb', line 208

def server_error?
  500 <= code && code < 600
end

#success?Boolean

Check if status code is successful (2XX)

Examples:

status.success? # => true

Returns:

  • (Boolean)


175
176
177
# File 'lib/http/response/status.rb', line 175

def success?
  200 <= code && code < 300
end

#to_iInteger

Convert status to Integer

Examples:

status.to_i # => 200

Returns:

  • (Integer)


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

#to_intInteger

Implicit conversion to Integer

Examples:

status.to_int # => 200

Returns:

  • (Integer)


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

def_delegators :@code, :to_i, :to_int

#to_sString

Return string representation of HTTP status

Examples:

status.to_s # => "200 OK"

Returns:

  • (String)


153
154
155
# File 'lib/http/response/status.rb', line 153

def to_s
  reason ? "#{code} #{reason}" : code.to_s
end

#to_symnil, Symbol

Symbolized #reason

Examples:

status.to_sym # => :ok

Returns:

  • (nil)

    unless code is well-known (see REASONS)

  • (Symbol)


220
221
222
# File 'lib/http/response/status.rb', line 220

def to_sym
  SYMBOLS[code]
end