module Phonelib::PhoneAnalyzerHelper
@private helper methods for analyser
Private Instance Methods
changes phone to with/without double country prefix
# File lib/phonelib/phone_analyzer_helper.rb, line 55 def changed_dp_phone(country, phone) data = Phonelib.phone_data[country] return if data.nil? || data[Core::DOUBLE_COUNTRY_PREFIX_FLAG].nil? country_code = Phonelib.phone_data[country][Core::COUNTRY_CODE] if phone.start_with? country_code * 2 phone.gsub(cr("^#{country_code}"), '') else "#{country_code}#{phone}" end end
defines whether country can have double country prefix in number
# File lib/phonelib/phone_analyzer_helper.rb, line 48 def country_can_dp?(country) Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::DOUBLE_COUNTRY_PREFIX_FLAG] && !original_starts_with_plus? end
Get country that was provided or default country in needable format
Attributes¶ ↑
-
country
- country passed for parsing
# File lib/phonelib/phone_analyzer_helper.rb, line 91 def country_or_default_country(country) country ||= (original_starts_with_plus? ? nil : Phonelib.default_country) country && country.to_s.upcase end
returns country prefix for provided country or nil
# File lib/phonelib/phone_analyzer_helper.rb, line 36 def country_prefix(country) country = country.to_s.upcase Phonelib.phone_data[country] && \ Phonelib.phone_data[country][Core::COUNTRY_CODE] end
caches regular expression, reusing it for later lookups
# File lib/phonelib/phone_analyzer_helper.rb, line 43 def cr(regexp) Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(regexp).freeze end
checks if country can have numbers with double country prefixes
Attributes¶ ↑
-
data
- country data used for parsing -
phone
- phone number being parsed -
parsed
- parsed regex match for phone
# File lib/phonelib/phone_analyzer_helper.rb, line 74 def double_prefix_allowed?(data, phone, parsed = {}) data[Core::DOUBLE_COUNTRY_PREFIX_FLAG] && phone =~ cr("^#{data[Core::COUNTRY_CODE]}") && parsed && (parsed[:valid].nil? || parsed[:valid].empty?) && !original_starts_with_plus? end
Checks if fixed line pattern and mobile pattern are the same and returns appropriate keys
Attributes¶ ↑
-
data
- country data
# File lib/phonelib/phone_analyzer_helper.rb, line 172 def fixed_and_mobile_keys(data) if data[Core::FIXED_LINE] == data[Core::MOBILE] [Core::FIXED_OR_MOBILE] else [Core::FIXED_LINE, Core::MOBILE] end end
constructs full regex for phone validation for provided phone data (international prefix, country code, national prefix, valid number)
Attributes¶ ↑
-
data
- country data hash -
country_optional
- whether to put country code as optional group
# File lib/phonelib/phone_analyzer_helper.rb, line 103 def full_regex_for_data(data, type, country_optional = true) regex = [] regex << '0{2}?' regex << "(#{data[Core::INTERNATIONAL_PREFIX]})?" regex << "(#{data[Core::COUNTRY_CODE]})#{country_optional ? '?' : ''}" regex << "(#{data[Core::NATIONAL_PREFIX_FOR_PARSING] || data[Core::NATIONAL_PREFIX]})?" regex << "(#{type_regex(data[Core::TYPES][Core::GENERAL], type)})" if data[Core::TYPES] cr("^#{regex.join}$") end
Checks number against regex and compares match length
Attributes¶ ↑
-
number
- phone number for validation -
regex
- regex for perfoming a validation
# File lib/phonelib/phone_analyzer_helper.rb, line 203 def number_match?(number, regex) match = number.match(cr("^(?:#{regex})$")) match && match.to_s.length == number.length end
Checks if passed number matches valid and possible patterns
Attributes¶ ↑
-
number
- phone number for validation -
p_regex
- possible regex pattern for validation -
v_regex
- valid regex pattern for validation -
not_valid
- specifies that number is not valid by general desc pattern
# File lib/phonelib/phone_analyzer_helper.rb, line 188 def number_valid_and_possible?(number, p_regex, v_regex, not_valid = false) possible = number_match?(number, p_regex) return [!not_valid && possible, possible] if p_regex == v_regex valid = !not_valid && possible && number_match?(number, v_regex) [valid && possible, possible] end
Returns original number passed if it's a string or empty string otherwise
# File lib/phonelib/phone_analyzer_helper.rb, line 82 def original_s @original_s ||= @original.is_a?(String) ? @original : '' end
# File lib/phonelib/phone_analyzer_helper.rb, line 6 def original_starts_with_plus? original_s[0] == Core::PLUS_SIGN end
defines if to validate against single country or not
# File lib/phonelib/phone_analyzer_helper.rb, line 25 def passed_country(country) code = country_prefix(country) if Core::PLUS_SIGN == @original[0] && code && !sanitized.start_with?(code) # in case number passed with + but it doesn't start with passed # country prefix country = nil end country end
Check if phone match country data
Attributes¶ ↑
-
phone
- phone number for parsing -
data
- country data
# File lib/phonelib/phone_analyzer_helper.rb, line 134 def phone_match_data?(phone, data, possible = false) country_code = "#{data[Core::COUNTRY_CODE]}" inter_prefix = "(#{data[Core::INTERNATIONAL_PREFIX]})?" return unless phone.match cr("^0{2}?#{inter_prefix}#{country_code}") type = possible ? Core::POSSIBLE_PATTERN : Core::VALID_PATTERN phone.match full_regex_for_data(data, type, false) end
checks if types has both :mobile and :fixed_line and replaces it with :fixed_or_mobile in case both present
# File lib/phonelib/phone_analyzer_helper.rb, line 145 def sanitize_fixed_mobile(types) fixed_mobile = [Core::FIXED_LINE, Core::MOBILE] [:possible, :valid].each do |key| if (fixed_mobile - types[key]).empty? types[key] = types[key] - fixed_mobile + [Core::FIXED_OR_MOBILE] end end types end
Returns regex for type with special types if needed
Attributes¶ ↑
-
data
- country types data for single type -
type
- possible or valid regex type needed
# File lib/phonelib/phone_analyzer_helper.rb, line 120 def type_regex(data, type) regex = [data[type]] if Phonelib.parse_special && data[Core::SHORT] && data[Core::SHORT][type] regex << data[Core::SHORT][type] end regex.join('|') end
returns array of phone types for check for current country data
Attributes¶ ↑
-
data
- country data hash
# File lib/phonelib/phone_analyzer_helper.rb, line 160 def types_for_check(data) exclude_list = PhoneAnalyzer::NOT_FOR_CHECK exclude_list += Phonelib::Core::SHORT_CODES unless Phonelib.parse_special Core::TYPES_DESC.keys - exclude_list + fixed_and_mobile_keys(data) end
converts symbols in phone to numbers
# File lib/phonelib/phone_analyzer_helper.rb, line 11 def vanity_converted(phone) return phone unless Phonelib.vanity_conversion (phone || '').gsub(cr('[a-zA-Z]')) do |c| c.upcase! # subtract "A" n = (c.ord - 65) / 3 # account for #7 & #9 which have 4 chars n -= 1 if c.match(Core::VANITY_4_LETTERS_KEYS_REGEX) (n + 2).to_s end end