Method: Phonelib::PhoneFormatter#country_code

Defined in:
lib/phonelib/phone_formatter.rb

#country_codeString

Returns the country code from the original phone number.

Returns:

  • (String)

    matched country phone code



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/phonelib/phone_formatter.rb', line 36

def country_code
  return @country_code if @country_code

  code = Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
  return @country_code = code unless code == '1' && Phonelib.phone_data[country][Core::LEADING_DIGITS] && Phonelib.phone_data[country][Core::LEADING_DIGITS] =~ /[\[\|]/

  match = e164.match(/\A\+(1(#{Phonelib.phone_data[country][Core::LEADING_DIGITS]}))/)
  if match
    @country_code = match[1]
  else
    @country_code = '1'
  end
end