Module: GlobalPhone::Parsing

Included in:
Database
Defined in:
lib/global_phone/parsing.rb

Instance Method Summary collapse

Instance Method Details

#parse(string, territory_name) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/global_phone/parsing.rb', line 6

def parse(string, territory_name)
  string = Number.normalize(string)
  territory = self.territory(territory_name)
  raise ArgumentError, "unknown territory `#{territory_name}'" unless territory

  if starts_with_plus?(string)
    parse_international_string(string)
  elsif string =~ territory.international_prefix
    string = strip_international_prefix(territory, string)
    parse_international_string(string)
  else
    territory.parse_national_string(string)
  end
end

#parse_international_string(string) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/global_phone/parsing.rb', line 21

def parse_international_string(string)
  string = Number.normalize(string)
  string = strip_leading_plus(string) if starts_with_plus?(string)

  if region = region_for_string(string)
    region.parse_national_string(string)
  end
end