class Recluse::StatusCode

An HTTP status code.

Attributes

code[R]

The status code. Either a number, a string with x's to represent wildcards, or 'idk'.

exact[R]

Whether or not this is an exact numerical code.

Public Class Methods

equal_digit?(a, b) click to toggle source

Digital comparison. x's are wildcards.

# File lib/recluse/statuscode.rb, line 86
def equal_digit?(a, b)
  ((a == b) || (a == 'x') || (b == 'x'))
end
new(code) click to toggle source

Create a status code.

# 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
valid_code?(code) click to toggle source

Is the passed code valid?

# 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

Public Instance Methods

equal?(other) click to toggle source

Is this code equal to another?

# 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?() click to toggle source

Whether or not this is an exact numerical code.

# File lib/recluse/statuscode.rb, line 48
def exact?
  @exact
end
to_s() click to toggle source

Output the status code to a string.

# File lib/recluse/statuscode.rb, line 42
def to_s
  @code.to_s
end