Class: Phonelib::Phone

Inherits:
Object
  • Object
show all
Includes:
PhoneAnalyzer, PhoneExtendedData, PhoneFormatter, PhoneValidation
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

Instance Method Summary collapse

Methods included from PhoneValidation

#errors, #validation

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

#analyze

Constructor Details

#initialize(phone, country = nil) ⇒ Phonelib::Phone

class initialization method

Parameters:

  • phone (String)

    Phone number for parsing

  • country (String|Symbol) (defaults to: nil)

    Country specification for parsing. Must be ISO code of country (2 letters) like 'US', 'us' or :us for United States



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/phonelib/phone.rb', line 28

def initialize(phone, country = nil)
  @original, @extension = separate_extension(phone.to_s)
  @extension = @extension.gsub(/[^0-9]/, '') if @extension

  if sanitized.empty?
    @data = {}
    @diagnostic_possibility = :impossible
  else
    @passed_country = passed_country(country)
    @data = analyze(sanitized, @passed_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
    @diagnostic_possibility = diagnostic_possibility
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Phonelib::PhoneFormatter

Instance Attribute Details

#extensionString (readonly)

Returns phone extension passed for parsing after a number.

Returns:

  • (String)

    phone extension passed for parsing after a number



11
12
13
# File 'lib/phonelib/phone.rb', line 11

def extension
  @extension
end

#national_numberString (readonly)

Returns phone national number.

Returns:

  • (String)

    phone national number



14
15
16
# File 'lib/phonelib/phone.rb', line 14

def national_number
  @national_number
end

#originalString (readonly)

Returns original phone number passed for parsing.

Returns:

  • (String)

    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

Parameters:

Returns:

  • (Boolean)

    result of equality comparison



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

def ==(other)
  other = Phonelib.parse(other) unless other.is_a?(Phonelib::Phone)
  return (e164 == other.e164) if valid? && other.valid?
  original == other.original
end

#countriesArray

Returns all countries that matched valid patterns

Returns:

  • (Array)

    Possible ISO2 country codes array



108
109
110
# File 'lib/phonelib/phone.rb', line 108

def countries
  @countries ||= @data.keys
end

#countryString

Returns first country that matched valid patterns

Returns:

  • (String)

    valid country ISO2 code or first matched country code



130
131
132
133
134
# File 'lib/phonelib/phone.rb', line 130

def country
  return @country if defined?(@country)

  @country = valid_country || main_country(countries)
end

#explicit_international_prefix?Boolean

Returns whether the original input begins with an explicit + or 00 international prefix after parser-recognized leading formatting.

Returns:

  • (Boolean)

    original input has an explicit international prefix



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

def explicit_international_prefix?
  !!original_starts_with_plus_or_double_zero?
end

#human_typeString

Return human representation of phone type

Returns:

  • (String)

    Human readable valid phone type



102
103
104
# File 'lib/phonelib/phone.rb', line 102

def human_type
  Core::TYPES_DESC[type]
end

#human_typesArray

Returns human representation of all matched phone types

Returns:

  • (Array)

    Array of human readable valid phone types



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

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

#impossible?Boolean

Returns whether a current parsed phone number is impossible

Returns:

  • (Boolean)

    parsed phone is impossible



160
161
162
# File 'lib/phonelib/phone.rb', line 160

def impossible?
  !possible?
end

#invalid?Boolean

Returns whether a current parsed phone number is invalid

Returns:

  • (Boolean)

    parsed phone is invalid



146
147
148
# File 'lib/phonelib/phone.rb', line 146

def invalid?
  !valid?
end

#invalid_for_country?(country) ⇒ Boolean

Returns whether a current parsed phone number is invalid for specified country

Parameters:

  • country (String|Symbol)

    ISO code of country (2 letters) like 'US', 'us' or :us for United States

Returns:

  • (Boolean)

    parsed phone number is invalid



196
197
198
# File 'lib/phonelib/phone.rb', line 196

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

#local_numberString

returns local number

Returns:

  • (String)

    local number



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/phonelib/phone.rb', line 166

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

Returns:

  • (Boolean)

    parsed phone is possible



152
153
154
155
156
# File 'lib/phonelib/phone.rb', line 152

def possible?
  return @possible if defined?(@possible)

  @possible = @data.any? { |_iso2, data| data[:possible].any? }
end

#possible_typesArray

Returns all possible types that matched possible patterns

Returns:

  • (Array)

    all possible phone types



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

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

#sanitizedString

method to get sanitized phone number (only numbers)

Returns:

  • (String)

    Sanitized phone number



62
63
64
65
66
67
# File 'lib/phonelib/phone.rb', line 62

def sanitized
  @sanitized ||=
      vanity_converted(@original).gsub(
          Phonelib.strict_check ? cr('^\+') : cr(Phonelib.sanitize_regex),
          '')
end

#to_sObject

method returns string representation of parsed phone



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

def to_s
  valid? ? e164 : original
end

#typeSymbol

Returns first phone type that matched

Returns:

  • (Symbol)

    valid phone type



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

def type
  types.first
end

#typesArray

Returns all phone types that matched valid patterns

Returns:

  • (Array)

    all valid phone types



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

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

#valid?Boolean

Returns whether a current parsed phone number is valid

Returns:

  • (Boolean)

    parsed phone is valid



138
139
140
141
142
# File 'lib/phonelib/phone.rb', line 138

def valid?
  return @valid if defined?(@valid)

  @valid = @data.any? { |_iso2, data| data[:valid].any? }
end

#valid_countriesArray

Return countries with valid patterns

Returns:

  • (Array)

    Valid ISO2 country codes array



114
115
116
117
118
# File 'lib/phonelib/phone.rb', line 114

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

#valid_countryString

Return valid country

Returns:

  • (String)

    valid ISO2 country code



122
123
124
125
126
# File 'lib/phonelib/phone.rb', line 122

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

Parameters:

  • country (String|Symbol)

    ISO code of country (2 letters) like 'US', 'us' or :us for United States

Returns:

  • (Boolean)

    parsed phone number is valid



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

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