Module: Phonelib::Core

Included in:
PhoneValidator, Phonelib
Defined in:
lib/phonelib/core.rb

Overview

main module that includes all basic data and methods

Constant Summary collapse

FILE_MAIN_DATA =
'data/phone_data.dat'
FILE_EXT_DATA =
'data/extended_data.dat'
GENERAL =

Validation patterns keys constants

:general_desc
PREMIUM_RATE =
:premium_rate
TOLL_FREE =
:toll_free
SHARED_COST =

between caller and recipient, and is hence typically less than PREMIUM_RATE calls

:shared_cost
VOIP =
:voip
PERSONAL_NUMBER =

and may be routed to either a MOBILE or FIXED_LINE number.

:personal_number
PAGER =
:pager
UAN =
:uan
VOICEMAIL =
:voicemail
FIXED_LINE =
:fixed_line
MOBILE =
:mobile
FIXED_OR_MOBILE =
:fixed_or_mobile
SHORT_CODE =
:short_code
EMERGENCY =
:emergency
CARRIER_SPECIFIC =
:carrier_specific
SMS_SERVICES =
:sms_services
EXPANDED_EMERGENCY =
:expanded_emergency
NO_INTERNATIONAL_DIALING =
:no_international_dialling
CARRIER_SERVICES =
:carrier_services
DIRECTORY_SERVICES =
:directory_services
STANDARD_RATE =
:standard_rate
CARRIER_SELECTION_CODES =
:carrier_selection_codes
AREA_CODE_OPTIONAL =
:area_code_optional
VALID_PATTERN =

Internal use keys for validations

:national_number_pattern
POSSIBLE_PATTERN =
:possible_number_pattern
NATIONAL_PREFIX =
:national_prefix
NATIONAL_PREFIX_FOR_PARSING =
:national_prefix_for_parsing
NATIONAL_PREFIX_TRANSFORM_RULE =
:national_prefix_transform_rule
NATIONAL_PREFIX_RULE =
:national_prefix_formatting_rule
COUNTRY_CODE =
:country_code
LEADING_DIGITS =
:leading_digits
INTERNATIONAL_PREFIX =
:international_prefix
MAIN_COUNTRY_FOR_CODE =
:main_country_for_code
DOUBLE_COUNTRY_PREFIX_FLAG =
:double_prefix
TYPES =
:types
FORMATS =
:formats
PATTERN =
:pattern
SHORT =
:short
PLUS_SIGN =
'+'.freeze
VANITY_4_LETTERS_KEYS_REGEX =
/[SVYZ]/.freeze
AREA_CODE_TYPES =
[FIXED_LINE, FIXED_OR_MOBILE, MOBILE].freeze
AREA_CODE_MOBILE_COUNTRIES =
%w(AR MX BR).freeze
AREA_CODE_MOBILE_TOKENS =
{
  'MX' => '1',
  'AR' => '9'
}.freeze
DEFAULT_NUMBER_FORMAT =
{
  pattern: '(\\d+)(\\d{3})(\\d{4})',
  format: '$1 $2 $3'
}.freeze
TYPES_DESC =
{
  general_desc: 'General Pattern',
  premium_rate: 'Premium Rate',
  toll_free: 'Toll Free',
  shared_cost: 'Shared Cost',
  voip: 'VoIP',
  personal_number: 'Personal Number',
  pager: 'Pager',
  uan: 'UAN',
  voicemail: 'VoiceMail',
  fixed_line: 'Fixed Line',
  mobile: 'Mobile',
  fixed_or_mobile: 'Fixed Line or Mobile',
  short_code: 'Short code',
  emergency: 'Emergency services',
  carrier_specific: 'Carrier specific number',
  sms_services: 'SMS Services only phone',
  expanded_emergency: 'Expanded emergency',
  no_international_dialling: 'No International Dialing phone',
  carrier_services: 'Carrier Services',
  directory_services: 'Directory Services',
  standard_rate: 'Standard Rate Destination',
  carrier_selection_codes: 'Carrier Selection codes',
  area_code_optional: 'Are code optional'
}.freeze
SHORT_CODES =
[
  :short_code, :emergency, :carrier_specific, :sms_services,
  :expanded_emergency, :no_international_dialling, :carrier_services,
  :directory_services, :standard_rate, :carrier_selection_codes,
  :area_code_optional
].freeze
EXT_PREFIXES =
:prefixes
EXT_GEO_NAMES =
:geo_names
EXT_COUNTRY_NAMES =
:country_names
EXT_TIMEZONES =
:timezones
EXT_CARRIERS =
:carriers
EXT_GEO_NAME_KEY =
:g
EXT_TIMEZONE_KEY =
:t
EXT_CARRIER_KEY =
:c
@@phone_data =
nil
@@phone_regexp_cache =
{}
@@phone_ext_data =
nil
@@default_country =
nil
@@extension_separator =
';'
@@extension_separate_symbols =
'#;'
@@parse_special =
false
@@strict_check =
false
@@strict_double_prefix_check =
false
@@override_phone_data =
nil
@@vanity_conversion =
false

Instance Method Summary collapse

Instance Method Details

#default_countryString|nil

getter method for default_country variable

Returns:

  • (String|nil)

    Default country set for parsing or nil



34
35
36
# File 'lib/phonelib/core.rb', line 34

def default_country
  @@default_country
end

#default_country=(country) ⇒ String|nil

setter method for default_country variable

Parameters:

  • country (String|Symbol)

    default country ISO2 code used for parsing

Returns:

  • (String|nil)

    Default country set for parsing or nil



41
42
43
# File 'lib/phonelib/core.rb', line 41

def default_country=(country)
  @@default_country = country
end

#extension_separate_symbolsString

getter method for extension_separate_symbols variable

Returns:

  • (String)

    Default extension separator symbols used for parsing



66
67
68
# File 'lib/phonelib/core.rb', line 66

def extension_separate_symbols
  @@extension_separate_symbols
end

#extension_separate_symbols=(separator) ⇒ String

setter method for extension_separate_symbols variable

Parameters:

  • separator (String)

    extension separator symbols used for parsing

Returns:

  • (String)

    Default extension separator symbols used for parsing



73
74
75
# File 'lib/phonelib/core.rb', line 73

def extension_separate_symbols=(separator)
  @@extension_separate_symbols = separator
end

#extension_separatorString

getter method for extension_separator variable

Returns:

  • (String)

    Default extension separator used for formatting



50
51
52
# File 'lib/phonelib/core.rb', line 50

def extension_separator
  @@extension_separator
end

#extension_separator=(separator) ⇒ String

setter method for extension_separator variable

Parameters:

  • separator (String)

    extension separator used for formatting

Returns:

  • (String)

    Default extension separator used for formatting



57
58
59
# File 'lib/phonelib/core.rb', line 57

def extension_separator=(separator)
  @@extension_separator = separator
end

#impossible?(phone_number) ⇒ Boolean

method checks if passed phone number is impossible

Parameters:

  • phone_number (String)

    the phone number to be parsed

Returns:

  • (Boolean)

    phone impossible or not



350
351
352
# File 'lib/phonelib/core.rb', line 350

def impossible?(phone_number)
  parse(phone_number).impossible?
end

#invalid?(phone_number) ⇒ Boolean

method checks if passed phone number is invalid

Parameters:

  • phone_number (String)

    the phone number to be parsed

Returns:

  • (Boolean)

    phone invalid or not



336
337
338
# File 'lib/phonelib/core.rb', line 336

def invalid?(phone_number)
  parse(phone_number).invalid?
end

#invalid_for_country?(phone_number, country) ⇒ Boolean

method checks if passed phone number is invalid for provided country

Parameters:

  • phone_number (String)

    the phone number to be parsed

  • country (String)

    ISO2 country code for phone parsing

Returns:

  • (Boolean)

    phone invalid for specified country or not



366
367
368
# File 'lib/phonelib/core.rb', line 366

def invalid_for_country?(phone_number, country)
  parse(phone_number, country).invalid_for_country?(country)
end

#override_phone_dataObject



132
133
134
# File 'lib/phonelib/core.rb', line 132

def override_phone_data
  @@override_phone_data
end

#override_phone_data=(file_path) ⇒ Object

setter for data file to use



128
129
130
# File 'lib/phonelib/core.rb', line 128

def override_phone_data=(file_path)
  @@override_phone_data = file_path
end

#parse(phone, passed_country = nil) ⇒ Phonelib::Phone

method for parsing phone number. On first run fills @@phone_data with data present in yaml file

Parameters:

  • phone (String)

    the phone number to be parsed

  • passed_country (nil|String|Symbol) (defaults to: nil)

    country for phone parsing

Returns:



322
323
324
# File 'lib/phonelib/core.rb', line 322

def parse(phone, passed_country = nil)
  Phonelib::Phone.new phone, passed_country
end

#parse_specialBoolean

getter for flag for special phone types parsing

Returns:

  • (Boolean)

    Flag defines whether to parse special phone types



83
84
85
# File 'lib/phonelib/core.rb', line 83

def parse_special
  @@parse_special
end

#parse_special=(special) ⇒ Boolean

setter for flag for special phone types parsing

Parameters:

  • special (Boolean)

    parse special phone types value

Returns:

  • (Boolean)

    Flag defines whether to parse special phone types



90
91
92
# File 'lib/phonelib/core.rb', line 90

def parse_special=(special)
  @@parse_special = special
end

#phone_dataHash

getter for phone data for other modules of gem, can be used outside

Returns:

  • (Hash)

    all data for phone parsing



9
10
11
# File 'lib/phonelib/core.rb', line 9

def phone_data
  @@phone_data ||= load_data.freeze
end

#phone_ext_dataObject



25
26
27
# File 'lib/phonelib/core.rb', line 25

def phone_ext_data
  @@phone_ext_data ||= load_ext_data.freeze
end

#phone_regexp_cacheObject



17
18
19
# File 'lib/phonelib/core.rb', line 17

def phone_regexp_cache
  @@phone_regexp_cache
end

#possible?(phone_number) ⇒ Boolean

method checks if passed phone number is possible

Parameters:

  • phone_number (String)

    the phone number to be parsed

Returns:

  • (Boolean)

    phone possible or not



343
344
345
# File 'lib/phonelib/core.rb', line 343

def possible?(phone_number)
  parse(phone_number).possible?
end

#strict_checkBoolean

getter for strict check flag

Returns:

  • (Boolean)

    Flag defines whether to do strict parsing check



99
100
101
# File 'lib/phonelib/core.rb', line 99

def strict_check
  @@strict_check
end

#strict_check=(strict) ⇒ Boolean

setter for strict check flag

Parameters:

  • strict (Boolean)

    make a strict parsing or not

Returns:

  • (Boolean)

    Flag defines whether to do strict parsing check



106
107
108
# File 'lib/phonelib/core.rb', line 106

def strict_check=(strict)
  @@strict_check = strict
end

#strict_double_prefix_checkBoolean

getter for strict double prefix check flag

Returns:

  • (Boolean)

    Flag defines whether to do strict double prefix parsing check



115
116
117
# File 'lib/phonelib/core.rb', line 115

def strict_double_prefix_check
  @@strict_double_prefix_check
end

#strict_double_prefix_check=(strict) ⇒ Boolean

setter for strict double prefix check flag

Parameters:

  • strict (Boolean)

    make a strict double prefix parsing or not

Returns:

  • (Boolean)

    Flag defines whether to do strict double prefix parsing check



122
123
124
# File 'lib/phonelib/core.rb', line 122

def strict_double_prefix_check=(strict)
  @@strict_double_prefix_check = strict
end

#valid?(phone_number) ⇒ Boolean

method checks if passed phone number is valid

Parameters:

  • phone_number (String)

    the phone number to be parsed

Returns:

  • (Boolean)

    phone valid or not



329
330
331
# File 'lib/phonelib/core.rb', line 329

def valid?(phone_number)
  parse(phone_number).valid?
end

#valid_for_country?(phone_number, country) ⇒ Boolean

method checks if passed phone number is valid for provided country

Parameters:

  • phone_number (String)

    the phone number to be parsed

  • country (String)

    ISO2 country code for phone parsing

Returns:

  • (Boolean)

    phone valid for specified country or not



358
359
360
# File 'lib/phonelib/core.rb', line 358

def valid_for_country?(phone_number, country)
  parse(phone_number, country).valid_for_country?(country)
end

#vanity_conversionObject



142
143
144
# File 'lib/phonelib/core.rb', line 142

def vanity_conversion
  @@vanity_conversion
end

#vanity_conversion=(value) ⇒ Object

setter for vanity phone numbers chars replacement



138
139
140
# File 'lib/phonelib/core.rb', line 138

def vanity_conversion=(value)
  @@vanity_conversion = value
end