Class: SanitizePhone

Inherits:
Object
  • Object
show all
Extended by:
ConstantVariables
Defined in:
lib/sanitize_phone.rb

Constant Summary

Constants included from ConstantVariables

ConstantVariables::COUNTRY_CODE_REGEX, ConstantVariables::DOUBLE_COUNTRY_CODE_REGEX, ConstantVariables::MM_PHONE_REGEX, ConstantVariables::MPT_REGEX, ConstantVariables::OOREDOO_REGEX, ConstantVariables::TELENOR_REGEX, ConstantVariables::ZERO_BEFORE_AREACODE_REGEX

Class Method Summary collapse

Class Method Details

.check_phone_regex(phone_number) ⇒ Object

Checking mm_phone_regex string



48
49
50
# File 'lib/sanitize_phone.rb', line 48

def check_phone_regex(phone_number)
  return phone_number.match(MM_PHONE_REGEX) ? true : false
end

.remove_double_country_code(phone_number) ⇒ Object

Clean Double Country code

  • Example Test Code

‘+95959978412345’ to ‘+959978412345’



27
28
29
30
31
32
33
34
# File 'lib/sanitize_phone.rb', line 27

def remove_double_country_code(phone_number)
  if phone_number.match(COUNTRY_CODE_REGEX)
    if phone_number.match(DOUBLE_COUNTRY_CODE_REGEX)
      phone_number = phone_number.sub('9595', '95')
    end        
  end
  return phone_number
end

.remove_whitespace_and_dash(phone_number) ⇒ Object

Removing ‘[],(),- and whitespace’ characters

  • Example Test Case

‘09 45001 2345 ’ -> ‘09450012345’ ‘09-45001-2345’ -> ‘09450012345’ ‘ 09450012345 ’ -> ‘09450012345’



14
15
16
17
18
19
20
21
22
# File 'lib/sanitize_phone.rb', line 14

def remove_whitespace_and_dash(phone_number)
  if phone_number
    phone_number = phone_number.strip
    phone_number = phone_number.gsub(' ','')
    phone_number = phone_number.gsub('-', '')
  end

  return phone_number
end

.remove_zero_before_area_code(phone_number) ⇒ Object

Clean Zero before area code

  • Example Test Code

‘+9509978412345’ to ‘+959978412345’



39
40
41
42
43
44
45
# File 'lib/sanitize_phone.rb', line 39

def remove_zero_before_area_code(phone_number)
  if phone_number.match(ZERO_BEFORE_AREACODE_REGEX)
    phone_number = phone_number.sub('9509', '959')
  end

  return phone_number
end