Skip to content

Instantly share code, notes, and snippets.

@nuclearsandwich
Created January 27, 2012 19:46
Show Gist options
  • Save nuclearsandwich/1690563 to your computer and use it in GitHub Desktop.
Save nuclearsandwich/1690563 to your computer and use it in GitHub Desktop.
module Cage
class HTTPStatus < BasicObject
STATUSES = ::Hash.new "UNKNOWN"
STATUSES.merge! ::Hash[
100 => "CONTINUE",
101 => "SWITCHING PROTOCOLS",
102 => "PROCESSING",
103 => "CHECKPOINT",
122 => "REQUEST-URI TOO LONG",
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",
306 => "SWITCH PROXY",
307 => "TEMPORARY REDIRECT",
308 => "RESUME INCOMPLETE",
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 (RFC 2324)",
422 => "UNPROCESSABLE ENTITY (WEBDAV) (RFC 4918)",
423 => "LOCKED (WEBDAV) (RFC 4918)",
424 => "FAILED DEPENDENCY (WEBDAV) (RFC 4918)",
425 => "UNORDERED COLLECTION (RFC 3648)",
426 => "UPGRADE REQUIRED (RFC 2817)",
428 => "PRECONDITION REQUIRED",
429 => "TOO MANY REQUESTS",
431 => "REQUEST HEADER FIELDS TOO LARGE",
444 => "NO RESPONSE",
449 => "RETRY WITH",
450 => "BLOCKED BY WINDOWS PARENTAL CONTROLS",
499 => "CLIENT CLOSED REQUEST",
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",
509 => "BANDWIDTH LIMIT EXCEEDED (APACHE BW/LIMITED EXTENSION)",
510 => "NOT EXTENDED",
511 => "NETWORK AUTHENTICATION REQUIRED",
598 => "NETWORK READ TIMEOUT ERROR",
599 => "NETWORK CONNECT TIMEOUT ERROR",
]
STATUSES.freeze
def initialize status_code
@code = status_code
@message = STATUSES[status_code]
end
def != other
@code != other
end
def == other
@code == other
end
def inspect
"#{@code} #{@message}"
end
alias_method :to_s, :inspect
alias_method :pretty_inspect, :inspect
def method_missing symbol, *args, &block
@code.send symbol, *args, &block
end
def respond_to? symbol
@code.respond_to? symbol || super
end
def self.new status_code
status_objects[status_code] ||= super
end
def self.status_objects
@status_objects ||= {}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment