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

#initialize(status_code) ⇒ StatusCode

Creates a new Status Code.

Parameters:

  • status_code (#to_i)


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

#default_reason_phraseString (readonly)

The default Reason Phrase of the Status Code.

Returns:

  • (String)


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

def default_reason_phrase
  @default_reason_phrase
end

Class Method Details

.default_reason_phrases{Fixnum => String}

The default Reason Phrases for Status Codes.

Returns:

  • ({Fixnum => String})


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

def default_reason_phrases
  @default_reason_phrases ||= {}
end

.lookup(status_code) ⇒ Webbed::StatusCode

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

Parameters:

  • status_code (#to_i)

    the Status Code

Returns:



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

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

.register(status_code, default_reason_phrase) ⇒ Webbed::StatusCode

Registers a Status Code and its default Reason Phrase.

Parameters:

  • status_code (#to_i)

    the Status Code

  • default_reason_phrase (String)

    the default Reason Phrase

Returns:



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

.registered{Fixnum => Webbed::StatusCode}

The registered Status Codes.

Returns:



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

def registered
  @registered ||= {}
end

Instance Method Details

#<=>(other_status_code) ⇒ Fixnum

Compares 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)



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

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

#client_error?Boolean

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)


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

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

#error?Boolean

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)


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

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

#informational?Boolean

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)


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

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

#redirection?Boolean

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)


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

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

#server_error?Boolean

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)


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

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

#successful?Boolean

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)


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

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

#to_iFixnum

Converts the Status Code to an integer.

Returns:

  • (Fixnum)


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

def to_i
  @status_code
end

#to_sString

Converts the Status Code to a string.

Returns:

  • (String)


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

def to_s
  @status_code.to_s
end

#unknown?Boolean

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)


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

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