Class: Phonelib::Phone
- Inherits:
-
Object
- Object
- Phonelib::Phone
- Includes:
- PhoneAnalyzer
- 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
-
#original ⇒ Object
readonly
defining reader methods for class variables.
Instance Method Summary collapse
-
#countries ⇒ Object
Returns all countries that matched valid patterns.
-
#country ⇒ Object
Returns first country that matched valid patterns.
-
#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 ⇒ 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.
-
#national ⇒ Object
Returns formatted national number.
-
#possible? ⇒ Boolean
Returns whether a current parsed phone number is possible.
-
#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_for_country?(country) ⇒ Boolean
Returns whether a current parsed phone number is valid for specified country.
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
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/phonelib/phone.rb', line 18 def initialize(original, country = nil) @original = original if sanitized.empty? @data = {} else @data = analyze(sanitized, country) first = @data.values.first @national_number = first ? first[:national] : sanitized end end |
Instance Attribute Details
#original ⇒ Object (readonly)
defining reader methods for class variables
5 6 7 |
# File 'lib/phonelib/phone.rb', line 5 def original @original end |
Instance Method Details
#countries ⇒ Object
Returns all countries that matched valid patterns
56 57 58 |
# File 'lib/phonelib/phone.rb', line 56 def countries @data.map { |iso2, data| iso2 } end |
#country ⇒ Object
Returns first country that matched valid patterns
68 69 70 71 72 73 74 |
# File 'lib/phonelib/phone.rb', line 68 def country @country ||= begin valid_countries.find do |iso2| @data[iso2][Core::MAIN_COUNTRY_FOR_CODE] == 'true' end || valid_countries.first || countries.first end end |
#human_type ⇒ Object
Return human representation of phone type
51 52 53 |
# File 'lib/phonelib/phone.rb', line 51 def human_type Core::TYPES_DESC[type] end |
#human_types ⇒ Object
Returns human representation of all matched phone types
46 47 48 |
# File 'lib/phonelib/phone.rb', line 46 def human_types types.map { |type| Core::TYPES_DESC[type] } end |
#impossible? ⇒ Boolean
Returns whether a current parsed phone number is impossible
92 93 94 |
# File 'lib/phonelib/phone.rb', line 92 def impossible? !possible? end |
#international ⇒ Object
Returns e164 formatted phone number
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/phonelib/phone.rb', line 114 def international return "+#{sanitized}" unless valid? format = @data[country][:format] if matches = @national_number.match(/#{format[Core::PATTERN]}/) national = format[:format].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
82 83 84 |
# File 'lib/phonelib/phone.rb', line 82 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
150 151 152 |
# File 'lib/phonelib/phone.rb', line 150 def invalid_for_country?(country) !valid_for_country?(country) end |
#national ⇒ Object
Returns formatted national number
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/phonelib/phone.rb', line 97 def national return @national_number unless valid? format, prefix, rule = get_formatting_data # add space to format groups, change first group to rule, # change rule's constants to values format_string = format[:format].gsub(/(\d)\$/, '\\1 $').gsub('$1', rule) .gsub(/(\$NP|\$FG)/, '$NP' => prefix, '$FG' => '$1') if matches = @national_number.match(/#{format[Core::PATTERN]}/) format_string.gsub(/\$\d/) { |el| matches[el[1].to_i] } else @national_number end end |
#possible? ⇒ Boolean
Returns whether a current parsed phone number is possible
87 88 89 |
# File 'lib/phonelib/phone.rb', line 87 def possible? @data.select { |iso2, data| data[:possible].any? }.any? end |
#sanitized ⇒ Object
method to get sanitized phone number (only numbers)
31 32 33 |
# File 'lib/phonelib/phone.rb', line 31 def sanitized @original && @original.gsub(/[^0-9]+/, '') || '' end |
#type ⇒ Object
Returns first phone type that matched
41 42 43 |
# File 'lib/phonelib/phone.rb', line 41 def type types.first end |
#types ⇒ Object
Returns all phone types that matched valid patterns
36 37 38 |
# File 'lib/phonelib/phone.rb', line 36 def types @data.flat_map { |iso2, data| data[:valid] }.uniq end |
#valid? ⇒ Boolean
Returns whether a current parsed phone number is valid
77 78 79 |
# File 'lib/phonelib/phone.rb', line 77 def valid? @data.select { |iso2, data| data[:valid].any? }.any? end |
#valid_countries ⇒ Object
Return countries with valid patterns
61 62 63 64 65 |
# File 'lib/phonelib/phone.rb', line 61 def valid_countries @valid_countries ||= countries.select do |iso2| @data[iso2][:valid].any? end 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
135 136 137 138 139 140 |
# File 'lib/phonelib/phone.rb', line 135 def valid_for_country?(country) country = country.to_s.upcase @data.select do |iso2, data| country == iso2 && data[:valid].any? end.any? end |