Class: Phonelib::Phone
- Inherits:
-
Object
- Object
- Phonelib::Phone
- Includes:
- PhoneAnalyzer, PhoneExtendedData
- Defined in:
- lib/phonelib/phone.rb
Overview
class for parsed phone number, includes validation and formatting methods
Constant Summary
Constants included from PhoneAnalyzer
Phonelib::PhoneAnalyzer::NOT_FOR_CHECK
Instance Attribute Summary collapse
-
#extension ⇒ Object
readonly
phone extension passed for parsing after a number.
-
#original ⇒ Object
readonly
defining reader methods for class variables original phone number passed for parsing.
Instance Method Summary collapse
-
#area_code ⇒ Object
returns area code of parsed number.
-
#countries ⇒ Object
Returns all countries that matched valid patterns.
-
#country ⇒ Object
Returns first country that matched valid patterns.
-
#country_code ⇒ Object
Returns the country code from the original phone number.
-
#e164 ⇒ Object
Returns e164 unformatted phone number.
-
#full_e164 ⇒ Object
returns e164 format of phone with extension added.
-
#full_international ⇒ Object
returns international formatted number with extension added.
-
#human_type ⇒ Object
Return human representation of phone type.
-
#human_types ⇒ Object
Returns human representation of all matched phone types.
-
#impossible? ⇒ Boolean
Returns whether a current parsed phone number is impossible.
-
#initialize(original, country = nil) ⇒ Phone
constructor
class initialization method.
-
#international(formatted = true) ⇒ Object
Returns e164 formatted phone number.
-
#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 ⇒ Object
returns local number.
-
#national(formatted = true) ⇒ Object
Returns formatted national number.
-
#possible? ⇒ Boolean
Returns whether a current parsed phone number is possible.
-
#possible_types ⇒ Object
Returns all possible types that matched possible patterns.
-
#sanitized ⇒ Object
method to get sanitized phone number (only numbers).
-
#type ⇒ Object
Returns first phone type that matched.
-
#types ⇒ Object
Returns all phone types that matched valid patterns.
-
#valid? ⇒ Boolean
Returns whether a current parsed phone number is valid.
-
#valid_countries ⇒ Object
Return countries with valid patterns.
-
#valid_country ⇒ Object
Return valid country.
-
#valid_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is valid for specified country.
Methods included from PhoneExtendedData
#carrier, #geo_name, #timezone
Methods included from PhoneAnalyzer
Constructor Details
#initialize(original, country = nil) ⇒ Phone
class initialization method
Attributes
-
phone
- Phone number for parsing -
country
- Country specification for parsing. Must be ISO code of country (2 letters) like ‘US’, ‘us’ or :us for United States
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/phonelib/phone.rb', line 22 def initialize(original, country = nil) @original, @extension = separate_extension(original) @extension.gsub!(/[^0-9]/, '') if @extension if sanitized.empty? @data = {} else @data = analyze(sanitized, @original.start_with?('+') ? nil : country) first = @data.values.first @national_number = first ? first[:national] : sanitized end end |
Instance Attribute Details
#extension ⇒ Object (readonly)
phone extension passed for parsing after a number
8 9 10 |
# File 'lib/phonelib/phone.rb', line 8 def extension @extension end |
#original ⇒ Object (readonly)
defining reader methods for class variables original phone number passed for parsing
6 7 8 |
# File 'lib/phonelib/phone.rb', line 6 def original @original end |
Instance Method Details
#area_code ⇒ Object
returns area code of parsed number
119 120 121 122 123 124 125 126 |
# File 'lib/phonelib/phone.rb', line 119 def area_code return nil unless possible? format_match, format_string = get_formatting_data if format_string =~ /^.*[0-9]+.*\$1/ && format_match format_string.gsub(/\$1.*$/, format_match[1]).gsub(/[^\d]+/, '') end end |
#countries ⇒ Object
Returns all countries that matched valid patterns
70 71 72 |
# File 'lib/phonelib/phone.rb', line 70 def countries @data.map { |iso2, data| iso2 } end |
#country ⇒ Object
Returns first country that matched valid patterns
87 88 89 |
# File 'lib/phonelib/phone.rb', line 87 def country @country ||= valid_country || get_main_country(countries) end |
#country_code ⇒ Object
Returns the country code from the original phone number.
92 93 94 95 96 |
# File 'lib/phonelib/phone.rb', line 92 def country_code if country_data = Phonelib.phone_data[country] country_data[:country_code] end end |
#e164 ⇒ Object
Returns e164 unformatted phone number
182 183 184 185 |
# File 'lib/phonelib/phone.rb', line 182 def e164 international = self.international international and international.gsub /[^+0-9]/, '' end |
#full_e164 ⇒ Object
returns e164 format of phone with extension added
177 178 179 |
# File 'lib/phonelib/phone.rb', line 177 def full_e164 "#{e164}#{formatted_extension}" end |
#full_international ⇒ Object
returns international formatted number with extension added
172 173 174 |
# File 'lib/phonelib/phone.rb', line 172 def full_international "#{international}#{formatted_extension}" end |
#human_type ⇒ Object
Return human representation of phone type
65 66 67 |
# File 'lib/phonelib/phone.rb', line 65 def human_type Core::TYPES_DESC[type] end |
#human_types ⇒ Object
Returns human representation of all matched phone types
60 61 62 |
# File 'lib/phonelib/phone.rb', line 60 def human_types types.map { |type| Core::TYPES_DESC[type] } end |
#impossible? ⇒ Boolean
Returns whether a current parsed phone number is impossible
114 115 116 |
# File 'lib/phonelib/phone.rb', line 114 def impossible? !possible? end |
#international(formatted = true) ⇒ Object
Returns e164 formatted phone number
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/phonelib/phone.rb', line 155 def international(formatted = true) return nil if sanitized.nil? || sanitized.empty? return "+#{sanitized}" unless valid? return "#{@data[country][Core::COUNTRY_CODE]}#{@national_number}" unless formatted format = @data[country][:format] if matches = @national_number.match(/#{format[Core::PATTERN]}/) fmt = format[:intl_format] || format[:format] national = fmt.gsub(/\$\d/) { |el| matches[el[1].to_i] } else national = @national_number end "+#{@data[country][Core::COUNTRY_CODE]} #{national}" end |
#invalid? ⇒ Boolean
Returns whether a current parsed phone number is invalid
104 105 106 |
# File 'lib/phonelib/phone.rb', line 104 def invalid? !valid? end |
#invalid_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is invalid for specified country
Attributes
-
country
- ISO code of country (2 letters) like ‘US’, ‘us’ or :us for United States
210 211 212 |
# File 'lib/phonelib/phone.rb', line 210 def invalid_for_country?(country) !valid_for_country?(country) end |
#local_number ⇒ Object
returns local number
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/phonelib/phone.rb', line 129 def local_number return national unless possible? format_match, format_string = get_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 |
#national(formatted = true) ⇒ Object
Returns formatted national number
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/phonelib/phone.rb', line 142 def national(formatted = true) return @national_number unless valid? format_match, format_string = get_formatting_data if format_match out = format_string.gsub(/\$\d/) { |el| format_match[el[1].to_i] } formatted ? out : out.gsub(/[^0-9]/, '') else @national_number end end |
#possible? ⇒ Boolean
Returns whether a current parsed phone number is possible
109 110 111 |
# File 'lib/phonelib/phone.rb', line 109 def possible? @data.select { |iso2, data| data[:possible].any? }.any? end |
#possible_types ⇒ Object
Returns all possible types that matched possible patterns
50 51 52 |
# File 'lib/phonelib/phone.rb', line 50 def possible_types @data.flat_map { |iso2, data| data[:possible] }.uniq end |
#sanitized ⇒ Object
method to get sanitized phone number (only numbers)
36 37 38 39 40 41 42 |
# File 'lib/phonelib/phone.rb', line 36 def sanitized @sanitized = if Phonelib.strict_check @original else @original && @original.gsub(/[^0-9]+/, '') || '' end end |
#type ⇒ Object
Returns first phone type that matched
55 56 57 |
# File 'lib/phonelib/phone.rb', line 55 def type types.first end |
#types ⇒ Object
Returns all phone types that matched valid patterns
45 46 47 |
# File 'lib/phonelib/phone.rb', line 45 def types @data.flat_map { |iso2, data| data[:valid] }.uniq end |
#valid? ⇒ Boolean
Returns whether a current parsed phone number is valid
99 100 101 |
# File 'lib/phonelib/phone.rb', line 99 def valid? @data.select { |iso2, data| data[:valid].any? }.any? end |
#valid_countries ⇒ Object
Return countries with valid patterns
75 76 77 78 79 |
# File 'lib/phonelib/phone.rb', line 75 def valid_countries @valid_countries ||= countries.select do |iso2| @data[iso2][:valid].any? end end |
#valid_country ⇒ Object
Return valid country
82 83 84 |
# File 'lib/phonelib/phone.rb', line 82 def valid_country @valid_country ||= get_main_country(valid_countries) end |
#valid_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is valid for specified country
Attributes
-
country
- ISO code of country (2 letters) like ‘US’, ‘us’ or :us for United States
195 196 197 198 199 200 |
# File 'lib/phonelib/phone.rb', line 195 def valid_for_country?(country) country = country.to_s.upcase @data.find do |iso2, data| country == iso2 && data[:valid].any? end.is_a? Array end |