Class: GlobalPhone::Number

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/global_phone/number.rb

Constant Summary collapse

E161_MAPPING =
Hash[*"a2b2c2d3e3f3g4h4i4j5k5l5m6n6o6p7q7r7s7t8u8v8w9x9y9z9".split("")]
VALID_ALPHA_CHARS =
/[a-zA-Z]/
LEADING_PLUS_CHARS =
/^\++/
NON_DIALABLE_CHARS =
/[^,#+\*\d]/
SPLIT_FIRST_GROUP =
/^(\d+)\W*(.*)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(territory, national_string) ⇒ Number

Returns a new instance of Number.



27
28
29
30
# File 'lib/global_phone/number.rb', line 27

def initialize(territory, national_string)
  @territory = territory
  @national_string = national_string
end

Instance Attribute Details

#national_stringObject (readonly)

Returns the value of attribute national_string.



20
21
22
# File 'lib/global_phone/number.rb', line 20

def national_string
  @national_string
end

#territoryObject (readonly)

Returns the value of attribute territory.



20
21
22
# File 'lib/global_phone/number.rb', line 20

def territory
  @territory
end

Class Method Details

.normalize(string) ⇒ Object



13
14
15
16
17
18
# 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

Instance Method Details

#area_codeObject



56
57
58
# File 'lib/global_phone/number.rb', line 56

def area_code
  @area_code ||= formatted_national_prefix.gsub(/[^\d]/, '') if formatted_national_prefix
end

#inspectObject



68
69
70
# File 'lib/global_phone/number.rb', line 68

def inspect
  "#<#{self.class.name} territory=#{territory.inspect} national_string=#{national_string.inspect}>"
end

#international_formatObject



46
47
48
49
50
51
52
53
54
# 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_stringObject



42
43
44
# File 'lib/global_phone/number.rb', line 42

def international_string
  @international_string ||= international_format.gsub(NON_DIALABLE_CHARS, "")
end

#local_numberObject



60
61
62
# File 'lib/global_phone/number.rb', line 60

def local_number
  @local_number ||= area_code ? national_string_parts[2] : national_format
end

#national_formatObject



32
33
34
35
36
37
38
39
40
# 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_sObject



72
73
74
# File 'lib/global_phone/number.rb', line 72

def to_s
  international_string
end

#valid?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/global_phone/number.rb', line 64

def valid?
  !!(format && national_string =~ national_pattern)
end