class GlobalPhone::Number

Constants

E161_MAPPING
LEADING_PLUS_CHARS
NON_DIALABLE_CHARS
SPLIT_FIRST_GROUP
VALID_ALPHA_CHARS

Attributes

national_string[R]
territory[R]

Public Class Methods

new(territory, national_string) click to toggle source
# File lib/global_phone/number.rb, line 27
def initialize(territory, national_string)
  @territory = territory
  @national_string = national_string
end
normalize(string) click to toggle source
# File lib/global_phone/number.rb, line 13
def self.normalize(string)
  string.to_s.
    gsub(VALID_ALPHA_CHARS) { |c| E161_MAPPING[c.downcase] }.
    gsub(LEADING_PLUS_CHARS, "+").
    gsub(NON_DIALABLE_CHARS, "")
end

Public Instance Methods

area_code() click to toggle source
# File lib/global_phone/number.rb, line 56
def area_code
  @area_code ||= formatted_national_prefix.gsub(/[^\d]/, '') if formatted_national_prefix
end
inspect() click to toggle source
# File lib/global_phone/number.rb, line 68
def inspect
  "#<#{self.class.name} territory=#{territory.inspect} national_string=#{national_string.inspect}>"
end
international_format() click to toggle source
# File lib/global_phone/number.rb, line 46
def international_format
  @international_format ||= begin
    if format && formatted_number = format.apply(national_string, :international)
      "+#{country_code} #{formatted_number}"
    else
      "+#{country_code} #{national_string}"
    end
  end
end
international_string() click to toggle source
# File lib/global_phone/number.rb, line 42
def international_string
  @international_string ||= international_format.gsub(NON_DIALABLE_CHARS, "")
end
local_number() click to toggle source
# File lib/global_phone/number.rb, line 60
def local_number
  @local_number ||= area_code ? national_string_parts[2] : national_format
end
national_format() click to toggle source
# File lib/global_phone/number.rb, line 32
def national_format
  @national_format ||= begin
    if format
      national_string_with_prefix
    else
      national_string
    end
  end
end
to_s() click to toggle source
# File lib/global_phone/number.rb, line 72
def to_s
  international_string
end
valid?() click to toggle source
# File lib/global_phone/number.rb, line 64
def valid?
  !!(format && national_string =~ national_pattern)
end

Protected Instance Methods

area_code_suffix() click to toggle source
# File lib/global_phone/number.rb, line 94
def area_code_suffix
  @area_code_suffix ||= national_string_parts[1]
end
find_format_for(string) click to toggle source
# File lib/global_phone/number.rb, line 81
def find_format_for(string)
  region.formats.detect { |format| format.match(string) } ||
  region.formats.detect { |format| format.match(string, false) }
end
format() click to toggle source
# File lib/global_phone/number.rb, line 77
def format
  @format ||= find_format_for(national_string)
end
formatted_national_prefix() click to toggle source
# File lib/global_phone/number.rb, line 98
def formatted_national_prefix
  @formatted_national_prefix ||= begin
    national_prefix_formatting_rule.gsub("$NP", national_prefix).gsub("$FG", area_code_suffix) if
      national_prefix_formatting_rule
  end
end
formatted_national_string() click to toggle source
# File lib/global_phone/number.rb, line 86
def formatted_national_string
  @formatted_national_string ||= format.apply(national_string, :national)
end
national_prefix_formatting_rule() click to toggle source
# File lib/global_phone/number.rb, line 110
def national_prefix_formatting_rule
  @national_prefix_formatting_rule ||=
    format.try(:national_prefix_formatting_rule) || territory.national_prefix_formatting_rule
end
national_string_parts() click to toggle source
# File lib/global_phone/number.rb, line 90
def national_string_parts
  @national_string_parts ||= formatted_national_string.match(SPLIT_FIRST_GROUP)
end
national_string_with_prefix() click to toggle source
# File lib/global_phone/number.rb, line 105
def national_string_with_prefix
  @national_string_with_prefix ||= national_prefix_formatting_rule && national_string_parts ?
    "#{formatted_national_prefix} #{local_number}" : formatted_national_string
end