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
@return [Fixnum] status code
Public Class Methods
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 22 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
Private Class Methods
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 47 def symbolize(str) str.to_s.downcase.tr("-", " ").gsub(/[^a-z ]/, "").gsub(/\s+/, "_").to_sym end
Public Instance Methods
# File lib/http/response/status.rb, line 148 def __getobj__ @code end
# File lib/http/response/status.rb, line 142 def __setobj__(obj) raise TypeError, "Expected #{obj.inspect} to respond to #to_i" unless obj.respond_to? :to_i @code = obj.to_i end
Check if status code is client error (4XX) @return [Boolean]
# File lib/http/response/status.rb, line 108 def client_error? 400 <= code && code < 500 end
Check if status code is informational (1XX) @return [Boolean]
# File lib/http/response/status.rb, line 90 def informational? 100 <= code && code < 200 end
@see REASONS
@return [String, nil] status message
# File lib/http/response/status.rb, line 79 def reason REASONS[code] end
Check if status code is redirection (3XX) @return [Boolean]
# File lib/http/response/status.rb, line 102 def redirect? 300 <= code && code < 400 end
Check if status code is server error (5XX) @return [Boolean]
# File lib/http/response/status.rb, line 114 def server_error? 500 <= code && code < 600 end
Check if status code is successful (2XX) @return [Boolean]
# File lib/http/response/status.rb, line 96 def success? 200 <= code && code < 300 end
@return [String] string representation of HTTP
status
# File lib/http/response/status.rb, line 84 def to_s "#{code} #{reason}".strip end
Symbolized {#reason}
@return [nil] unless code is well-known (see REASONS
) @return [Symbol]
# File lib/http/response/status.rb, line 122 def to_sym SYMBOLS[code] end