Class: Phony::NationalCode

Inherits:
Object
  • Object
show all
Defined in:
lib/phony/national_code.rb

Overview

NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(national_splitter, local_splitter) ⇒ NationalCode

Returns a new instance of NationalCode.



9
10
11
12
# File 'lib/phony/national_code.rb', line 9

def initialize(national_splitter, local_splitter)
  @national_splitter = national_splitter
  @local_splitter    = local_splitter
end

Instance Attribute Details

#local_splitterObject (readonly)

Returns the value of attribute local_splitter.



7
8
9
# File 'lib/phony/national_code.rb', line 7

def local_splitter
  @local_splitter
end

Instance Method Details

#normalize(national_number, _options = {}) ⇒ Object

Split gets a number without country code and removes a relative zero.

Note: Some cases, like Italy, don’t remove the relative zero.



28
29
30
# File 'lib/phony/national_code.rb', line 28

def normalize(national_number, _options = {})
  national_number.gsub(/\A0+/, EMPTY_STRING)
end

#split(national_number) ⇒ Object

Split gets a number without country code and splits it into its parts.



17
18
19
20
21
22
# File 'lib/phony/national_code.rb', line 17

def split(national_number)
  zero, ndc_or_rest, rest = @national_splitter.split national_number.dup
  return [zero, ndc_or_rest] unless rest

  [zero, ndc_or_rest, *@local_splitter.split(rest)]
end