Sha256: 0827c6c0b4cbd835bc83050a90f7444b441defcde941adc1ee1607dd63a5c5f5

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

require 'dl_validator/version'
require 'config/dl_config'
require  'extensions/drivers_license_invalid_validator' if defined?(Rails)

module DlValidator
  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
    dl_number = dl_number.to_s.upcase.gsub(/(\W|_)*/, '')
    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

  # This is moved out into a method to make unit testing easier.
  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dl_validator-0.0.4 lib/dl_validator.rb
dl_validator-0.0.3 lib/dl_validator.rb
dl_validator-0.0.2 lib/dl_validator.rb