Class: Phonelib::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/phonelib/phone.rb

Overview

class for parsed phone number, includes validation and formatting methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone, country_data) ⇒ Phone

class initialization method

Attributes

  • phone - Phone number for parsing

  • country_data - Hash of data for parsing



15
16
17
18
19
20
# File 'lib/phonelib/phone.rb', line 15

def initialize(phone, country_data)
  @original = phone
  @sanitized = sanitize_phone(@original)
  @analyzed_data = {}
  analyze_phone(country_data) unless @sanitized.empty?
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

#sanitizedObject (readonly)

defining reader methods for class variables



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

def sanitized
  @sanitized
end

Instance Method Details

#countriesObject

Returns all countries that matched valid patterns



43
44
45
# File 'lib/phonelib/phone.rb', line 43

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

#countryObject

Returns first country that matched valid patterns



55
56
57
58
59
60
61
# File 'lib/phonelib/phone.rb', line 55

def country
  @country ||= begin
    valid_countries.find do |iso2|
      @analyzed_data[iso2][:main_country_for_code] == 'true'
    end || valid_countries.first
  end
end

#human_typeObject

Return human representation of phone type



38
39
40
# File 'lib/phonelib/phone.rb', line 38

def human_type
  Core::TYPES[type]
end

#human_typesObject

Returns human representation of all matched phone types



33
34
35
# File 'lib/phonelib/phone.rb', line 33

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

#impossible?Boolean

Returns whether a current parsed phone number is impossible



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

def impossible?
  !possible?
end

#internationalObject

Returns e164 formatted phone number



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/phonelib/phone.rb', line 98

def international
  return "+#{@sanitized}" unless valid?
  national_number = national
  if national_prefix = @analyzed_data[country][Core::NATIONAL_PREFIX]
    national_number.gsub!(/^#{national_prefix}/, '')
    national_number.strip!
  end
  country_code = @analyzed_data[country][Core::COUNTRY_CODE]

  "+#{country_code} #{national_number}"
end

#invalid?Boolean

Returns whether a current parsed phone number is invalid



69
70
71
# File 'lib/phonelib/phone.rb', line 69

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
    


133
134
135
# File 'lib/phonelib/phone.rb', line 133

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

#nationalObject

Returns formatted national number



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/phonelib/phone.rb', line 84

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')

  matches = @national_number.match(/#{format[:pattern]}/)
  format_string.gsub(/\$\d/) { |el| matches[el[1].to_i] }
end

#possible?Boolean

Returns whether a current parsed phone number is possible



74
75
76
# File 'lib/phonelib/phone.rb', line 74

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

#typeObject

Returns first phone type that matched



28
29
30
# File 'lib/phonelib/phone.rb', line 28

def type
  types.first
end

#typesObject

Returns all phone types that matched valid patterns



23
24
25
# File 'lib/phonelib/phone.rb', line 23

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

#valid?Boolean

Returns whether a current parsed phone number is valid



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

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

#valid_countriesObject

Return countries with valid patterns



48
49
50
51
52
# File 'lib/phonelib/phone.rb', line 48

def valid_countries
  @valid_countries ||= countries.select do |iso2|
    @analyzed_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
    


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

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