Module: AreaCodeValidator

Defined in:
lib/area_code_validator.rb,
lib/config/area_code_config.rb,
lib/area_code_validator/version.rb

Defined Under Namespace

Classes: AreaCodeConfig

Constant Summary collapse

VERSION =
'0.0.6'

Class Method Summary collapse

Class Method Details

.get_abbreviation_key(area_code_state) ⇒ Object

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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/area_code_validator.rb', line 28

def self.get_abbreviation_key(area_code_state)
  # If the area_code_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 area_code_state.length > 2
    AreaCodeConfig::STATES.each do |k, v|
      key = k if v == area_code_state
    end
  else
    key = area_code_state # The area_code_state is already an abbreviated state
  end
  key
end

.invalid?(area_code, area_code_state) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/area_code_validator.rb', line 7

def self.invalid?(area_code, area_code_state)
  # Stop here and return true if either area_code or area_code_state are nil
  return true if area_code.nil? or area_code_state.nil?

  # Upcase and scrub the area_code and area_code_state
  area_code        = area_code.to_s.upcase.gsub(/(\W|[A-Z]|_)*/, '')
  area_code_state  = area_code_state.to_s.upcase.gsub(/(\W|\d|_)*/, '')

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

  # Find the state abbreviation key we need to access our hash of arrays of area codes.
  key = get_abbreviation_key(area_code_state)

  # If the area code is in our list return false else return true
  return false if AreaCodeConfig::AREA_CODES[key].include?(area_code)
  true
end

.valid?(area_code, area_code_state) ⇒ Boolean

inverse of invalid?

Returns:

  • (Boolean)


42
43
44
# File 'lib/area_code_validator.rb', line 42

def self.valid?(area_code, area_code_state)
  !invalid?(area_code, area_code_state)
end