Class: HTTP::Response::Status
- Inherits:
-
Delegator
- Object
- Delegator
- HTTP::Response::Status
- Defined in:
- lib/http/response/status.rb,
lib/http/response/status/reasons.rb
Constant Summary collapse
- SYMBOLS =
Code to Symbol map
REASONS.transform_values { |v| symbolize(v) }.freeze
- SYMBOL_CODES =
Reversed SYMBOLS map.
SYMBOLS.to_h { |k, v| [v, k] }.freeze
- REASONS =
Code to Reason map
{ 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 { |_, v| v.freeze }.freeze
Instance Attribute Summary collapse
-
#code ⇒ Fixnum
readonly
Status code.
Class Method Summary collapse
-
.coerce(object) ⇒ Status
(also: [])
Coerces given value to Status.
Instance Method Summary collapse
- #__getobj__ ⇒ Object
- #__setobj__(obj) ⇒ Object
-
#client_error? ⇒ Boolean
Check if status code is client error (4XX).
-
#informational? ⇒ Boolean
Check if status code is informational (1XX).
-
#inspect ⇒ Object
Printable version of HTTP Status, surrounded by quote marks, with special characters escaped.
-
#reason ⇒ String?
Status message.
-
#redirect? ⇒ Boolean
Check if status code is redirection (3XX).
-
#server_error? ⇒ Boolean
Check if status code is server error (5XX).
-
#success? ⇒ Boolean
Check if status code is successful (2XX).
-
#to_s ⇒ String
String representation of HTTP status.
-
#to_sym ⇒ nil, Symbol
Symbolized #reason.
Instance Attribute Details
#code ⇒ Fixnum (readonly)
Returns status code.
75 76 77 |
# File 'lib/http/response/status.rb', line 75 def code @code end |
Class Method Details
.coerce(object) ⇒ Status Also known as: []
Coerces given value to Status.
22 23 24 25 26 27 28 29 30 31 32 |
# 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 |
Instance Method Details
#__getobj__ ⇒ Object
148 149 150 |
# File 'lib/http/response/status.rb', line 148 def __getobj__ @code end |
#__setobj__(obj) ⇒ Object
142 143 144 145 146 |
# 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 |
#client_error? ⇒ Boolean
Check if status code is client error (4XX)
108 109 110 |
# File 'lib/http/response/status.rb', line 108 def client_error? 400 <= code && code < 500 end |
#informational? ⇒ Boolean
Check if status code is informational (1XX)
90 91 92 |
# File 'lib/http/response/status.rb', line 90 def informational? 100 <= code && code < 200 end |
#inspect ⇒ Object
Printable version of HTTP Status, surrounded by quote marks, with special characters escaped.
(see String#inspect)
130 131 132 |
# File 'lib/http/response/status.rb', line 130 def inspect "#<#{self.class} #{self}>" end |
#reason ⇒ String?
Returns status message.
79 80 81 |
# File 'lib/http/response/status.rb', line 79 def reason REASONS[code] end |
#redirect? ⇒ Boolean
Check if status code is redirection (3XX)
102 103 104 |
# File 'lib/http/response/status.rb', line 102 def redirect? 300 <= code && code < 400 end |
#server_error? ⇒ Boolean
Check if status code is server error (5XX)
114 115 116 |
# File 'lib/http/response/status.rb', line 114 def server_error? 500 <= code && code < 600 end |
#success? ⇒ Boolean
Check if status code is successful (2XX)
96 97 98 |
# File 'lib/http/response/status.rb', line 96 def success? 200 <= code && code < 300 end |
#to_s ⇒ String
Returns string representation of HTTP status.
84 85 86 |
# File 'lib/http/response/status.rb', line 84 def to_s "#{code} #{reason}".strip end |