Class: Recluse::StatusCode
- Inherits:
-
Object
- Object
- Recluse::StatusCode
- Defined in:
- lib/recluse/statuscode.rb
Overview
An HTTP status code.
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
The status code.
-
#exact ⇒ Object
readonly
Whether or not this is an exact numerical code.
Class Method Summary collapse
-
.equal_digit?(a, b) ⇒ Boolean
Digital comparison.
-
.valid_code?(code) ⇒ Boolean
Is the passed code valid?.
Instance Method Summary collapse
-
#equal?(other) ⇒ Boolean
Is this code equal to another?.
-
#exact? ⇒ Boolean
Whether or not this is an exact numerical code.
-
#initialize(code) ⇒ StatusCode
constructor
Create a status code.
-
#to_s ⇒ Object
Output the status code to a string.
Constructor Details
#initialize(code) ⇒ StatusCode
Create a status code.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/recluse/statuscode.rb', line 20 def initialize(code) raise StatusCodeError, "Invalid status code: #{code}" unless StatusCode.valid_code?(code) case code when String if (code =~ /^[\d]{3}/).nil? # wildcards or idk @code = code.downcase @exact = @code == 'idk' else # whole number @code = code.to_i @exact = true end when Recluse::StatusCode @code = code.code @exact = code.exact when Integer @code = code @exact = true end end |
Instance Attribute Details
#code ⇒ Object (readonly)
The status code. Either a number, a string with x’s to represent wildcards, or ‘idk’.
12 13 14 |
# File 'lib/recluse/statuscode.rb', line 12 def code @code end |
#exact ⇒ Object (readonly)
Whether or not this is an exact numerical code.
16 17 18 |
# File 'lib/recluse/statuscode.rb', line 16 def exact @exact end |
Class Method Details
.equal_digit?(a, b) ⇒ Boolean
Digital comparison. x’s are wildcards.
86 87 88 |
# File 'lib/recluse/statuscode.rb', line 86 def equal_digit?(a, b) ((a == b) || (a == 'x') || (b == 'x')) end |
.valid_code?(code) ⇒ Boolean
Is the passed code valid?
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/recluse/statuscode.rb', line 66 def self.valid_code?(code) case code when String code = code.downcase return false if (code =~ /^([\dx]{3}|idk)$/i).nil? return true if (code == 'idk') || (code[0] == 'x') initial = code[0].to_i ((1 <= initial) && (initial <= 9)) when Integer ((100 <= code) && code < 1000) when Recluse::StatusCode true else false end end |
Instance Method Details
#equal?(other) ⇒ Boolean
Is this code equal to another?
54 55 56 57 58 59 60 61 62 |
# File 'lib/recluse/statuscode.rb', line 54 def equal?(other) comparable = StatusCode.new other return @code == comparable.code if exact? && comparable.exact? self_s = to_s comparable_s = comparable.to_s (0...3).all? do |i| StatusCode.equal_digit?(self_s[i], comparable_s[i]) end end |
#exact? ⇒ Boolean
Whether or not this is an exact numerical code.
48 49 50 |
# File 'lib/recluse/statuscode.rb', line 48 def exact? @exact end |
#to_s ⇒ Object
Output the status code to a string.
42 43 44 |
# File 'lib/recluse/statuscode.rb', line 42 def to_s @code.to_s end |