Class: Webbed::StatusCode

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

Overview

Representation of an HTTP Status Code.

Constant Summary

UNKNOWN_REASON_PHRASE =
'Unknown Status Code'

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (StatusCode) initialize(status_code)

Creates a new Status Code.



50
51
52
53
# File 'lib/webbed/status_code.rb', line 50

def initialize(status_code)
  @status_code = status_code.to_i
  @default_reason_phrase = self.class.default_reason_phrases[@status_code] || UNKNOWN_REASON_PHRASE
end

Instance Attribute Details

- (String) default_reason_phrase (readonly)

The default Reason Phrase of the Status Code.



45
46
47
# File 'lib/webbed/status_code.rb', line 45

def default_reason_phrase
  @default_reason_phrase
end

Class Method Details

+ ({Fixnum => String}) default_reason_phrases

The default Reason Phrases for Status Codes.



19
20
21
# File 'lib/webbed/status_code.rb', line 19

def default_reason_phrases
  @default_reason_phrases ||= {}
end

+ (Webbed::StatusCode) lookup(status_code)

Looks up the registered Status Code or returns a temporary one.



37
38
39
# File 'lib/webbed/status_code.rb', line 37

def lookup(status_code)
  registered[status_code.to_i] || new(status_code)
end

+ (Webbed::StatusCode) register(status_code, default_reason_phrase)

Registers a Status Code and its default Reason Phrase.



28
29
30
31
# File 'lib/webbed/status_code.rb', line 28

def register(status_code, default_reason_phrase)
  default_reason_phrases[status_code.to_i] = default_reason_phrase
  registered[status_code.to_i] = new(status_code)
end

+ ({Fixnum => Webbed::StatusCode}) registered

The registered Status Codes.



12
13
14
# File 'lib/webbed/status_code.rb', line 12

def registered
  @registered ||= {}
end

Instance Method Details

- (Fixnum) <=>(other_status_code)

Compares the Status Code to another Status Code.



59
60
61
# File 'lib/webbed/status_code.rb', line 59

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.



113
114
115
# File 'lib/webbed/status_code.rb', line 113

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.



143
144
145
# File 'lib/webbed/status_code.rb', line 143

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.



83
84
85
# File 'lib/webbed/status_code.rb', line 83

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.



103
104
105
# File 'lib/webbed/status_code.rb', line 103

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.



123
124
125
# File 'lib/webbed/status_code.rb', line 123

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.



93
94
95
# File 'lib/webbed/status_code.rb', line 93

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

- (Fixnum) to_i

Converts the Status Code to an integer.



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

def to_i
  @status_code
end

- (String) to_s

Converts the Status Code to a string.



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

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.



133
134
135
# File 'lib/webbed/status_code.rb', line 133

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