Class: Phonelib::Phone

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from PhoneAnalyzer

#analyze

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

#originalObject (readonly)

defining reader methods for class variables



5
6
7
# File 'lib/phonelib/phone.rb', line 5

def original
  @original
end

Instance Method Details

#carrierObject

Returns carrier of parsed phone number or nil if number is invalid or there is no carrier specified in db for this number



191
192
193
194
# File 'lib/phonelib/phone.rb', line 191

def carrier
  get_ext_name Phonelib::Core::EXT_CARRIERS,
               Phonelib::Core::EXT_CARRIER_KEY
end

#countriesObject

Returns all countries that matched valid patterns



61
62
63
# File 'lib/phonelib/phone.rb', line 61

def countries
  @data.map { |iso2, data| iso2 }
end

#countryObject

Returns first country that matched valid patterns



73
74
75
76
77
78
79
# File 'lib/phonelib/phone.rb', line 73

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

#country_codeObject

Returns the country code from the original phone number.



82
83
84
85
86
# File 'lib/phonelib/phone.rb', line 82

def country_code
  if country_data = Phonelib.phone_data[country]
    country_data[:country_code]
  end
end

#e164Object

Returns e164 unformatted phone number



143
144
145
146
# File 'lib/phonelib/phone.rb', line 143

def e164
  international = self.international
  international and international.gsub /[^+0-9]/, ''
end

#geo_nameObject

Returns geo name of parsed phone number or nil if number is invalid or there is no geo name specified in db for this number



177
178
179
180
# File 'lib/phonelib/phone.rb', line 177

def geo_name
  get_ext_name Phonelib::Core::EXT_GEO_NAMES,
               Phonelib::Core::EXT_GEO_NAME_KEY
end

#human_typeObject

Return human representation of phone type



56
57
58
# File 'lib/phonelib/phone.rb', line 56

def human_type
  Core::TYPES_DESC[type]
end

#human_typesObject

Returns human representation of all matched phone types



51
52
53
# File 'lib/phonelib/phone.rb', line 51

def human_types
  types.map { |type| Core::TYPES_DESC[type] }
end

#impossible?Boolean

Returns whether a current parsed phone number is impossible

Returns:

  • (Boolean)


104
105
106
# File 'lib/phonelib/phone.rb', line 104

def impossible?
  !possible?
end

#internationalObject

Returns e164 formatted phone number



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/phonelib/phone.rb', line 126

def international
  sanitized = self.sanitized
  return nil if sanitized.nil? || sanitized.empty?
  return "+#{sanitized}" unless valid?

  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

Returns:

  • (Boolean)


94
95
96
# File 'lib/phonelib/phone.rb', line 94

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

Returns:

  • (Boolean)


171
172
173
# File 'lib/phonelib/phone.rb', line 171

def invalid_for_country?(country)
  !valid_for_country?(country)
end

#nationalObject

Returns formatted national number



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/phonelib/phone.rb', line 109

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

Returns:

  • (Boolean)


99
100
101
# File 'lib/phonelib/phone.rb', line 99

def possible?
  @data.select { |iso2, data| data[:possible].any? }.any?
end

#possible_typesObject

Returns all possible types that matched possible patterns



41
42
43
# File 'lib/phonelib/phone.rb', line 41

def possible_types
  @data.flat_map { |iso2, data| data[:possible] }.uniq
end

#sanitizedObject

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

#timezoneObject

Returns timezone of parsed phone number or nil if number is invalid or there is no timezone specified in db for this number



184
185
186
187
# File 'lib/phonelib/phone.rb', line 184

def timezone
  get_ext_name Phonelib::Core::EXT_TIMEZONES,
               Phonelib::Core::EXT_TIMEZONE_KEY
end

#typeObject

Returns first phone type that matched



46
47
48
# File 'lib/phonelib/phone.rb', line 46

def type
  types.first
end

#typesObject

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/phonelib/phone.rb', line 89

def valid?
  @data.select { |iso2, data| data[:valid].any? }.any?
end

#valid_countriesObject

Return countries with valid patterns



66
67
68
69
70
# File 'lib/phonelib/phone.rb', line 66

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

Returns:

  • (Boolean)


156
157
158
159
160
161
# File 'lib/phonelib/phone.rb', line 156

def valid_for_country?(country)
  country = country.to_s.upcase
  @data.select do |iso2, data|
    country == iso2 && data[:valid].any?
  end.any?
end