Class: Phonelib::Phone

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

Instance Method Summary collapse

Methods included from PhoneExtendedData

#carrier, #geo_name, #timezone

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



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

#extensionObject (readonly)

phone extension passed for parsing after a number



8
9
10
# File 'lib/phonelib/phone.rb', line 8

def extension
  @extension
end

#originalObject (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_codeObject

returns area code of parsed number



115
116
117
118
119
120
121
122
# File 'lib/phonelib/phone.rb', line 115

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

#countriesObject

Returns all countries that matched valid patterns



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

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

#countryObject

Returns first country that matched valid patterns



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

def country
  @country ||= valid_country || get_main_country(countries)
end

#country_codeObject

Returns the country code from the original phone number.



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

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

#e164Object

Returns e164 unformatted phone number



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

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

#full_e164Object

returns e164 format of phone with extension added



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

def full_e164
  "#{e164}#{formatted_extension}"
end

#full_internationalObject

returns international formatted number with extension added



166
167
168
# File 'lib/phonelib/phone.rb', line 166

def full_international
  "#{international}#{formatted_extension}"
end

#human_typeObject

Return human representation of phone type



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

def human_type
  Core::TYPES_DESC[type]
end

#human_typesObject

Returns human representation of all matched phone types



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

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

#impossible?Boolean

Returns whether a current parsed phone number is impossible

Returns:

  • (Boolean)


110
111
112
# File 'lib/phonelib/phone.rb', line 110

def impossible?
  !possible?
end

#internationalObject

Returns e164 formatted phone number



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/phonelib/phone.rb', line 150

def international
  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)


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

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)


204
205
206
# File 'lib/phonelib/phone.rb', line 204

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

#local_numberObject

returns local number



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/phonelib/phone.rb', line 125

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

#nationalObject

Returns formatted national number



138
139
140
141
142
143
144
145
146
147
# File 'lib/phonelib/phone.rb', line 138

def national
  return @national_number unless valid?
  format_match, format_string = get_formatting_data

  if format_match
    format_string.gsub(/\$\d/) { |el| format_match[el[1].to_i] }
  else
    @national_number
  end
end

#possible?Boolean

Returns whether a current parsed phone number is possible

Returns:

  • (Boolean)


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

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

#possible_typesObject

Returns all possible types that matched possible patterns



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

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

#sanitizedObject

method to get sanitized phone number (only numbers)



36
37
38
# File 'lib/phonelib/phone.rb', line 36

def sanitized
  @sanitized ||= @original && @original.gsub(/[^0-9]+/, '') || ''
end

#typeObject

Returns first phone type that matched



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

def type
  types.first
end

#typesObject

Returns all phone types that matched valid patterns



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

def types
  @data.flat_map { |iso2, data| data[:valid] }.uniq
end

#valid?Boolean

Returns whether a current parsed phone number is valid

Returns:

  • (Boolean)


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

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

#valid_countriesObject

Return countries with valid patterns



71
72
73
74
75
# File 'lib/phonelib/phone.rb', line 71

def valid_countries
  @valid_countries ||= countries.select do |iso2|
    @data[iso2][:valid].any?
  end
end

#valid_countryObject

Return valid country



78
79
80
# File 'lib/phonelib/phone.rb', line 78

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

Returns:

  • (Boolean)


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

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