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
- @@skip_eager_loading =
false
- @@phone_regexp_cache =
{}
- @@phone_ext_data =
nil
- @@default_country =
nil
- @@extension_separator =
';'
- @@extension_separate_symbols =
'#;'
- @@parse_special =
false
- @@strict_check =
false
- @@ignore_plus =
false
- @@sanitize_regex =
'[^0-9]+'
- @@strict_double_prefix_check =
false
- @@override_phone_data =
nil
- @@additional_regexes =
{}
- @@vanity_conversion =
false
Instance Method Summary collapse
- #add_additional_regex(country, type, national_regex) ⇒ Object
- #additional_regexes ⇒ Object
-
#additional_regexes=(data) ⇒ Object
setter for data file to use.
- #data_by_country_codes ⇒ Object
-
#default_country ⇒ String, ...
getter method for default_country variable.
-
#default_country=(country) ⇒ String, ...
setter method for default_country variable.
- #dump_additional_regexes ⇒ Object
-
#eager_load! ⇒ Object
eagerly initialize the gem, loads data into memory.
-
#extension_separate_symbols ⇒ String
getter method for extension_separate_symbols variable.
-
#extension_separate_symbols=(separator) ⇒ String
setter method for extension_separate_symbols variable.
-
#extension_separator ⇒ String
getter method for extension_separator variable.
-
#extension_separator=(separator) ⇒ String
setter method for extension_separator variable.
-
#ignore_plus ⇒ Boolean
getter for ignore plus flag.
-
#ignore_plus=(ignore_plus) ⇒ Boolean
setter for ignore plus flag.
-
#impossible?(phone_number) ⇒ Boolean
method checks if passed phone number is impossible.
-
#invalid?(phone_number) ⇒ Boolean
method checks if passed phone number is invalid.
-
#invalid_for_country?(phone_number, country) ⇒ Boolean
method checks if passed phone number is invalid for provided country.
- #override_phone_data ⇒ Object
-
#override_phone_data=(file_path) ⇒ Object
setter for data file to use.
-
#parse(phone, passed_country = nil) ⇒ Phonelib::Phone
method for parsing phone number.
-
#parse_special ⇒ Boolean
getter for flag for special phone types parsing.
-
#parse_special=(special) ⇒ Boolean
setter for flag for special phone types parsing.
-
#phone_data ⇒ Hash
getter for phone data for other modules of gem, can be used outside.
- #phone_data_int_prefixes ⇒ Object
- #phone_ext_data ⇒ Object
- #phone_regexp_cache ⇒ Object
-
#possible?(phone_number) ⇒ Boolean
method checks if passed phone number is possible.
-
#sanitize_regex ⇒ String
getter for sanitize regex.
-
#sanitize_regex=(regex) ⇒ String
setter for sanitize regex.
- #skip_eager_loading! ⇒ Object
-
#strict_check ⇒ Boolean
getter for strict check flag.
-
#strict_check=(strict) ⇒ Boolean
setter for strict check flag.
-
#strict_double_prefix_check ⇒ Boolean
getter for strict double prefix check flag.
-
#strict_double_prefix_check=(strict) ⇒ Boolean
setter for strict double prefix check flag.
-
#valid?(phone_number) ⇒ Boolean
method checks if passed phone number is valid.
-
#valid_for_country?(phone_number, country) ⇒ Boolean
method checks if passed phone number is valid for provided country.
- #vanity_conversion ⇒ Object
-
#vanity_conversion=(value) ⇒ Object
setter for vanity phone numbers chars replacement.
Instance Method Details
#add_additional_regex(country, type, national_regex) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/phonelib/core.rb', line 204 def add_additional_regex(country, type, national_regex) return unless Phonelib::Core::TYPES_DESC.keys.include?(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 |
#additional_regexes ⇒ Object
225 226 227 |
# File 'lib/phonelib/core.rb', line 225 def additional_regexes @@additional_regexes end |
#additional_regexes=(data) ⇒ Object
setter for data file to use
195 196 197 198 199 200 201 202 |
# 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 |
#data_by_country_codes ⇒ Object
29 30 31 |
# 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 |
#default_country ⇒ String, ...
getter method for default_country variable
59 60 61 |
# File 'lib/phonelib/core.rb', line 59 def default_country @@default_country end |
#default_country=(country) ⇒ String, ...
setter method for default_country variable
66 67 68 |
# File 'lib/phonelib/core.rb', line 66 def default_country=(country) @@default_country = country end |
#dump_additional_regexes ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 |
# 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 |
#eager_load! ⇒ Object
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.
11 12 13 14 15 |
# File 'lib/phonelib/core.rb', line 11 def eager_load! return if @@skip_eager_loading phone_data phone_ext_data end |
#extension_separate_symbols ⇒ String
getter method for extension_separate_symbols variable
91 92 93 |
# File 'lib/phonelib/core.rb', line 91 def extension_separate_symbols @@extension_separate_symbols end |
#extension_separate_symbols=(separator) ⇒ String
setter method for extension_separate_symbols variable
98 99 100 |
# File 'lib/phonelib/core.rb', line 98 def extension_separate_symbols=(separator) @@extension_separate_symbols = separator end |
#extension_separator ⇒ String
getter method for extension_separator variable
75 76 77 |
# File 'lib/phonelib/core.rb', line 75 def extension_separator @@extension_separator end |
#extension_separator=(separator) ⇒ String
setter method for extension_separator variable
82 83 84 |
# File 'lib/phonelib/core.rb', line 82 def extension_separator=(separator) @@extension_separator = separator end |
#ignore_plus ⇒ Boolean
getter for ignore plus flag
140 141 142 |
# File 'lib/phonelib/core.rb', line 140 def ignore_plus @@ignore_plus end |
#ignore_plus=(ignore_plus) ⇒ Boolean
setter for ignore plus flag
147 148 149 |
# File 'lib/phonelib/core.rb', line 147 def ignore_plus=(ignore_plus) @@ignore_plus = ignore_plus end |
#impossible?(phone_number) ⇒ Boolean
method checks if passed phone number is impossible
443 444 445 |
# File 'lib/phonelib/core.rb', line 443 def impossible?(phone_number) parse(phone_number).impossible? end |
#invalid?(phone_number) ⇒ Boolean
method checks if passed phone number is invalid
429 430 431 |
# File 'lib/phonelib/core.rb', line 429 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
459 460 461 |
# File 'lib/phonelib/core.rb', line 459 def invalid_for_country?(phone_number, country) parse(phone_number, country).invalid_for_country?(country) end |
#override_phone_data ⇒ Object
189 190 191 |
# File 'lib/phonelib/core.rb', line 189 def override_phone_data @@override_phone_data end |
#override_phone_data=(file_path) ⇒ Object
setter for data file to use
185 186 187 |
# File 'lib/phonelib/core.rb', line 185 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
415 416 417 |
# File 'lib/phonelib/core.rb', line 415 def parse(phone, passed_country = nil) Phonelib::Phone.new phone, passed_country end |
#parse_special ⇒ Boolean
getter for flag for special phone types parsing
108 109 110 |
# File 'lib/phonelib/core.rb', line 108 def parse_special @@parse_special end |
#parse_special=(special) ⇒ Boolean
setter for flag for special phone types parsing
115 116 117 |
# File 'lib/phonelib/core.rb', line 115 def parse_special=(special) @@parse_special = special end |
#phone_data ⇒ Hash
getter for phone data for other modules of gem, can be used outside
24 25 26 |
# File 'lib/phonelib/core.rb', line 24 def phone_data @@phone_data ||= load_data.freeze end |
#phone_data_int_prefixes ⇒ Object
34 35 36 |
# 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 |
#phone_ext_data ⇒ Object
50 51 52 |
# File 'lib/phonelib/core.rb', line 50 def phone_ext_data @@phone_ext_data ||= load_ext_data.freeze end |
#phone_regexp_cache ⇒ Object
42 43 44 |
# File 'lib/phonelib/core.rb', line 42 def phone_regexp_cache @@phone_regexp_cache end |
#possible?(phone_number) ⇒ Boolean
method checks if passed phone number is possible
436 437 438 |
# File 'lib/phonelib/core.rb', line 436 def possible?(phone_number) parse(phone_number).possible? end |
#sanitize_regex ⇒ String
getter for sanitize regex
156 157 158 |
# File 'lib/phonelib/core.rb', line 156 def sanitize_regex @@sanitize_regex end |
#sanitize_regex=(regex) ⇒ String
setter for sanitize regex
163 164 165 |
# File 'lib/phonelib/core.rb', line 163 def sanitize_regex=(regex) @@sanitize_regex = regex.is_a?(String) ? regex : regex.to_s end |
#skip_eager_loading! ⇒ Object
18 19 20 |
# File 'lib/phonelib/core.rb', line 18 def skip_eager_loading! @@skip_eager_loading = true end |
#strict_check ⇒ Boolean
getter for strict check flag
124 125 126 |
# File 'lib/phonelib/core.rb', line 124 def strict_check @@strict_check end |
#strict_check=(strict) ⇒ Boolean
setter for strict check flag
131 132 133 |
# File 'lib/phonelib/core.rb', line 131 def strict_check=(strict) @@strict_check = strict end |
#strict_double_prefix_check ⇒ Boolean
getter for strict double prefix check flag
172 173 174 |
# File 'lib/phonelib/core.rb', line 172 def strict_double_prefix_check @@strict_double_prefix_check end |
#strict_double_prefix_check=(strict) ⇒ Boolean
setter for strict double prefix check flag
179 180 181 |
# File 'lib/phonelib/core.rb', line 179 def strict_double_prefix_check=(strict) @@strict_double_prefix_check = strict end |
#valid?(phone_number) ⇒ Boolean
method checks if passed phone number is valid
422 423 424 |
# File 'lib/phonelib/core.rb', line 422 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
451 452 453 |
# File 'lib/phonelib/core.rb', line 451 def valid_for_country?(phone_number, country) parse(phone_number, country).valid_for_country?(country) end |
#vanity_conversion ⇒ Object
235 236 237 |
# File 'lib/phonelib/core.rb', line 235 def vanity_conversion @@vanity_conversion end |
#vanity_conversion=(value) ⇒ Object
setter for vanity phone numbers chars replacement
231 232 233 |
# File 'lib/phonelib/core.rb', line 231 def vanity_conversion=(value) @@vanity_conversion = value end |