Class: Phonelib::Phone
- Inherits:
-
Object
- Object
- Phonelib::Phone
- Includes:
- PhoneAnalyzer, PhoneExtendedData, PhoneFormatter
- Defined in:
- lib/phonelib/phone.rb
Overview
class for parsed phone number, includes validation and formatting methods
Constant Summary
Constants included from PhoneExtendedData
Phonelib::PhoneExtendedData::EXT_KEYS
Constants included from PhoneAnalyzer
Phonelib::PhoneAnalyzer::NOT_FOR_CHECK
Instance Attribute Summary collapse
-
#extension ⇒ String
readonly
Phone extension passed for parsing after a number.
-
#national_number ⇒ String
readonly
Phone national number.
-
#original ⇒ String
readonly
Original phone number passed for parsing.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare a phone number against a string or other parsed number.
-
#countries ⇒ Array
Returns all countries that matched valid patterns.
-
#country ⇒ String
Returns first country that matched valid patterns.
-
#human_type ⇒ String
Return human representation of phone type.
-
#human_types ⇒ Array
Returns human representation of all matched phone types.
-
#impossible? ⇒ Boolean
Returns whether a current parsed phone number is impossible.
-
#initialize(phone, country = nil) ⇒ Phonelib::Phone
constructor
class initialization method.
-
#invalid? ⇒ Boolean
Returns whether a current parsed phone number is invalid.
-
#invalid_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is invalid for specified country.
-
#local_number ⇒ String
returns local number.
-
#possible? ⇒ Boolean
Returns whether a current parsed phone number is possible.
-
#possible_types ⇒ Array
Returns all possible types that matched possible patterns.
-
#sanitized ⇒ String
method to get sanitized phone number (only numbers).
-
#to_s ⇒ Object
method returns string representation of parsed phone.
-
#type ⇒ Symbol
Returns first phone type that matched.
-
#types ⇒ Array
Returns all phone types that matched valid patterns.
-
#valid? ⇒ Boolean
Returns whether a current parsed phone number is valid.
-
#valid_countries ⇒ Array
Return countries with valid patterns.
-
#valid_country ⇒ String
Return valid country.
-
#valid_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is valid for specified country.
Methods included from PhoneFormatter
#area_code, #country_code, #e164, #full_e164, #full_international, #full_national, #international, #method_missing, #national, #raw_national
Methods included from PhoneExtendedData
#carrier, #geo_name, #timezone, #timezones, #valid_country_name
Methods included from PhoneAnalyzer
Constructor Details
#initialize(phone, country = nil) ⇒ Phonelib::Phone
class initialization method
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/phonelib/phone.rb', line 27 def initialize(phone, country = nil) @original, @extension = separate_extension(phone.to_s) @extension = @extension.gsub(/[^0-9]/, '') if @extension if sanitized.empty? @data = {} else @data = analyze(sanitized, passed_country(country)) # the national number must come from the country #country reports: # another entry can be a possible-only match, and mixing entries makes # e164 splice one country's code onto another's number (issue #355) @national_number = country_data ? country_data[:national] : sanitized end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Phonelib::PhoneFormatter
Instance Attribute Details
#extension ⇒ String (readonly)
Returns phone extension passed for parsing after a number.
11 12 13 |
# File 'lib/phonelib/phone.rb', line 11 def extension @extension end |
#national_number ⇒ String (readonly)
Returns phone national number.
14 15 16 |
# File 'lib/phonelib/phone.rb', line 14 def national_number @national_number end |
#original ⇒ String (readonly)
Returns original phone number passed for parsing.
8 9 10 |
# File 'lib/phonelib/phone.rb', line 8 def original @original end |
Instance Method Details
#==(other) ⇒ Boolean
Compare a phone number against a string or other parsed number
50 51 52 53 54 |
# File 'lib/phonelib/phone.rb', line 50 def ==(other) other = Phonelib.parse(other) unless other.is_a?(Phonelib::Phone) return (e164 == other.e164) if valid? && other.valid? original == other.original end |
#countries ⇒ Array
Returns all countries that matched valid patterns
97 98 99 |
# File 'lib/phonelib/phone.rb', line 97 def countries @countries ||= @data.keys end |
#country ⇒ String
Returns first country that matched valid patterns
119 120 121 122 123 |
# File 'lib/phonelib/phone.rb', line 119 def country return @country if defined?(@country) @country = valid_country || main_country(countries) end |
#human_type ⇒ String
Return human representation of phone type
91 92 93 |
# File 'lib/phonelib/phone.rb', line 91 def human_type Core::TYPES_DESC[type] end |
#human_types ⇒ Array
Returns human representation of all matched phone types
85 86 87 |
# File 'lib/phonelib/phone.rb', line 85 def human_types types.map { |type| Core::TYPES_DESC[type] } end |
#impossible? ⇒ Boolean
Returns whether a current parsed phone number is impossible
149 150 151 |
# File 'lib/phonelib/phone.rb', line 149 def impossible? !possible? end |
#invalid? ⇒ Boolean
Returns whether a current parsed phone number is invalid
135 136 137 |
# File 'lib/phonelib/phone.rb', line 135 def invalid? !valid? end |
#invalid_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is invalid for specified country
185 186 187 |
# File 'lib/phonelib/phone.rb', line 185 def invalid_for_country?(country) !valid_for_country?(country) end |
#local_number ⇒ String
returns local number
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/phonelib/phone.rb', line 155 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 |
#possible? ⇒ Boolean
Returns whether a current parsed phone number is possible
141 142 143 144 145 |
# File 'lib/phonelib/phone.rb', line 141 def possible? return @possible if defined?(@possible) @possible = @data.any? { |_iso2, data| data[:possible].any? } end |
#possible_types ⇒ Array
Returns all possible types that matched possible patterns
73 74 75 |
# File 'lib/phonelib/phone.rb', line 73 def possible_types @possible_types ||= @data.flat_map { |_iso2, data| data[:possible] }.uniq end |
#sanitized ⇒ String
method to get sanitized phone number (only numbers)
58 59 60 61 62 63 |
# File 'lib/phonelib/phone.rb', line 58 def sanitized @sanitized ||= vanity_converted(@original).gsub( Phonelib.strict_check ? cr('^\+') : cr(Phonelib.sanitize_regex), '') end |
#to_s ⇒ Object
method returns string representation of parsed phone
43 44 45 |
# File 'lib/phonelib/phone.rb', line 43 def to_s valid? ? e164 : original end |
#type ⇒ Symbol
Returns first phone type that matched
79 80 81 |
# File 'lib/phonelib/phone.rb', line 79 def type types.first end |
#types ⇒ Array
Returns all phone types that matched valid patterns
67 68 69 |
# File 'lib/phonelib/phone.rb', line 67 def types @types ||= @data.flat_map { |_iso2, data| data[:valid] }.uniq end |
#valid? ⇒ Boolean
Returns whether a current parsed phone number is valid
127 128 129 130 131 |
# File 'lib/phonelib/phone.rb', line 127 def valid? return @valid if defined?(@valid) @valid = @data.any? { |_iso2, data| data[:valid].any? } end |
#valid_countries ⇒ Array
Return countries with valid patterns
103 104 105 106 107 |
# File 'lib/phonelib/phone.rb', line 103 def valid_countries @valid_countries ||= countries.select do |iso2| @data[iso2][:valid].any? end end |
#valid_country ⇒ String
Return valid country
111 112 113 114 115 |
# File 'lib/phonelib/phone.rb', line 111 def valid_country return @valid_country if defined?(@valid_country) @valid_country = main_country(valid_countries) end |
#valid_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is valid for specified country
172 173 174 175 176 177 178 |
# File 'lib/phonelib/phone.rb', line 172 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 |