module Phonelib::Core
main module that includes all basic data and methods
Constants
- AREA_CODE_MOBILE_COUNTRIES
-
@private Area code countries for mobile type
- AREA_CODE_MOBILE_TOKENS
-
@private Area code mobile phone token
- AREA_CODE_OPTIONAL
-
@private area code optional type
- AREA_CODE_TYPES
-
@private Area code possible types
- CARRIER_SELECTION_CODES
-
@private carrier selection codes
- CARRIER_SERVICES
-
@private carrier services type
- CARRIER_SPECIFIC
-
@private carrier specific type
- COUNTRY_CODE
-
@private Country code key
- DEFAULT_NUMBER_FORMAT
-
@private Default number formatting data hash
- DIRECTORY_SERVICES
-
@private directory services
- DOUBLE_COUNTRY_PREFIX_FLAG
-
@private Double country prefix flag key
- EMERGENCY
-
@private emergency numbers
- EXPANDED_EMERGENCY
-
@private expendad emergency type
- EXT_CARRIERS
-
@private Extended data carriers array key
- EXT_CARRIER_KEY
-
@private Extended data key for carrier in prefixes hash
- EXT_COUNTRY_NAMES
-
@private Extended data country names array key
- EXT_GEO_NAMES
-
@private Extended data geo names array key
- EXT_GEO_NAME_KEY
-
@private Extended data key for geoname in prefixes hash
- EXT_PREFIXES
-
@private Extended data prefixes hash key
- EXT_TIMEZONES
-
@private Extended data timezones array key
- EXT_TIMEZONE_KEY
-
@private Extended data key for timezone in prefixes hash
- FILE_EXT_DATA
-
@private Extended data file
- FILE_MAIN_DATA
-
@private Main data file
- FIXED_LINE
-
@private Fixed line pattern key
- FIXED_LINE_OR_MOBILE_ARRAY
- FIXED_OR_MOBILE
-
@private In case
MOBILE
and FIXED patterns are the same,this type is returned
- FIXED_OR_MOBILE_ARRAY
-
@private Static arrays used to avoid allocations
- FORMATS
-
@private Formats key
- GENERAL
-
Validation patterns keys constants @private General pattern for country key
- INTERNATIONAL_PREFIX
-
@private International prefix key
- LEADING_DIGITS
-
@private Leading digits key
- MAIN_COUNTRY_FOR_CODE
-
@private Main country for code key
- MOBILE
-
@private Mobile phone number pattern key
- NATIONAL_PREFIX
-
@private National prefix key
- NATIONAL_PREFIX_FOR_PARSING
-
@private National prefix for parsing key
- NATIONAL_PREFIX_RULE
-
@private National prefix rule key
- NATIONAL_PREFIX_TRANSFORM_RULE
-
@private National prefix transform rule key
- NIL_RESULT_ARRAY
- NO_INTERNATIONAL_DIALING
-
@private no international dialling type
- PAGER
-
@private Pager phone number pattern key
- PATTERN
-
@private Pattern key
- PERSONAL_NUMBER
-
@private A personal number is associated with a particular person, and may be routed to either a
MOBILE
orFIXED_LINE
number. - PLUS_SIGN
-
@private Plus sign
- POSSIBLE_PATTERN
-
@private Possible regex pattern key
- POSSIBLE_VALID_ARRAY
- PREMIUM_RATE
-
@private Freephone line pattern key
- SHARED_COST
-
@private Shared cost pattern key. The cost of this call is shared between caller and recipient, and is hence typically less than
PREMIUM_RATE
calls - SHORT
-
@private Short key
- SHORT_CODE
-
@private Short code
- SHORT_CODES
-
@private short codes types keys
- SMS_SERVICES
-
@private SMS Services only type
- STANDARD_RATE
-
@private standard rate type
- TOLL_FREE
-
@private Freephone line pattern key
- TYPES
-
@private Types key
- TYPES_DESC
-
@private hash of all phone types with human representation
- TYPES_DESC_KEYS
- UAN
-
@private Used for ‘Universal Access Numbers’ or ‘Company Numbers’.
They may be further routed to specific offices, but allow one number to be used for a company.
- VALID_PATTERN
-
Internal use keys for validations @private Valid regex pattern key
- VALID_POSSIBLE_ARRAY
- VANITY_4_LETTERS_KEYS_REGEX
-
@private vanity numbers 4 keys letters
- VOICEMAIL
-
@private Used for ‘Voice Mail Access Numbers’.
- VOIP
-
@private VoIP pattern key. This includes TSoIP (Telephony Service over IP)
Public Instance Methods
Source
# File lib/phonelib/core.rb, line 204 def add_additional_regex(country, type, national_regex) return unless Phonelib::Core::TYPES_DESC.key?(type.to_sym) return unless national_regex.is_a?(String) @@phone_data = @@data_by_country_codes = nil @@additional_regexes[country.to_s.upcase] ||= {} @@additional_regexes[country.to_s.upcase][type] ||= [] @@additional_regexes[country.to_s.upcase][type] << national_regex end
Source
# File lib/phonelib/core.rb, line 225 def additional_regexes @@additional_regexes end
Source
# File lib/phonelib/core.rb, line 195 def additional_regexes=(data) return unless data.is_a?(Array) @@additional_regexes = {} data.each do |row| next if row.size != 3 add_additional_regex(*row) end end
setter for data file to use
Source
# File lib/phonelib/core.rb, line 29 def data_by_country_codes @@data_by_country_codes ||= phone_data.each_value.group_by { |d| d[COUNTRY_CODE] }.freeze end
@private getter for phone data indexed by country code (internal use only)
Source
# File lib/phonelib/core.rb, line 59 def default_country @@default_country end
getter method for default_country
variable @return [String,Symbol,Array<String,Symbol>,nil] Default country ISO2 code or codes used for parsing
Source
# File lib/phonelib/core.rb, line 66 def default_country=(country) @@default_country = country end
setter method for default_country
variable @param country [String,Symbol,Array<String,Symbol>] Default country ISO2 code or codes used for parsing @return [String,Symbol,Array<String,Symbol>] Default country ISO2 code or codes used for parsing
Source
# File lib/phonelib/core.rb, line 213 def dump_additional_regexes rows = [] @@additional_regexes.each do |country, types| types.each do |type, regexes| regexes.each do |regex| rows << [country, type, regex] end end end rows end
Source
# File lib/phonelib/core.rb, line 11 def eager_load! return if @@skip_eager_loading phone_data phone_ext_data end
eagerly initialize the gem, loads data into memory. not required, initialization is done lazily otherwise, but may be desirable in production enviroments to avoid initialization time on first use.
Source
# File lib/phonelib/core.rb, line 91 def extension_separate_symbols @@extension_separate_symbols end
getter method for extension_separate_symbols
variable @return [String] Default extension separator symbols used for parsing
Source
# File lib/phonelib/core.rb, line 98 def extension_separate_symbols=(separator) @@extension_separate_symbols = separator end
setter method for extension_separate_symbols
variable @param separator [String] extension separator symbols used for parsing @return [String] Default extension separator symbols used for parsing
Source
# File lib/phonelib/core.rb, line 75 def extension_separator @@extension_separator end
getter method for extension_separator
variable @return [String] Default extension separator used for formatting
Source
# File lib/phonelib/core.rb, line 82 def extension_separator=(separator) @@extension_separator = separator end
setter method for extension_separator
variable @param separator [String] extension separator used for formatting @return [String] Default extension separator used for formatting
Source
# File lib/phonelib/core.rb, line 140 def ignore_plus @@ignore_plus end
getter for ignore plus flag @return [Boolean] Flag defines whether to reset country in case number has + and country prefix doesn’t match
Source
# File lib/phonelib/core.rb, line 147 def ignore_plus=(ignore_plus) @@ignore_plus = ignore_plus end
setter for ignore plus flag @param ignore_plus
[Boolean] ignore plus sign or not @return [Boolean] Flag defines whether to ignore plus for country reset during validations in case country prefix doesn’t match
Source
# File lib/phonelib/core.rb, line 451 def impossible?(phone_number) parse(phone_number).impossible? end
method checks if passed phone number is impossible @param phone_number [String] the phone number to be parsed @return [Boolean] phone impossible or not
Source
# File lib/phonelib/core.rb, line 437 def invalid?(phone_number) parse(phone_number).invalid? end
method checks if passed phone number is invalid @param phone_number [String] the phone number to be parsed @return [Boolean] phone invalid or not
Source
# File lib/phonelib/core.rb, line 467 def invalid_for_country?(phone_number, country) parse(phone_number, country).invalid_for_country?(country) end
method checks if passed phone number is invalid for provided country @param phone_number [String] the phone number to be parsed @param country [String] ISO2 country code for phone parsing @return [Boolean] phone invalid for specified country or not
Source
# File lib/phonelib/core.rb, line 189 def override_phone_data @@override_phone_data end
Source
# File lib/phonelib/core.rb, line 185 def override_phone_data=(file_path) @@override_phone_data = file_path end
setter for data file to use
Source
# File lib/phonelib/core.rb, line 423 def parse(phone, passed_country = nil) Phonelib::Phone.new phone, passed_country end
method for parsing phone number. On first run fills @@phone_data with data present in yaml file @param phone [String] the phone number to be parsed @param passed_country [nil|String|Symbol] country for phone parsing @return [Phonelib::Phone] parsed phone entity
Source
# File lib/phonelib/core.rb, line 108 def parse_special @@parse_special end
getter for flag for special phone types parsing @return [Boolean] Flag defines whether to parse special phone types
Source
# File lib/phonelib/core.rb, line 115 def parse_special=(special) @@parse_special = special end
setter for flag for special phone types parsing @param special [Boolean] parse special phone types value @return [Boolean] Flag defines whether to parse special phone types
Source
# File lib/phonelib/core.rb, line 24 def phone_data @@phone_data ||= load_data.freeze end
getter for phone data for other modules of gem, can be used outside @return [Hash] all data for phone parsing
Source
# File lib/phonelib/core.rb, line 34 def phone_data_int_prefixes @@all_int_prefixes ||= phone_data.map {|k,v| v[:international_prefix] }.select { |v| v != '' }.compact.uniq.join('|').freeze end
@private getter for all international prefixes in phone_data
Source
# File lib/phonelib/core.rb, line 50 def phone_ext_data @@phone_ext_data ||= load_ext_data.freeze end
@private getter for extended phone data
Source
# File lib/phonelib/core.rb, line 42 def phone_regexp_cache @@phone_regexp_cache end
@private getter for phone regexp cache (internal use only)
Source
# File lib/phonelib/core.rb, line 444 def possible?(phone_number) parse(phone_number).possible? end
method checks if passed phone number is possible @param phone_number [String] the phone number to be parsed @return [Boolean] phone possible or not
Source
# File lib/phonelib/core.rb, line 156 def sanitize_regex @@sanitize_regex end
getter for sanitize regex @return [String] regex of symbols to wipe from parsed number
Source
# File lib/phonelib/core.rb, line 163 def sanitize_regex=(regex) @@sanitize_regex = regex.is_a?(String) ? regex : regex.to_s end
setter for sanitize regex @param regex [String] symbols to wipe from parsed number @return [String] regex of symbols to wipe from parsed number
Source
# File lib/phonelib/core.rb, line 18 def skip_eager_loading! @@skip_eager_loading = true end
Source
# File lib/phonelib/core.rb, line 124 def strict_check @@strict_check end
getter for strict check flag @return [Boolean] Flag defines whether to do strict parsing check
Source
# File lib/phonelib/core.rb, line 131 def strict_check=(strict) @@strict_check = strict end
setter for strict check flag @param strict [Boolean] make a strict parsing or not @return [Boolean] Flag defines whether to do strict parsing check
Source
# File lib/phonelib/core.rb, line 172 def strict_double_prefix_check @@strict_double_prefix_check end
getter for strict double prefix check flag @return [Boolean] Flag defines whether to do strict double prefix parsing check
Source
# File lib/phonelib/core.rb, line 179 def strict_double_prefix_check=(strict) @@strict_double_prefix_check = strict end
setter for strict double prefix check flag @param strict [Boolean] make a strict double prefix parsing or not @return [Boolean] Flag defines whether to do strict double prefix parsing check
Source
# File lib/phonelib/core.rb, line 430 def valid?(phone_number) parse(phone_number).valid? end
method checks if passed phone number is valid @param phone_number [String] the phone number to be parsed @return [Boolean] phone valid or not
Source
# File lib/phonelib/core.rb, line 459 def valid_for_country?(phone_number, country) parse(phone_number, country).valid_for_country?(country) end
method checks if passed phone number is valid for provided country @param phone_number [String] the phone number to be parsed @param country [String] ISO2 country code for phone parsing @return [Boolean] phone valid for specified country or not
Source
# File lib/phonelib/core.rb, line 235 def vanity_conversion @@vanity_conversion end
Source
# File lib/phonelib/core.rb, line 231 def vanity_conversion=(value) @@vanity_conversion = value end
setter for vanity phone numbers chars replacement
Private Instance Methods
Source
# File lib/phonelib/core.rb, line 474 def load_data data_file = "#{File.dirname(__FILE__)}/../../#{FILE_MAIN_DATA}" default_data = Marshal.load(File.binread(data_file)) if override_phone_data override_data_file = Marshal.load(File.binread(override_phone_data)) default_data.merge!(override_data_file) end additional_regexes.each do |country, types| types.each do |type, regex| default_data[country][Core::TYPES][type] ||= {} [Core::VALID_PATTERN, Core::POSSIBLE_PATTERN].each do |key| if default_data[country][Core::TYPES][type][key] default_data[country][Core::TYPES][type][key] << "|#{regex.join('|')}" else default_data[country][Core::TYPES][type][key] = regex.join('|') end if type != Core::GENERAL default_data[country][Core::TYPES][Core::GENERAL][key] << "|#{regex.join('|')}" end end end end default_data end
@private Load data file into memory
Source
# File lib/phonelib/core.rb, line 500 def load_ext_data data_file = "#{File.dirname(__FILE__)}/../../#{FILE_EXT_DATA}" Marshal.load(File.binread(data_file)) end
@private Load extended data file into memory