Class: Phony::Country
- Inherits:
-
Object
- Object
- Phony::Country
- Defined in:
- lib/phony/country.rb
Constant Summary collapse
- @@international_absolute_format =
'+%s%s%s%s%s'
- @@international_relative_format =
'00%s%s%s%s%s'
- @@national_format =
'%s%s%s%s'
- @@default_space =
' '
- @@default_local_space =
' '
- @@default_parentheses =
false
- @@basic_cleaning_pattern =
Cleans all non-numeric characters.
/\(0|\D/
Instance Attribute Summary collapse
-
#codes ⇒ Object
Returns the value of attribute codes.
Instance Method Summary collapse
-
#clean(number) ⇒ Object
Clean number of all non-numeric characters and return a copy.
-
#clean!(number) ⇒ Object
Clean number of all non-numeric characters and return it.
-
#format(national_number, options = {}) ⇒ Object
Format the number, given the national part of it.
- #format_cc_ndc(trunk, ndc, local, type, space, parentheses, use_trunk) ⇒ Object
- #format_local(local, local_space) ⇒ Object
- #format_ndc(ndc, parentheses) ⇒ Object
- #format_with_ndc(format, cc, ndc, local, space) ⇒ Object
- #format_without_ndc(format, cc, local, space) ⇒ Object
-
#initialize(*codes) ⇒ Country
constructor
TODO: Doc.
- #internal_split(national_number) ⇒ Splitters::Local, ...
-
#normalize(national_number, options = {}) ⇒ Object
Removes 0s from partially normalized numbers such as 410443643533.
-
#plausible?(rest, hints = {}) ⇒ Boolean
Tests for plausibility of this national number.
-
#split(national_number) ⇒ Trunk, ...
A number is split with the code handlers as given in the initializer.
-
#vanity?(national_number) ⇒ Boolean
Is this national number a vanity number?.
- #vanity_to_number(vanity_number) ⇒ Object
-
#with(cc, options = {}) ⇒ Object
Options.
-
#|(other) ⇒ Object
DSL method.
Constructor Details
#initialize(*codes) ⇒ Country
TODO: Doc.
17 18 19 |
# File 'lib/phony/country.rb', line 17 def initialize *codes @codes = codes end |
Instance Attribute Details
#codes ⇒ Object
Returns the value of attribute codes.
5 6 7 |
# File 'lib/phony/country.rb', line 5 def codes @codes end |
Instance Method Details
#clean(number) ⇒ Object
Clean number of all non-numeric characters and return a copy.
148 149 150 |
# File 'lib/phony/country.rb', line 148 def clean(number) clean! number && number.dup end |
#clean!(number) ⇒ Object
Clean number of all non-numeric characters and return it.
154 155 156 |
# File 'lib/phony/country.rb', line 154 def clean!(number) number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number end |
#format(national_number, options = {}) ⇒ Object
Format the number, given the national part of it.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/phony/country.rb', line 74 def format(national_number, = {}) type = [:format] || @format space = [:spaces] || @space || @@default_space local_space = [:local_spaces] || @local_space || space || @@default_local_space parentheses = [:parentheses] parentheses = @parentheses || @@default_parentheses if parentheses.nil? use_trunk = [:trunk] trunk, ndc, *local_pieces = split national_number local = format_local local_pieces, local_space format_cc_ndc trunk, ndc, local, type, space, parentheses, use_trunk end |
#format_cc_ndc(trunk, ndc, local, type, space, parentheses, use_trunk) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/phony/country.rb', line 98 def format_cc_ndc(trunk, ndc, local, type, space, parentheses, use_trunk) # NOTE: We mark NDCs that are of type "none" with false (nil trips plausible?). This would result in false being printed. # Therefore we set NDC to nil when formatting. ndc = nil if ndc == false case type when String trunk &&= trunk.format(space, use_trunk) type % { trunk: trunk, cc: @cc, ndc: ndc, local: local } when nil, :international_absolute, :international, :+ if ndc format_with_ndc(@@international_absolute_format, @cc, format_ndc(ndc, parentheses), local, space) else format_without_ndc(@@international_absolute_format, @cc, local, space) end when :international_relative if ndc format_with_ndc(@@international_relative_format, @cc, format_ndc(ndc, parentheses), local, space) else format_without_ndc(@@international_relative_format, @cc, local, space) end when :national trunk &&= trunk.format(space, use_trunk) if ndc && !ndc.empty? @@national_format % [trunk, format_ndc(ndc, parentheses), space, local] else @@national_format % [trunk, nil, nil, local] end when :local local end end |
#format_local(local, local_space) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/phony/country.rb', line 89 def format_local(local, local_space) if local.empty? EMPTY_STRING else local.compact! local.join local_space.to_s end end |
#format_ndc(ndc, parentheses) ⇒ Object
130 131 132 133 |
# File 'lib/phony/country.rb', line 130 def format_ndc(ndc, parentheses) ndc = nil if ndc == false # TODO parentheses ? "(#{ndc})" : ndc end |
#format_with_ndc(format, cc, ndc, local, space) ⇒ Object
135 136 137 |
# File 'lib/phony/country.rb', line 135 def format_with_ndc(format, cc, ndc, local, space) format % [cc, space, ndc, space, local] end |
#format_without_ndc(format, cc, local, space) ⇒ Object
139 140 141 |
# File 'lib/phony/country.rb', line 139 def format_without_ndc(format, cc, local, space) format % [cc, space, local, nil, nil] end |
#internal_split(national_number) ⇒ Splitters::Local, ...
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/phony/country.rb', line 60 def internal_split(national_number) trunk = nil @codes.each do |national_splitter| new_trunk, ndc, *rest = national_splitter.split national_number trunk ||= new_trunk return [national_splitter.local_splitter, trunk, ndc, *rest] if rest && !rest.empty? end # Best effort. [nil, trunk, national_number, []] end |
#normalize(national_number, options = {}) ⇒ Object
Removes 0s from partially normalized numbers such as 410443643533.
Example:
410443643533 -> 41443643533
In some cases it doesn’t, like Italy.
Note: Options such as CC
168 169 170 171 172 173 174 |
# File 'lib/phony/country.rb', line 168 def normalize(national_number, = {}) clean! national_number @codes.each_with_object national_number do |code, number| result = code.normalize number, break result if result end end |
#plausible?(rest, hints = {}) ⇒ Boolean
Tests for plausibility of this national number.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/phony/country.rb', line 178 def plausible?(rest, hints = {}) local, _, ndc, *rest = internal_split 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) # If there is no local part, we can assume it's not a plausible number. # (Or, not defined correctly in Phony yet) return false unless local # Local code specific checks. # local.plausible? rest, hints end |
#split(national_number) ⇒ Trunk, ...
A number is split with the code handlers as given in the initializer.
Note: If the ndc is nil, it will not return it.
51 52 53 54 |
# File 'lib/phony/country.rb', line 51 def split(national_number) _, trunk, ndc, *rest = internal_split national_number [trunk, ndc, *rest] end |
#vanity?(national_number) ⇒ Boolean
Is this national number a vanity number?
209 210 211 |
# File 'lib/phony/country.rb', line 209 def vanity?(national_number) Vanity.vanity? national_number end |
#vanity_to_number(vanity_number) ⇒ Object
213 214 215 216 |
# File 'lib/phony/country.rb', line 213 def vanity_to_number(vanity_number) _, ndc, *rest = split vanity_number "#{ndc}#{Vanity.replace(rest.join)}" end |
#with(cc, options = {}) ⇒ Object
Options.
TODO Rewrite.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/phony/country.rb', line 34 def with(cc, = {}) @cc = cc @invalid_ndcs = [:invalid_ndcs] @format = [:format] @space = [:space] @local_space = [:local_space] @parentheses = [:parentheses] end |
#|(other) ⇒ Object
DSL method.
Chain two codes together.
25 26 27 28 |
# File 'lib/phony/country.rb', line 25 def |(other) self.codes = codes + other.codes self end |