class Phonelib::Phone
class for parsed phone number, includes validation and formatting methods
Attributes
@!attribute [r] extension @return [String] phone extension passed for parsing after a number
@!attribute [r] national_number
@return [String] phone national number
@!attribute [r] original @return [String] original phone number passed for parsing
Public Class Methods
class initialization method @param phone [String] Phone
number for parsing @param country [String|Symbol] Country specification for parsing.
Must be ISO code of country (2 letters) like 'US', 'us' or :us for United States
@return [Phonelib::Phone] parsed phone instance
# File lib/phonelib/phone.rb, line 25 def initialize(phone, country = nil) @original, @extension = separate_extension(phone.to_s) @extension.gsub!(/[^0-9]/, '') if @extension if sanitized.empty? @data = {} else @data = analyze(sanitized, passed_country(country)) first = @data.values.first @national_number = first ? first[:national] : sanitized end end
Public Instance Methods
Compare a phone number against a string or other parsed number @param other [String|Phonelib::Phone] Phone
number to compare against @return [Boolean] result of equality comparison
# File lib/phonelib/phone.rb, line 46 def ==(other) other = Phonelib.parse(other) unless other.is_a?(Phonelib::Phone) return (e164 == other.e164) if valid? && other.valid? original == other.original end
Returns all countries that matched valid patterns @return [Array] Possible ISO2 country codes array
# File lib/phonelib/phone.rb, line 93 def countries @data.map { |iso2, _data| iso2 } end
Returns first country that matched valid patterns @return [String] valid country ISO2 code or first matched country code
# File lib/phonelib/phone.rb, line 113 def country @country ||= valid_country || main_country(countries) end
Return human representation of phone type @return [String] Human readable valid phone type
# File lib/phonelib/phone.rb, line 87 def human_type Core::TYPES_DESC[type] end
Returns human representation of all matched phone types @return [Array] Array of human readable valid phone types
# File lib/phonelib/phone.rb, line 81 def human_types types.map { |type| Core::TYPES_DESC[type] } end
Returns whether a current parsed phone number is impossible @return [Boolean] parsed phone is impossible
# File lib/phonelib/phone.rb, line 137 def impossible? !possible? end
Returns whether a current parsed phone number is invalid @return [Boolean] parsed phone is invalid
# File lib/phonelib/phone.rb, line 125 def invalid? !valid? end
Returns whether a current parsed phone number is invalid for specified country @param country [String|Symbol] ISO code of country (2 letters) like 'US',
'us' or :us for United States
@return [Boolean] parsed phone number is invalid
# File lib/phonelib/phone.rb, line 173 def invalid_for_country?(country) !valid_for_country?(country) end
returns local number @return [String] local number
# File lib/phonelib/phone.rb, line 143 def local_number return national unless possible? format_match, format_string = formatting_data if format_string =~ /^.*[0-9]+.*\$1/ && format_match format_string.gsub(/^.*\$2/, '$2') .gsub(/\$\d/) { |el| format_match[el[1].to_i] } else national end end
Returns whether a current parsed phone number is possible @return [Boolean] parsed phone is possible
# File lib/phonelib/phone.rb, line 131 def possible? @possible ||= @data.select { |_iso2, data| data[:possible].any? }.any? end
Returns all possible types that matched possible patterns @return [Array] all possible phone types
# File lib/phonelib/phone.rb, line 69 def possible_types @possible_types ||= @data.flat_map { |_iso2, data| data[:possible] }.uniq end
method to get sanitized phone number (only numbers) @return [String] Sanitized phone number
# File lib/phonelib/phone.rb, line 54 def sanitized @sanitized ||= vanity_converted(@original).gsub( Phonelib.strict_check ? cr('^\+') : cr(Phonelib.sanitize_regex), '') end
method returns string representation of parsed phone
# File lib/phonelib/phone.rb, line 39 def to_s valid? ? e164 : original end
Returns first phone type that matched @return [Symbol] valid phone type
# File lib/phonelib/phone.rb, line 75 def type types.first end
Returns all phone types that matched valid patterns @return [Array] all valid phone types
# File lib/phonelib/phone.rb, line 63 def types @types ||= @data.flat_map { |_iso2, data| data[:valid] }.uniq end
Returns whether a current parsed phone number is valid @return [Boolean] parsed phone is valid
# File lib/phonelib/phone.rb, line 119 def valid? @valid ||= @data.select { |_iso2, data| data[:valid].any? }.any? end
Return countries with valid patterns @return [Array] Valid ISO2 country codes array
# File lib/phonelib/phone.rb, line 99 def valid_countries @valid_countries ||= countries.select do |iso2| @data[iso2][:valid].any? end end
Return valid country @return [String] valid ISO2 country code
# File lib/phonelib/phone.rb, line 107 def valid_country @valid_country ||= main_country(valid_countries) end
Returns whether a current parsed phone number is valid for specified country @param country [String|Symbol] ISO code of country (2 letters) like 'US',
'us' or :us for United States
@return [Boolean] parsed phone number is valid
# File lib/phonelib/phone.rb, line 160 def valid_for_country?(country) country = country.to_s.upcase tdata = analyze(sanitized, passed_country(country)) tdata.find do |iso2, data| country == iso2 && data[:valid].any? end.is_a? Array end
Private Instance Methods
@private get main country for code among provided countries
# File lib/phonelib/phone.rb, line 191 def main_country(countries_array) countries_array.find do |iso2| @data[iso2][Core::MAIN_COUNTRY_FOR_CODE] == 'true' end || countries_array.first end
@private extracts extension from passed phone number if provided
# File lib/phonelib/phone.rb, line 180 def separate_extension(original) regex = if Phonelib.extension_separate_symbols.is_a?(Array) cr("#{Phonelib.extension_separate_symbols.join('|')}") else cr("[#{Phonelib.extension_separate_symbols}]") end split = (original || '').split regex [split.first || '', split[1..-1] && split[1..-1].join || ''] end