class HTTP::Response::Status

Constants

REASONS

Code to Reason map

@example Usage

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

@return [Hash<Fixnum => String>]

SYMBOLS

Code to Symbol map

@example Usage

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

@return [Hash<Fixnum => Symbol>]

SYMBOL_CODES

Reversed {SYMBOLS} map.

@example Usage

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

@return [Hash<Symbol => Fixnum>]

Attributes

code[R]

@return [Fixnum] status code

Public Class Methods

[](object)
Alias for: coerce
coerce(object) click to toggle source

Coerces given value to Status.

@example

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

@raise [Error] if coercion is impossible @param [Symbol, to_i] object @return [Status]

# 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
         end

  return new code if code

  raise Error, "Can't coerce #{object.class}(#{object}) to #{self}"
end
Also aliased as: []

Private Class Methods

symbolize(str) click to toggle source

Symbolizes given string

@example

symbolize "Bad Request"           # => :bad_request
symbolize "Request-URI Too Long"  # => :request_uri_too_long
symbolize "I'm a Teapot"          # => :im_a_teapot

@param [#to_s] str @return [Symbol]

# File lib/http/response/status.rb, line 45
def symbolize(str)
  str.to_s.downcase.tr("-", " ").gsub(/[^a-z ]/, "").gsub(/\s+/, "_").to_sym
end

Public Instance Methods

__getobj__() click to toggle source
# File lib/http/response/status.rb, line 145
def __getobj__
  @code
end
__setobj__(obj) click to toggle source
# File lib/http/response/status.rb, line 140
def __setobj__(obj)
  raise TypeError, "Expected #{obj.inspect} to respond to #to_i" unless obj.respond_to? :to_i
  @code = obj.to_i
end
client_error?() click to toggle source

Check if status code is client error (4XX) @return [Boolean]

# File lib/http/response/status.rb, line 106
def client_error?
  400 <= code && code < 500
end
informational?() click to toggle source

Check if status code is informational (1XX) @return [Boolean]

# File lib/http/response/status.rb, line 88
def informational?
  100 <= code && code < 200
end
inspect() click to toggle source

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

(see String#inspect)

# File lib/http/response/status.rb, line 128
def inspect
  "#<#{self.class} #{self}>"
end
reason() click to toggle source

@see REASONS @return [String, nil] status message

# File lib/http/response/status.rb, line 77
def reason
  REASONS[code]
end
redirect?() click to toggle source

Check if status code is redirection (3XX) @return [Boolean]

# File lib/http/response/status.rb, line 100
def redirect?
  300 <= code && code < 400
end
server_error?() click to toggle source

Check if status code is server error (5XX) @return [Boolean]

# File lib/http/response/status.rb, line 112
def server_error?
  500 <= code && code < 600
end
success?() click to toggle source

Check if status code is successful (2XX) @return [Boolean]

# File lib/http/response/status.rb, line 94
def success?
  200 <= code && code < 300
end
to_s() click to toggle source

@return [String] string representation of HTTP status

# File lib/http/response/status.rb, line 82
def to_s
  "#{code} #{reason}".strip
end
to_sym() click to toggle source

Symbolized {#reason}

@return [nil] unless code is well-known (see REASONS) @return [Symbol]

# File lib/http/response/status.rb, line 120
def to_sym
  SYMBOLS[code]
end