Class: Webbed::StatusCode

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/webbed/status_code.rb

Overview

Representation of HTTP Status Codes

Constant Summary

UNKNOWN_REASON_PHRASE =
'Unknown Status Code'
REASON_PHRASES =
{
  100 => 'Continue',
  101 => 'Switching Protocols',
  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  307 => 'Temporary 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 Time-out',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Request Entity Too Large',
  414 => 'Request-URI Too Large',
  415 => 'Unsupported Media Type',
  416 => 'Requested range not satisfiable',
  417 => 'Expectation Failed',
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Time-out',
  505 => 'HTTP Version not supported'
}
@@cached =
{}

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (StatusCode) initialize(status_code)

Creates a new Status Code

Parameters:

  • status_code (Fixnum)


72
73
74
75
# File 'lib/webbed/status_code.rb', line 72

def initialize(status_code)
  @status_code = status_code
  @default_reason_phrase = REASON_PHRASES[@status_code] || UNKNOWN_REASON_PHRASE
end

Instance Attribute Details

- (String) default_reason_phrase (readonly)

The default Reason Phrase of the Status Code

Returns:

  • (String)


55
56
57
# File 'lib/webbed/status_code.rb', line 55

def default_reason_phrase
  @default_reason_phrase
end

Class Method Details

+ (StatusCode) new(status_code)

Retrieves a Status Code from the cache or creates a new one

All created Status Codes are cached forever.

Parameters:

  • status_code (Fixnum)

Returns:

See Also:



64
65
66
67
# File 'lib/webbed/status_code.rb', line 64

def self.new(status_code)
  status_code = status_code.to_i
  @@cached[status_code] ||= super(status_code)
end

Instance Method Details

- (Fixnum) <=>(other_status_code)

Comparse the Status Code to another Status Code

Parameters:

  • other_status_code (#to_i)

    the other Status Code

Returns:

  • (Fixnum)

    the sign of the comparison (either 1, 0, or -1)



81
82
83
# File 'lib/webbed/status_code.rb', line 81

def <=>(other_status_code)
  @status_code <=> other_status_code.to_i
end

- (Boolean) client_error?

Whether or not the Status Code is a client error

According to RFC 2616, client error status codes are in the range of 400 to 499, inclusive.

Returns:

  • (Boolean)


135
136
137
# File 'lib/webbed/status_code.rb', line 135

def client_error?
  (400...500).include?(@status_code)
end

- (Boolean) error?

Whether or not the Status Code is an error

According to RFC 2616, Status Codes that signify errors are in the range of 400 to 599, inclusive.

Returns:

  • (Boolean)


165
166
167
# File 'lib/webbed/status_code.rb', line 165

def error?
  (400...600).include?(@status_code)
end

- (Boolean) informational?

Whether or not the Status Code is informational

According to RFC 2616, informational status codes are in the range of 100 to 199, inclusive.

Returns:

  • (Boolean)


105
106
107
# File 'lib/webbed/status_code.rb', line 105

def informational?
  (100...200).include?(@status_code)
end

- (Boolean) redirection?

Whether or not the Status Code is a redirection

According to RFC 2616, redirection status codes are in the range of 300 to 399, inclusive.

Returns:

  • (Boolean)


125
126
127
# File 'lib/webbed/status_code.rb', line 125

def redirection?
  (300...400).include?(@status_code)
end

- (Boolean) server_error?

Whether or not the Status Code is a server error

According to RFC 2616, server error status codes are in the range of 500 to 599, inclusive.

Returns:

  • (Boolean)


145
146
147
# File 'lib/webbed/status_code.rb', line 145

def server_error?
  (500...600).include?(@status_code)
end

- (Boolean) successful?

Whether or not the Status Code is successful

According to RFC 2616, successful status codes are in the range of 200 to 299, inclusive.

Returns:

  • (Boolean)


115
116
117
# File 'lib/webbed/status_code.rb', line 115

def successful?
  (200...300).include?(@status_code)
end

- (Fixnum) to_i

Converts the Status Code to an integer

Returns:

  • (Fixnum)


88
89
90
# File 'lib/webbed/status_code.rb', line 88

def to_i
  @status_code
end

- (String) to_s

Converts the Status Code to a string

Returns:

  • (String)


95
96
97
# File 'lib/webbed/status_code.rb', line 95

def to_s
  @status_code.to_s
end

- (Boolean) unknown?

Whether or not the Status Code is unknown

According to RFC 2616, the only defined Status Code ranges are from 100 to 599, inclusive. Anything outside that range is an unknown Status Code.

Returns:

  • (Boolean)


155
156
157
# File 'lib/webbed/status_code.rb', line 155

def unknown?
  !(100...600).include?(@status_code)
end