phony 2.20.13 → 2.20.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/lib/phony/config.rb +25 -26
  3. data/lib/phony/countries/argentina.rb +311 -314
  4. data/lib/phony/countries/austria.rb +71 -72
  5. data/lib/phony/countries/bangladesh.rb +27 -28
  6. data/lib/phony/countries/belarus.rb +110 -111
  7. data/lib/phony/countries/brazil.rb +92 -92
  8. data/lib/phony/countries/cambodia.rb +9 -9
  9. data/lib/phony/countries/china.rb +22 -21
  10. data/lib/phony/countries/croatia.rb +6 -5
  11. data/lib/phony/countries/georgia.rb +84 -84
  12. data/lib/phony/countries/germany.rb +4067 -4068
  13. data/lib/phony/countries/guinea.rb +8 -8
  14. data/lib/phony/countries/india.rb +31 -34
  15. data/lib/phony/countries/indonesia.rb +57 -46
  16. data/lib/phony/countries/ireland.rb +4 -5
  17. data/lib/phony/countries/italy.rb +54 -53
  18. data/lib/phony/countries/japan.rb +456 -456
  19. data/lib/phony/countries/kyrgyzstan.rb +109 -109
  20. data/lib/phony/countries/latvia.rb +34 -34
  21. data/lib/phony/countries/libya.rb +106 -106
  22. data/lib/phony/countries/malaysia.rb +12 -12
  23. data/lib/phony/countries/moldova.rb +42 -42
  24. data/lib/phony/countries/montenegro.rb +24 -20
  25. data/lib/phony/countries/myanmar.rb +39 -39
  26. data/lib/phony/countries/namibia.rb +35 -29
  27. data/lib/phony/countries/nepal.rb +60 -62
  28. data/lib/phony/countries/netherlands.rb +13 -13
  29. data/lib/phony/countries/pakistan.rb +135 -111
  30. data/lib/phony/countries/paraguay.rb +135 -135
  31. data/lib/phony/countries/russia_kazakhstan_abkhasia_south_ossetia.rb +125 -128
  32. data/lib/phony/countries/saudi_arabia.rb +5 -5
  33. data/lib/phony/countries/serbia.rb +47 -39
  34. data/lib/phony/countries/somalia.rb +18 -18
  35. data/lib/phony/countries/south_korea.rb +16 -13
  36. data/lib/phony/countries/sweden.rb +28 -28
  37. data/lib/phony/countries/taiwan.rb +20 -19
  38. data/lib/phony/countries/tajikistan.rb +70 -70
  39. data/lib/phony/countries/turkmenistan.rb +65 -65
  40. data/lib/phony/countries/ukraine.rb +607 -607
  41. data/lib/phony/countries/united_kingdom.rb +66 -66
  42. data/lib/phony/countries/uruguay.rb +40 -41
  43. data/lib/phony/countries/vietnam.rb +18 -17
  44. data/lib/phony/countries/zimbabwe.rb +31 -31
  45. data/lib/phony/countries.rb +440 -443
  46. data/lib/phony/country.rb +42 -44
  47. data/lib/phony/country_codes.rb +61 -60
  48. data/lib/phony/dsl.rb +23 -26
  49. data/lib/phony/local_splitters/fixed.rb +20 -29
  50. data/lib/phony/local_splitters/regex.rb +28 -31
  51. data/lib/phony/national_code.rb +7 -10
  52. data/lib/phony/national_splitters/default.rb +9 -11
  53. data/lib/phony/national_splitters/dsl.rb +8 -18
  54. data/lib/phony/national_splitters/fixed.rb +11 -14
  55. data/lib/phony/national_splitters/none.rb +7 -11
  56. data/lib/phony/national_splitters/regex.rb +13 -17
  57. data/lib/phony/national_splitters/variable.rb +36 -38
  58. data/lib/phony/trunk_code.rb +16 -18
  59. data/lib/phony/vanity.rb +12 -13
  60. data/lib/phony.rb +84 -84
  61. metadata +5 -39
  62. data/spec/functional/config_spec.rb +0 -44
  63. data/spec/functional/plausibility_spec.rb +0 -689
  64. data/spec/lib/phony/countries_spec.rb +0 -1459
  65. data/spec/lib/phony/country_codes_spec.rb +0 -227
  66. data/spec/lib/phony/country_spec.rb +0 -104
  67. data/spec/lib/phony/dsl_spec.rb +0 -28
  68. data/spec/lib/phony/local_splitters/fixed_spec.rb +0 -56
  69. data/spec/lib/phony/local_splitters/regex_spec.rb +0 -94
  70. data/spec/lib/phony/national_code_spec.rb +0 -61
  71. data/spec/lib/phony/national_splitters/default_spec.rb +0 -34
  72. data/spec/lib/phony/national_splitters/fixed_spec.rb +0 -45
  73. data/spec/lib/phony/national_splitters/none_spec.rb +0 -28
  74. data/spec/lib/phony/national_splitters/regex_spec.rb +0 -23
  75. data/spec/lib/phony/national_splitters/variable_spec.rb +0 -35
  76. data/spec/lib/phony/trunk_code_spec.rb +0 -85
  77. data/spec/lib/phony/vanity_spec.rb +0 -30
  78. data/spec/lib/phony_spec.rb +0 -70
@@ -1,7 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
-
3
4
  module LocalSplitters
4
-
5
5
  # Local splitter class to split the last part of
6
6
  # a number, i.e. minus cc or ndc.
7
7
  #
@@ -11,63 +11,60 @@ module Phony
11
11
  # to avoid getting new local splitter instances.
12
12
  #
13
13
  class Regex
14
-
15
14
  attr_reader :fallback, :mapping
16
-
15
+
17
16
  # Get a splitter for the given format.
18
17
  #
19
18
  # Note: Not cached.
20
19
  #
21
- def self.instance_for mapping
20
+ def self.instance_for(mapping)
22
21
  new mapping
23
22
  end
24
-
23
+
25
24
  # Initialize with a regex => format mapping.
26
25
  #
27
- def initialize mapping
26
+ def initialize(mapping)
28
27
  @fallback = mapping.delete(:fallback) || [12]
29
28
  @mapping = mapping
30
29
  end
31
-
30
+
32
31
  # Split a local number according to an assumed country specific format.
33
32
  #
34
33
  # Examples
35
34
  # * split '3643533' # => ['364', '35', '33'] # (Switzerland)
36
35
  #
37
- def split number
36
+ def split(number)
38
37
  mapping.each do |regex, format|
39
- next unless number =~ regex
38
+ next unless number&.match?(regex)
39
+
40
40
  return split_with(number, format)
41
41
  end
42
42
  split_with number, fallback
43
43
  end
44
-
45
- def plausible? rest, hints = {}
46
- number = rest.inject('', :+)
44
+
45
+ def plausible?(rest, _hints = {})
46
+ number = rest.sum('')
47
47
  mapping.each do |regex, format|
48
- next unless number =~ regex
48
+ next unless number&.match?(regex)
49
+
49
50
  return plausible_with? number, format
50
51
  end
51
52
  plausible_with? number, fallback
52
53
  end
53
-
54
+
54
55
  private
55
-
56
- def split_with number, format
57
- format.inject([]) do |result, size|
58
- result << number.slice!(0..size-1)
59
- return result if number.empty?
60
- result
61
- end << number
62
- end
63
-
64
- def plausible_with? number, format
65
- length = format.inject 0, :+
66
- number.length == length
67
- end
68
56
 
57
+ def split_with(number, format)
58
+ format.each_with_object([]) do |size, result|
59
+ result << number.slice!(0..size - 1)
60
+ return result if number.empty?
61
+ end << number
62
+ end
63
+
64
+ def plausible_with?(number, format)
65
+ length = format.sum
66
+ number.length == length
67
+ end
69
68
  end
70
-
71
69
  end
72
-
73
- end
70
+ end
@@ -1,14 +1,12 @@
1
- module Phony
1
+ # frozen_string_literal: true
2
2
 
3
+ module Phony
3
4
  # NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.
4
5
  #
5
6
  class NationalCode
6
-
7
7
  attr_reader :local_splitter
8
-
9
- #
10
- #
11
- def initialize national_splitter, local_splitter
8
+
9
+ def initialize(national_splitter, local_splitter)
12
10
  @national_splitter = national_splitter
13
11
  @local_splitter = local_splitter
14
12
  end
@@ -16,9 +14,10 @@ module Phony
16
14
  # Split gets a number without country code and splits it into
17
15
  # its parts.
18
16
  #
19
- def split national_number
17
+ def split(national_number)
20
18
  zero, ndc_or_rest, rest = @national_splitter.split national_number.dup
21
19
  return [zero, ndc_or_rest] unless rest
20
+
22
21
  [zero, ndc_or_rest, *@local_splitter.split(rest)]
23
22
  end
24
23
 
@@ -26,10 +25,8 @@ module Phony
26
25
  #
27
26
  # Note: Some cases, like Italy, don't remove the relative zero.
28
27
  #
29
- def normalize national_number, options = {}
28
+ def normalize(national_number, _options = {})
30
29
  national_number.gsub(/\A0+/, EMPTY_STRING)
31
30
  end
32
-
33
31
  end
34
-
35
32
  end
@@ -1,15 +1,14 @@
1
- module Phony
1
+ # frozen_string_literal: true
2
2
 
3
+ module Phony
3
4
  module NationalSplitters
4
-
5
- # TODO Default = Fixed.new(...)?
5
+ # TODO: Default = Fixed.new(...)?
6
6
  #
7
7
  class Default < DSL
8
-
9
8
  def self.instance_for
10
- @instance ||= new
9
+ @instance_for ||= new
11
10
  end
12
-
11
+
13
12
  # "Splits" the national part of a phone number into a single piece.
14
13
  #
15
14
  # @param [String] national_number An national part of a number.
@@ -19,10 +18,10 @@ module Phony
19
18
  # @example Split the national part of a Swiss number.
20
19
  # Phony.split("1234567") # => ["1234567"]
21
20
  #
22
- def split national_number
21
+ def split(national_number)
23
22
  [nil, national_number]
24
23
  end
25
-
24
+
26
25
  # By default, the national part of a number is always plausible.
27
26
  #
28
27
  # @param [String] rest An national part of a number (ignored).
@@ -34,16 +33,15 @@ module Phony
34
33
  # @example Split the national part of a Swiss number.
35
34
  # Phony.plausible?("1234567") # => true
36
35
  #
37
- def plausible? rest, size, hints = {}
36
+ def plausible?(_rest, _size, _hints = {})
38
37
  true
39
38
  end
40
-
39
+
41
40
  # A valid length (at least 3).
42
41
  #
43
42
  def length
44
43
  3
45
44
  end
46
-
47
45
  end
48
46
  end
49
47
  end
@@ -1,31 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
-
3
4
  module NationalSplitters
4
-
5
5
  # TODO
6
6
  #
7
7
  class DSL
8
-
9
- #
10
- #
11
- def >> local_splitter
8
+ def >>(local_splitter)
12
9
  country_for local_splitter
13
10
  end
14
-
15
- #
16
- #
17
- def country_for local_splitter
11
+
12
+ def country_for(local_splitter)
18
13
  Phony::Country.new Phony::NationalCode.new(self, local_splitter)
19
14
  end
20
15
 
21
- # TODO Remove?
16
+ # TODO: Remove?
22
17
  #
23
- def reserved
24
-
25
- end
26
-
18
+ def reserved; end
27
19
  end
28
-
29
20
  end
30
-
31
- end
21
+ end
@@ -1,15 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
-
3
4
  module NationalSplitters
4
-
5
5
  # TODO
6
6
  #
7
7
  class Fixed < DSL
8
-
9
8
  attr_writer :special_splitter
10
-
9
+
11
10
  @mapping = {}
12
-
11
+
13
12
  # Get a splitter for the given format.
14
13
  #
15
14
  # Caches the created splitter for the given format.
@@ -17,27 +16,25 @@ module Phony
17
16
  def self.instance_for *parameters
18
17
  @mapping[parameters] ||= new(*parameters)
19
18
  end
20
-
21
- def initialize size, options = {}
19
+
20
+ def initialize(size, _options = {})
22
21
  @size = size
23
22
  @zero = nil # options[:zero] == false ? nil : '0'
24
23
  end
25
-
24
+
26
25
  # Takes a national number and splits it into ndc and rest.
27
26
  #
28
- def split national_number
27
+ def split(national_number)
29
28
  return [@zero, national_number] unless @size
29
+
30
30
  [@zero, national_number.slice!(0...@size), national_number]
31
31
  end
32
-
32
+
33
33
  # A valid length.
34
34
  #
35
35
  def length
36
36
  @size
37
37
  end
38
-
39
38
  end
40
-
41
39
  end
42
-
43
- end
40
+ end
@@ -1,18 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
-
3
4
  module NationalSplitters
4
-
5
5
  # This is a national splitter for countries
6
6
  # which have no NDC / Area Code.
7
7
  #
8
8
  class None < Default
9
-
10
9
  # Get a splitter. Caches.
11
10
  #
12
11
  def self.instance_for(*)
13
- @instance ||= new
12
+ @instance_for ||= new
14
13
  end
15
-
14
+
16
15
  # On false:
17
16
  #
18
17
  # This is a hack to make phony's
@@ -33,18 +32,15 @@ module Phony
33
32
  #
34
33
  # Note: Decided it stays in. When formatting, it's turned into nil.
35
34
  #
36
- def split national_number
35
+ def split(national_number)
37
36
  [nil, false, national_number]
38
37
  end
39
-
38
+
40
39
  # A valid length.
41
40
  #
42
41
  def length
43
42
  0
44
43
  end
45
-
46
44
  end
47
-
48
45
  end
49
-
50
- end
46
+ end
@@ -1,7 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
-
3
4
  module NationalSplitters
4
-
5
5
  # National splitter class to split the ndc-local part of a number.
6
6
  #
7
7
  # Countries can create new instances according to their needs.
@@ -10,46 +10,42 @@ module Phony
10
10
  # to avoid getting new local splitter instances.
11
11
  #
12
12
  class Regex < Fixed
13
-
14
13
  attr_reader :on_fail_take, :regex
15
-
14
+
16
15
  # Get a splitter for the given format.
17
16
  #
18
17
  # Note: Not cached.
19
18
  #
20
- def self.instance_for regex, on_fail_take = nil, options = {}
19
+ def self.instance_for(regex, on_fail_take = nil, options = {})
21
20
  new regex, on_fail_take, options
22
21
  end
23
22
 
24
- def initialize regex, on_fail_take = nil, options = {}
25
- super on_fail_take, options
23
+ def initialize(regex, on_fail_take = nil, options = {})
24
+ super(on_fail_take, options)
26
25
 
27
26
  @regex = regex
28
27
  end
29
-
28
+
30
29
  # Split a local number according to an assumed country specific format.
31
30
  #
32
31
  # Examples
33
32
  # * split '3643533' # => ['364', '35', '33'] # (Switzerland)
34
33
  #
35
- def split national_number
34
+ def split(national_number)
36
35
  # Improve matching.
37
36
  #
38
- return [@zero, national_number.slice!(0..$1.size-1), national_number] if national_number =~ regex
39
-
37
+ return [@zero, national_number.slice!(0..::Regexp.last_match(1).size - 1), national_number] if national_number =~ regex
38
+
40
39
  # Not found.
41
40
  #
42
- super national_number
41
+ super(national_number)
43
42
  end
44
-
43
+
45
44
  # A valid length.
46
45
  #
47
46
  def length
48
47
  # raise "#{self.class.name} has no length that can be automatically extracted."
49
48
  end
50
-
51
49
  end
52
-
53
50
  end
54
-
55
- end
51
+ end
@@ -1,68 +1,66 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
-
3
4
  module NationalSplitters
4
-
5
5
  class Variable < Fixed
6
-
7
- #
8
- #
9
- def initialize fallback, ndcs, options = {}
10
- super fallback, options
6
+ def initialize(fallback, ndcs, options = {})
7
+ super(fallback, options)
11
8
  @ndcs = optimize ndcs
12
9
  end
13
-
10
+
14
11
  # Takes a national number and splits it into ndc and rest.
15
12
  #
16
- def split national_number
13
+ def split(national_number)
17
14
  fallback_number = national_number.dup
18
-
15
+
19
16
  # Extract a starting point.
20
17
  #
21
18
  # This if can possibly be removed.
22
19
  #
23
- presumed_code = if @mapped_ndc_min_length > 0
24
- presumed_code = national_number.slice!(0..@mapped_ndc_min_length-1)
25
- else
26
- ''
27
- end
28
-
20
+ presumed_code = if @mapped_ndc_min_length.positive?
21
+ presumed_code = national_number.slice!(0..@mapped_ndc_min_length - 1)
22
+ else
23
+ ''
24
+ end
25
+
29
26
  # Try for all possible mapped.
30
27
  #
31
28
  @mapped_ndc_min_length.upto @mapped_ndc_max_length do |i|
32
29
  ndcs_of_size_i = @ndcs[i]
33
- return [@zero, presumed_code, national_number] unless ndcs_of_size_i && !ndcs_of_size_i.include?(presumed_code)
30
+ unless ndcs_of_size_i && !ndcs_of_size_i.include?(presumed_code)
31
+ return [@zero, presumed_code,
32
+ national_number]
33
+ end
34
+
34
35
  presumed_code << national_number.slice!(0..0)
35
36
  end
36
-
37
+
37
38
  # Not found.
38
39
  #
39
- super fallback_number
40
+ super(fallback_number)
40
41
  end
41
-
42
+
42
43
  # A valid length.
43
44
  #
44
45
  def length
45
46
  (@mapped_ndc_min_length..@mapped_ndc_max_length)
46
47
  end
47
-
48
+
48
49
  private
49
-
50
- # Optimizes and restructures the given ndcs array.
51
- #
52
- def optimize ndc_ary
53
- ndcs = {}
54
- ndc_ary.each do |ndc|
55
- ndcs[ndc.length] ||= []
56
- ndcs[ndc.length] << ndc
57
- end
58
- keys = ndcs.keys
59
- @mapped_ndc_min_length = keys.min # || 1
60
- @mapped_ndc_max_length = keys.max
61
- ndcs
50
+
51
+ # Optimizes and restructures the given ndcs array.
52
+ #
53
+ def optimize(ndc_ary)
54
+ ndcs = {}
55
+ ndc_ary.each do |ndc|
56
+ ndcs[ndc.length] ||= []
57
+ ndcs[ndc.length] << ndc
62
58
  end
63
-
59
+ keys = ndcs.keys
60
+ @mapped_ndc_min_length = keys.min # || 1
61
+ @mapped_ndc_max_length = keys.max
62
+ ndcs
63
+ end
64
64
  end
65
-
66
65
  end
67
-
68
- end
66
+ end
@@ -1,7 +1,7 @@
1
- module Phony
1
+ # frozen_string_literal: true
2
2
 
3
+ module Phony
3
4
  class TrunkCode
4
-
5
5
  # Parameters:
6
6
  # * code: The trunk code, e.g. 0.
7
7
  #
@@ -10,17 +10,17 @@ module Phony
10
10
  # * split: [true, false (default)] Remove the trunk code when splitting (only use if number scheme is defined unambiguously).
11
11
  # * format: [true (default), false] Add the trunk code when formatting (passing `false` will not add it).
12
12
  #
13
- def initialize code, options = {}
13
+ def initialize(code, options = {})
14
14
  @code = code
15
- @trunk_code_replacement = /\A#{code.gsub(%r{%s}, '')}/
15
+ @trunk_code_replacement = /\A#{code.gsub(/%s/, '')}/
16
16
  @normalize = options[:normalize] || options[:normalize].nil?
17
17
  @split = options[:split]
18
18
  @format = options[:format] || options[:format].nil?
19
19
  end
20
-
20
+
21
21
  # Prepends itself to the other codes.
22
22
  #
23
- def | other
23
+ def |(other)
24
24
  other.codes.unshift self
25
25
  other
26
26
  end
@@ -28,30 +28,28 @@ module Phony
28
28
  # Split gets a number without country code and splits it into
29
29
  # its parts.
30
30
  #
31
- def split national_number
31
+ def split(national_number)
32
32
  national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @split
33
- return [self, national_number]
33
+ [self, national_number]
34
34
  end
35
-
35
+
36
36
  # Normalize normalizes the given national number.
37
37
  #
38
- def normalize national_number, options = {}
38
+ def normalize(national_number, options = {})
39
39
  national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @normalize && options[:cc]
40
- return national_number
40
+ national_number
41
41
  end
42
-
42
+
43
43
  # Format the trunk code using the spaces given.
44
44
  #
45
- def format space, force = nil
46
- if force || @format
45
+ def format(space, force = nil)
46
+ return unless force || @format
47
47
  if @code.size > 1
48
48
  (@code % space).gsub(/\D/, ' ')
49
49
  else
50
50
  @code
51
51
  end
52
- end
52
+
53
53
  end
54
-
55
54
  end
56
-
57
- end
55
+ end
data/lib/phony/vanity.rb CHANGED
@@ -1,38 +1,37 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
-
3
4
  # Helper module that maps vanity numbers to digit numbers.
4
5
  #
5
6
  module Vanity
6
-
7
7
  # Returns a char to number mapping string for the String#tr method.
8
8
  #
9
9
  def self.mapping
10
- @@mapping ||= [
11
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.freeze,
12
- '2223334445556667777888999922233344455566677778889999'.freeze
10
+ @@mapping ||= %w[
11
+ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
12
+ 2223334445556667777888999922233344455566677778889999
13
13
  ]
14
14
  end
15
-
15
+
16
16
  # Replaces (and normalizes) vanity characters of passed number with correct digits.
17
17
  #
18
- def self.replace number
18
+ def self.replace(number)
19
19
  number.tr(*mapping)
20
20
  end
21
-
21
+
22
22
  # Returns true if there is a character in the number
23
23
  # after the first three numbers.
24
24
  #
25
25
  @@vanity_regexp = /\A\d{3}[a-zA-Z]{6,12}\Z/
26
- def self.vanity? number
26
+ def self.vanity?(number)
27
27
  !(normalized(number) =~ @@vanity_regexp).nil?
28
28
  end
29
-
29
+
30
30
  # Vanity-Normalized.
31
31
  #
32
32
  @@vanity_normalizing_regexp = /^0*|[^\w]/
33
- def self.normalized number
33
+ def self.normalized(number)
34
34
  number.gsub @@vanity_normalizing_regexp, ''
35
35
  end
36
-
37
36
  end
38
- end
37
+ end