Class: Phony::Country

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

Constant Summary collapse

@@basic_cleaning_pattern =

Cleans all non-numeric characters.

/\(0\)|\D/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*codes) ⇒ Country

Returns a new instance of Country


12
13
14
# File 'lib/phony/country.rb', line 12

def initialize *codes
  @codes = codes
end

Instance Attribute Details

#codesObject

Returns the value of attribute codes


8
9
10
# File 'lib/phony/country.rb', line 8

def codes
  @codes
end

#formatObject (readonly)

Returns the value of attribute format


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

def format
  @format
end

#local_spaceObject (readonly)

Returns the value of attribute local_space


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

def local_space
  @local_space
end

#spaceObject (readonly)

Returns the value of attribute space


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

def space
  @space
end

Instance Method Details

#clean(number) ⇒ Object

Clean number of all non-numeric characters and return a copy.


62
63
64
# File 'lib/phony/country.rb', line 62

def clean number
  clean! number && number.dup
end

#clean!(number) ⇒ Object

Clean number of all non-numeric characters and return it.


67
68
69
# File 'lib/phony/country.rb', line 67

def clean! number
  number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number
end

#normalize(national_number) ⇒ Object

Removes 0s from partially normalized numbers such as 410443643533.

Example:

410443643533 -> 41443643533

In some cases it doesn't, like Italy.


79
80
81
82
83
84
85
86
87
# File 'lib/phony/country.rb', line 79

def normalize national_number
  clean! national_number
  normalized = @codes.reduce national_number do |number, code|
    result = code.normalize number
    break result if result
    number
  end
  normalized
end

#plausible?(rest, hints = {}) ⇒ Boolean

Tests for plausibility of this national number.

Returns:

  • (Boolean)

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/phony/country.rb', line 91

def plausible? rest, hints = {}
  local, _, ndc, *rest = split_ndc rest

  # Element based checking.
  #
  # Note: ndc == false means the country has none.
  #
  return false if ndc.nil?
  return false if ndc && ndc.empty?
  return false if @invalid_ndcs && @invalid_ndcs === ndc

  # # A valid range for the rest is 0 or 3+ total digits.
  # #
  # return false if (1..2) === rest_size

  # National destination code plausible?
  #
  ndc_needed = hints[:ndc]
  return false if ndc_needed && !(ndc_needed === ndc)

  # Local code specific checks.
  #
  return local.plausible? rest, hints
end

#split(national_number) ⇒ Object

A number is split with the code handlers as given in the initializer.

Note: If the ndc is nil, it will not return it.


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/phony/country.rb', line 39

def split national_number
  trunk = nil
  @codes.each do |code|
    new_trunk, ndc, *rest = code.split national_number
    trunk ||= new_trunk
    return [trunk, ndc, *rest] if rest && !rest.empty?
  end
  # Best effort in error case.
  #
  [trunk, national_number, []]
end

#split_ndc(national_number) ⇒ Object


50
51
52
53
54
55
# File 'lib/phony/country.rb', line 50

def split_ndc national_number
  @codes.each do |code|
    zero, ndc, *rest = code.split national_number
    return [code.local_splitter, zero, ndc, *rest] if rest && !rest.empty?
  end
end

#vanity?(national_number) ⇒ Boolean

Is this national number a vanity number?

Returns:

  • (Boolean)

118
119
120
# File 'lib/phony/country.rb', line 118

def vanity? national_number
  Vanity.vanity? national_number
end

#vanity_to_number(vanity_number) ⇒ Object


123
124
125
126
# File 'lib/phony/country.rb', line 123

def vanity_to_number vanity_number
  _, ndc, *rest = split vanity_number
  "#{ndc}#{Vanity.replace(rest.join)}"
end

#with(cc, options = {}) ⇒ Object

Options.

TODO Rewrite.


27
28
29
30
31
32
33
# File 'lib/phony/country.rb', line 27

def with cc, options = {}
  @cc           = cc
  @invalid_ndcs = options[:invalid_ndcs]
  @format       = options[:format]
  @space        = options[:space]
  @local_space  = options[:local_space]
end

#|(other) ⇒ Object

Chain two codes together.


18
19
20
21
# File 'lib/phony/country.rb', line 18

def | other
  self.codes = self.codes + other.codes
  self
end