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

inspect() click to toggle source
# File lib/global_phone/number.rb, line 60
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
national_format() click to toggle source
# File lib/global_phone/number.rb, line 32
def national_format
  @national_format ||= begin
    if format && result = format.apply(national_string, :national)
      apply_national_prefix_format(result)
    else
      national_string
    end
  end
end
to_s() click to toggle source
# File lib/global_phone/number.rb, line 64
def to_s
  international_string
end
valid?() click to toggle source
# File lib/global_phone/number.rb, line 56
def valid?
  !!(format && national_string =~ national_pattern)
end

Protected Instance Methods

apply_national_prefix_format(result) click to toggle source
# File lib/global_phone/number.rb, line 78
def apply_national_prefix_format(result)
  prefix = national_prefix_formatting_rule
  return result unless prefix && match = result.match(SPLIT_FIRST_GROUP)

  prefix = prefix.gsub("$NP", national_prefix)
  prefix = prefix.gsub("$FG", match[1])
  result = "#{prefix} #{match[2]}"
end
find_format_for(string) click to toggle source
# File lib/global_phone/number.rb, line 73
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 69
def format
  @format ||= find_format_for(national_string)
end
national_prefix_formatting_rule() click to toggle source
# File lib/global_phone/number.rb, line 87
def national_prefix_formatting_rule
  format.national_prefix_formatting_rule || territory.national_prefix_formatting_rule
end