module DlValidator

Constants

VERSION

Public Class Methods

get_abbreviation_key(dl_state) click to toggle source

This is moved out into a method to make unit testing easier.

# File lib/dl_validator.rb, line 26
def self.get_abbreviation_key(dl_state)
  # If the dl_state is greater than 2 then it is a full state name and we need to find the corresponding abbreviation for that state.
  key = ''
  if dl_state.length > 2
    DlConfig::STATES.each do |k, v|
      key = k if v == dl_state
    end
  else
    key = dl_state # The dl_state is already an abbreviated state
  end
  key
end
invalid?(dl_number, dl_state) click to toggle source
# File lib/dl_validator.rb, line 6
def self.invalid?(dl_number, dl_state)
  # Stop and return true if either dl_number or dl_state are nil
  return true if dl_number.nil? or dl_state.nil?

  # Downcase and remove non-word characters that are not asterisks
  dl_number = dl_number.to_s.upcase.gsub(/[^a-zA-Z0-9\*]*/, '')
  dl_state  = dl_state.to_s.upcase.gsub(/(\W|\d|_)*/, '')

  # Stop here and return true if the state does not exist
  return true if !DlConfig::STATES.include?(dl_state) and !DlConfig::STATES.values.include?(dl_state)

  # Find the state abbreviation key we need to access our regex hash.
  key = get_abbreviation_key(dl_state)

  # Regular expression match to validate the drivers license is invalid or not
  return false if DlConfig::STATES_REGEX[key].match(dl_number)
  true
end
valid?(dl_number, dl_state) click to toggle source

Inverse of invalid?

# File lib/dl_validator.rb, line 40
def self.valid?(dl_number, dl_state)
  !invalid?(dl_number, dl_state)
end