lib/phonelib/phone_analyzer_helper.rb in phonelib-0.6.2 vs lib/phonelib/phone_analyzer_helper.rb in phonelib-0.6.3
- old
+ new
@@ -1,24 +1,26 @@
module Phonelib
- # helper methods for analyser
+ # @private helper methods for analyser
module PhoneAnalyzerHelper
private
# defines if to validate against single country or not
def passed_country(country)
- country_code = country_prefix(country)
- if @original.start_with?('+') && country_code && !sanitized.start_with?(country_code)
- # in case number passed with + but it doesn't start with passed country prefix
+ code = country_prefix(country)
+ if @original.start_with?('+') && code && !sanitized.start_with?(code)
+ # in case number passed with + but it doesn't start with passed
+ # country prefix
country = nil
end
country
end
# returns country prefix for provided country or nil
def country_prefix(country)
country = country.to_s.upcase
- Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
+ Phonelib.phone_data[country] && \
+ Phonelib.phone_data[country][Core::COUNTRY_CODE]
end
# caches regular expression, reusing it for later lookups
def cr(regexp)
Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(regexp)
@@ -31,21 +33,26 @@
# * +data+ - country data used for parsing
# * +phone+ - phone number being parsed
# * +parsed+ - parsed regex match for phone
def allows_double_prefix(data, phone, parsed)
data[Core::DOUBLE_COUNTRY_PREFIX_FLAG] &&
- phone =~ cr("^#{data[Core::COUNTRY_CODE]}") &&
- parsed && (parsed[:valid].nil? || parsed[:valid].empty?)
+ phone =~ cr("^#{data[Core::COUNTRY_CODE]}") &&
+ parsed && (parsed[:valid].nil? || parsed[:valid].empty?)
end
+ # Returns original number passed if it's a string or empty string otherwise
+ def original_string
+ @original.is_a?(String) ? @original : ''
+ end
+
# Get country that was provided or default country in needable format
#
# ==== Attributes
#
# * +country+ - country passed for parsing
def country_or_default_country(country)
- country = country || Phonelib.default_country
+ country ||= (original_string.start_with?('+') ? nil : Phonelib.default_country)
country && country.to_s.upcase
end
# constructs full regex for phone validation for provided phone data
# (international prefix, country code, national prefix, valid number)
@@ -141,6 +148,6 @@
end
[valid && possible, possible]
end
end
-end
\ No newline at end of file
+end