phony 1.9.0 → 2.19.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/README.textile +64 -112
  3. data/lib/phony/config.rb +91 -0
  4. data/lib/phony/countries/argentina.rb +355 -0
  5. data/lib/phony/countries/austria.rb +56 -22
  6. data/lib/phony/countries/bangladesh.rb +57 -0
  7. data/lib/phony/countries/belarus.rb +133 -0
  8. data/lib/phony/countries/brazil.rb +101 -95
  9. data/lib/phony/countries/cambodia.rb +131 -0
  10. data/lib/phony/countries/china.rb +13 -6
  11. data/lib/phony/countries/croatia.rb +23 -0
  12. data/lib/phony/countries/georgia.rb +94 -0
  13. data/lib/phony/countries/germany.rb +66 -42
  14. data/lib/phony/countries/guinea.rb +46 -0
  15. data/lib/phony/countries/india.rb +50 -0
  16. data/lib/phony/countries/indonesia.rb +55 -0
  17. data/lib/phony/countries/ireland.rb +35 -28
  18. data/lib/phony/countries/italy.rb +272 -166
  19. data/lib/phony/countries/japan.rb +468 -0
  20. data/lib/phony/countries/kyrgyzstan.rb +120 -0
  21. data/lib/phony/countries/latvia.rb +43 -0
  22. data/lib/phony/countries/libya.rb +116 -0
  23. data/lib/phony/countries/malaysia.rb +31 -7
  24. data/lib/phony/countries/moldova.rb +53 -0
  25. data/lib/phony/countries/montenegro.rb +30 -0
  26. data/lib/phony/countries/myanmar.rb +55 -0
  27. data/lib/phony/countries/namibia.rb +37 -0
  28. data/lib/phony/countries/nepal.rb +73 -0
  29. data/lib/phony/countries/netherlands.rb +17 -5
  30. data/lib/phony/countries/pakistan.rb +121 -0
  31. data/lib/phony/countries/paraguay.rb +147 -0
  32. data/lib/phony/countries/{russia_kazakhstan_abhasia_south_osetia.rb → russia_kazakhstan_abkhasia_south_ossetia.rb} +35 -24
  33. data/lib/phony/countries/saudi_arabia.rb +40 -0
  34. data/lib/phony/countries/serbia.rb +47 -0
  35. data/lib/phony/countries/somalia.rb +26 -0
  36. data/lib/phony/countries/south_korea.rb +19 -10
  37. data/lib/phony/countries/sweden.rb +58 -38
  38. data/lib/phony/countries/taiwan.rb +28 -0
  39. data/lib/phony/countries/tajikistan.rb +79 -0
  40. data/lib/phony/countries/turkmenistan.rb +76 -0
  41. data/lib/phony/countries/ukraine.rb +630 -0
  42. data/lib/phony/countries/united_kingdom.rb +639 -44
  43. data/lib/phony/countries/uruguay.rb +53 -0
  44. data/lib/phony/countries/vietnam.rb +133 -0
  45. data/lib/phony/countries/zimbabwe.rb +39 -0
  46. data/lib/phony/countries.rb +901 -301
  47. data/lib/phony/country.rb +177 -20
  48. data/lib/phony/country_codes.rb +119 -101
  49. data/lib/phony/dsl.rb +113 -68
  50. data/lib/phony/local_splitters/fixed.rb +25 -1
  51. data/lib/phony/local_splitters/regex.rb +16 -2
  52. data/lib/phony/national_code.rb +7 -7
  53. data/lib/phony/national_splitters/default.rb +35 -3
  54. data/lib/phony/national_splitters/dsl.rb +12 -7
  55. data/lib/phony/national_splitters/fixed.rb +7 -1
  56. data/lib/phony/national_splitters/none.rb +7 -3
  57. data/lib/phony/national_splitters/regex.rb +6 -0
  58. data/lib/phony/national_splitters/variable.rb +13 -9
  59. data/lib/phony/trunk_code.rb +57 -0
  60. data/lib/phony/vanity.rb +3 -3
  61. data/lib/phony.rb +239 -55
  62. data/spec/functional/config_spec.rb +44 -0
  63. data/spec/functional/plausibility_spec.rb +656 -0
  64. data/spec/lib/phony/countries_spec.rb +1207 -119
  65. data/spec/lib/phony/country_codes_spec.rb +99 -81
  66. data/spec/lib/phony/country_spec.rb +54 -14
  67. data/spec/lib/phony/dsl_spec.rb +2 -2
  68. data/spec/lib/phony/local_splitters/fixed_spec.rb +4 -4
  69. data/spec/lib/phony/local_splitters/regex_spec.rb +50 -2
  70. data/spec/lib/phony/national_code_spec.rb +34 -34
  71. data/spec/lib/phony/national_splitters/default_spec.rb +34 -0
  72. data/spec/lib/phony/national_splitters/fixed_spec.rb +12 -6
  73. data/spec/lib/phony/national_splitters/none_spec.rb +13 -3
  74. data/spec/lib/phony/national_splitters/regex_spec.rb +1 -1
  75. data/spec/lib/phony/national_splitters/variable_spec.rb +11 -5
  76. data/spec/lib/phony/trunk_code_spec.rb +85 -0
  77. data/spec/lib/phony/vanity_spec.rb +15 -19
  78. data/spec/lib/phony_spec.rb +59 -277
  79. metadata +67 -34
  80. data/lib/phony/validator.rb +0 -26
  81. data/lib/phony/validators.rb +0 -88
  82. data/spec/lib/phony/validations_spec.rb +0 -109
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4f497a4a52bd0c47e845d1fa471e5465e1c949349fdcf78cf5e6f4ef6f501ee9
4
+ data.tar.gz: 64bff396ac5a0a20fd27a8b8176ea71ec3fc569c129c7297e7910ae02b1a473d
5
+ SHA512:
6
+ metadata.gz: 20f087521f9e50b8fa1b31418ad24363fe97949ca7eae3b787429dbfdd2c8ee83d66a99f898033e5890e76351517fc237746084e68d33878d7a258c69179bfb8
7
+ data.tar.gz: a29e55bf204811d025b7ba758ab04e3584eabb43ccefc9011c8bc4c6b1e122f5e50ad5b8f714012a35c6c4e637d5894a2272ea40810765e01de3b3c24d85497a
data/README.textile CHANGED
@@ -1,164 +1,116 @@
1
1
  "!{float:right}https://secure.travis-ci.org/floere/phony.png!":http://travis-ci.org/floere/phony
2
+ !https://coveralls.io/repos/floere/phony/badge.svg?branch=master(Coverage Status)!:https://coveralls.io/r/floere/phony?branch=master
3
+ "!https://codeclimate.com/github/floere/phony.png!":https://codeclimate.com/github/floere/phony
4
+ !http://inch-ci.org/github/floere/phony.png!:http://inch-ci.org/github/floere/phony
2
5
 
3
6
  h1. Phony
4
7
 
5
- Used in e.g.: "airbnb.com":http://airbnb.com, "restorm.com":http://restorm.com, "socialcam.com":http://socialcam.com, "zendesk.com":http://www.zendesk.com/ (and many, many others).
8
+ Disclaimer: Phony works with international numbers only, such as @61 412 345 678@!
6
9
 
7
- There's also a Rails wrapper: https://github.com/joost/phony_rails.
10
+ The (admittedly crazy) goal of this Gem is to be able to normalize/format/split all phone numbers in the world.
8
11
 
9
- h2. Description
10
-
11
- This gem can normalize, format and split E164 numbers.
12
- "More about E164 numbers in this Wiki":http://en.wikipedia.org/wiki/E.164.
13
-
14
- The (admittedly crazy) *goal* of this Gem is to be able to format/split all phone numbers in the world.
15
-
16
- Currently handles Abhas, Afghan, Algerian, Austrian, Australian, Belgian, Brazilian, Cambodian, Chilean, Chinese, Croatian, Cuban, Cypriot, Czech, Danish, Dutch, Egyptian, Estonian, French, German, Ghanan, Gibraltar, Greek, Hungarian, Italian, Irish, Kazakh, Lithuanian, Luxembourgian, Malaysian, Malta, Mexican, Monaco, Morocco, New Zealand, Nigerian, Norwegian, Peruvian, Polish, Romanian, Russian, Rwandan, Singapore, Slovakian, South African, South Korean, South Osetian, Spanish, Sudan, Swedish, Swiss, Thailand, Tunisian, Turkish, Liechtenstein, UK, US, Venezuelan, and Vietnamese numbers.
17
-
18
- And to some extent, all others. Just try if it works for you.
19
-
20
- If it doesn't, please "enter an issue":http://github.com/floere/phony/issues.
21
-
22
- h2. Installation
23
-
24
- <pre><code>gem install phony</code></pre>
25
-
26
- h2(#examples). Some examples
27
-
28
- h3(#plausibility). Plausibility
29
-
30
- "Plausible" means "seems reasonable or probable", not implying 100% correctness on a @true@, but implying 100% correctness on a @false@ return value. So if this returns @true@, it might still be not a plausible number. If it returns @false@, it is definitely not an E164 conform number.
31
-
32
- Use this method in combination with @normalize@ for saving it into the database. Also see https://github.com/floere/phony/issues/35 for a discussion on the topic.
33
-
34
- Note that you can add constraints to the plausibility check like the country code, @cc@, and the national destination code, @ndc@. Use the options to further limit the plausibility range. For example, only allow a small number of countries.
35
-
36
- @Phony.plausible?('0000000').should be_false@
37
-
38
- @Phony.plausible?('hello').should be_false@
39
-
40
- @Phony.plausible?('+41 44 111 22 33').should be_true@
12
+ Used in: "airbnb.com":http://airbnb.com, "socialcam.com":http://socialcam.com, "zendesk.com":http://www.zendesk.com/ (and many, many others).
41
13
 
42
- @Phony.plausible?('+41 44 111 22 33', cc: '41').should be_true@
14
+ h2(#memory). Runtime Memory Usage
43
15
 
44
- @Phony.plausible?('+41 44 111 22 33', ndc: '44').should be_true@
16
+ According to "memory_profiler":https://github.com/SamSaffron/memory_profiler, the Phony gem uses roughly 1MB of memory per Ruby process.
17
+ Usage was generated using (look for @Total retained@): @ruby -e 'require "memory_profiler"; MemoryProfiler.report(allow_files: "phony"){ require "phony" }.pretty_print'@
45
18
 
46
- @Phony.plausible?('+41 44 111 22 33', cc: '1').should be_false@
47
-
48
- @Phony.plausible?('+41 44 111 22 33', ndc: '43').should be_false@
49
-
50
- @Phony.plausible?('+41 44 111 22 33', cc: '41', ndc: '44').should be_true@
51
-
52
- @Phony.plausible?('+41 44 111 22 33', cc: /4(0|2)/, ndc: /4(4|5)/).should be_false@
53
-
54
- @Phony.plausible?('+41 44 111 22 33', cc: /4(0|1)/, ndc: /4(4|5)/).should be_true@
55
-
56
- h3(#normalizing). Normalizing
57
-
58
- This will often raise an error if you try normalizing a non E164-izable number (a number that does not contain enough information to be normalized into an E164 conform number). Use @Phony.plausible?@ for checking if it can be normalized first.
59
-
60
- @Phony.normalize('41443643533').should == '41443643533'@
61
-
62
- @Phony.normalize('+41 44 364 35 33').should == '41443643533'@
63
-
64
- @Phony.normalize('+41 44 364 35 33').should == '41443643533'@
65
-
66
- @Phony.normalize('+41 800 11 22 33').should == '41800112233'@
67
-
68
- @Phony.normalize('John: +41 44 364 35 33').should == '41443643533'@
69
-
70
- @Phony.normalize('1 (703) 451-5115').should == '17034515115'@
71
-
72
- @Phony.normalize('1-888-407-4747').should == '18884074747'@
73
-
74
- @Phony.normalize('1.906.387.1698').should == '19063871698'@
75
-
76
- @Phony.normalize('+41 (044) 364 35 33').should == '41443643533'@
19
+ h2. Description
77
20
 
78
- h3(#formatting). Formatting
21
+ This gem normalizes, formats and splits "*E164 phone numbers*":http://en.wikipedia.org/wiki/E.164. A valid E164 phone number *must* include a country code.
79
22
 
80
- @Phony.formatted('41443643532').should == '+41 44 364 35 32'@
23
+ E164 numbers are international numbers with a country dial prefix, usually an area code and a subscriber number. For example, the Australian number @+61 412 345 678@ can be broken down into the following components:
24
+ * Country Code (CC): a country code of @61@
25
+ * National Destination Code (NDC): a mobile number denoted by the @4@ (specific to Australia)
26
+ * Local Number Part: a subscriber number of @12 345 678@
81
27
 
82
- @Phony.formatted('41800112233').should == '+41 800 11 22 33'@
28
+ It currently handles the countries listed at the end of this README.
83
29
 
84
- @Phony.formatted('43198110').should == '+43 1 98110'@
30
+ It is covered by roughly 2,250 tests that run in 2 seconds (April 2019).
31
+ If it doesn't work, please "enter an issue":http://github.com/floere/phony/issues or better, fork and "send a pull request":http://github.com/floere/phony/pulls.
85
32
 
86
- @Phony.formatted('18705551122').should == '+1 870 555 1122'@
33
+ h2. Installation
87
34
 
88
- h4(#international). International
35
+ With Rails? Check out: https://github.com/joost/phony_rails.
89
36
 
90
- @Phony.formatted('18091231234', :format => :international).should == '+1 809 123 1234'@
37
+ With Bundler: Append @gem 'phony'@ to your @Gemfile@ and @bundle install@ it.
91
38
 
92
- @Phony.formatted('43198110', :format => :international).should == '+43 1 98110'@
39
+ Without Bundler: Run @gem install phony@ from your command line.
93
40
 
94
- @Phony.formatted('43198110', :format => :international_absolute).should == '+43 1 98110'@
41
+ h2(#usage). Usage docs
95
42
 
96
- @Phony.formatted('33142278186', :format => :+).should == '+33 1 42 27 81 86'@
43
+ Phony uses "qed":https://github.com/rubyworks/qed as docs and to run its functional tests. Start here for usage docs: "Usage index":./qed/index.md.
97
44
 
98
- @Phony.formatted('43198110', :format => :international_relative).should == '0043 1 98110'@
45
+ h3(#normalizing). Phony.normalize(number)
99
46
 
100
- @Phony.formatted('4233841148', :format => :international_relative).should == '00423 384 11 48'@
47
+ Normalize intelligently removes all non-numeric characters of a number. Do it before storing a number in a DB.
101
48
 
102
- h4(#spaces). With spaces
49
+ "Phony.normalize docs":./qed/normalize.md
103
50
 
104
- @Phony.formatted('18091231234', :format => :international, :spaces => '').should == '+18091231234'@
51
+ @Phony.normalize('1-888-407-4747').assert == '18884074747'@
105
52
 
106
- @Phony.formatted('43198110', :format => :international, :spaces => '').should == '+43198110'@
53
+ h3(#formatting). Phony.format(number, options = {})
107
54
 
108
- @Phony.formatted('43198110', :format => :international_absolute, :spaces => '').should == '+43198110'@
55
+ Format formats a normalized number according to a country's predominant formatting. Lots of options for int'l, national, local formatting.
109
56
 
110
- @Phony.formatted('33142278186', :format => :+, :spaces => '').should == '+33142278186'@
57
+ "Phony.format docs":./qed/format.md
111
58
 
112
- @Phony.formatted('43198110', :format => :international_relative, :spaces => '').should == '0043198110'@
59
+ @Phony.format('41443643532').assert == '+41 44 364 35 32'@
113
60
 
114
- @Phony.formatted('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'@
61
+ h3(#plausibility). Phony.plausible?(number, options = {})
115
62
 
116
- h4(#special). With special spaces
63
+ Is a number plausible?
117
64
 
118
- @Phony.formatted('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'@
65
+ "Phony.plausible? docs":./qed/plausibility.md
119
66
 
120
- @Phony.formatted('43198110', :format => :international, :spaces => :-).should == '+43-1-98110'@
67
+ @Phony.assert.plausible?('+41 44 111 22 33')@
121
68
 
122
- @Phony.formatted('43198110', :format => :international_absolute, :spaces => :-).should == '+43-1-98110'@
69
+ h3(#splitting). Phony.split(number)
123
70
 
124
- @Phony.formatted('33142278186', :format => :+, :spaces => :-).should == '+33-1-42-27-81-86'@
71
+ Split a number into its parts: CC, NDC, local.
125
72
 
126
- @Phony.formatted('43198110', :format => :international_relative, :spaces => :-).should == '0043-1-98110'@
73
+ "Phony.split docs":./qed/split.md
127
74
 
128
- @Phony.formatted('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'@
75
+ @Phony.split('3928061371').assert == ['39', '2', '806', '1371']@
129
76
 
130
- h4(#national). National
77
+ NB If a country does not have an NDC, @#split@ will return @false@ in the NDC position, for example for Denmark:
131
78
 
132
- @Phony.formatted('41443643532', :format => :national).should == '044 364 35 32'@
79
+ @Phony.split('4512121212').assert == ['45', false, '12', '12', '12', '12']@
133
80
 
134
- @Phony.formatted('41800112233', :format => :national).should == '0800 11 22 33'@
81
+ h3(#loading). Loading only a country subset (Phony 2.18.0+).
135
82
 
136
- @Phony.formatted('43198110', :format => :national).should == '01 98110'@
83
+ Use this in case you'd like to save "memory":#memory that is used by Phony's CC rules.
137
84
 
138
- h4(#local). Local
85
+ "Phony::Config.load docs":./qed/config.md
139
86
 
140
- @Phony.formatted('41443643532', :format => :local).should == '364 35 32'@
87
+ First, @require 'phony/config'@.
88
+ Then, one of the following, which will load the rest of Phony.
141
89
 
142
- @Phony.formatted('493038625454', :format => :local).should == '386 25454'@
90
+ Load only these CCs:
91
+ @Phony::Config.load(only: ['41', '44'])@
143
92
 
144
- h3(#splitting). Splitting
93
+ Loads everything except these CCs:
94
+ @Phony::Config.load(except: ['41', '44'])@
145
95
 
146
- @Phony.split('43198110').should == ['43', '1', '98110']@
96
+ Convenience form of @only@:
97
+ @Phony::Config.load('41', '44')@
147
98
 
148
- @Phony.split('33112345678').should == ['33', '1', '12','34','56','78']@
99
+ Each of these loads the rest of Phony.
149
100
 
150
- @Phony.split('4976112345').should == ['49', '761', '123', '45']@
101
+ Memory usage can be checked using (look for @Total retained@):
102
+ @ruby -e 'require "memory_profiler"; MemoryProfiler.report(allow_files: "phony"){ require "phony/config"; Phony::Config.load("1") }.pretty_print'@
103
+ For example, when just loading the NANP CC, the retained memory usage is ~63kB.
151
104
 
152
- @Phony.split('3928061371').should == ['39', '2', '806', '1371']@
105
+ h2. List of Handled Countries
153
106
 
154
- @Phony.split('41443643532').should == ['41', '44', '364', '35', '32']@
107
+ Mildly unmaintained list: Abhas, Afghan, Algerian, Argentina, Austrian, Australian, Azerbaijani, Belgian, Brazilian, Cambodian, Chilean, Chinese, Croatian, Cuban, Cypriot, Czech, Danish, Dutch, Egyptian, El Salvadorian, Estonian, French, German, Ghanan, Gibraltar, Greek, Haiti, Hong Kong, Hungarian, Indian, Iran, Irish, Israel, Italian, Japanese, Kazakh, Liberian, Lithuanian, Luxembourgian, Malaysian, Malta, Mexican, Monaco, Morocco, New Zealand, Nigerian, Norwegian, Peruvian, Polish, Romanian, Russian, Rwandan, Seychelles, Singapore, Slovakian, South African, South Korean, South Osetian, Spanish, Sri Lankan, Sudan, Swedish, Swiss, Thailand, Tunisian, Turkish, Liechtenstein, UK, US, Venezuelan, Vietnamese, and Zambian numbers.
155
108
 
156
- @Phony.split('15551115511').should == ['1', '555', '111', '5511']@
109
+ h2. Proud Sponsors
157
110
 
158
- @Phony.split('6491234567').should == ['64', '9', '123', '4567']@
111
+ * Renuo AG (July 22 –): "Github":https://github.com/renuo, "Homepage":https://www.renuo.ch
159
112
 
160
- @Phony.split('41800334455').should == ['41', '800', '33', '44', '55']@
113
+ h2. License
161
114
 
162
- Note: There is also a ! version of each of these methods which
163
- will destroy the original string and return a new (or old) one.
164
- Just work only with the returned value, and you will be fine.
115
+ MIT.
116
+ See "LICENSE":./LICENSE file.
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phony
4
+
5
+ # Add config.
6
+ class << self
7
+ attr_writer :config
8
+
9
+ def config
10
+ # Default config includes all CCs.
11
+ @config ||= Config.new([], [])
12
+ end
13
+ end
14
+
15
+ # The Config class is only used to configure Phony and to load specific
16
+ # subsets of CCs.
17
+ #
18
+ class Config
19
+
20
+ attr_reader :included_ccs, :excluded_ccs
21
+
22
+ def initialize included_ccs, excluded_ccs
23
+ @included_ccs = included_ccs || []
24
+ @excluded_ccs = excluded_ccs || []
25
+ end
26
+
27
+ def load? cc
28
+ return false if has_excluded? && excluded_ccs.include?(cc)
29
+
30
+ if has_included?
31
+ # We have to check the included_ccs, otherwise false.
32
+ return true if included_ccs.include?(cc)
33
+
34
+ false
35
+ else
36
+ # It's not in excluded and no included was given.
37
+ true
38
+ end
39
+ end
40
+ def has_included?
41
+ !included_ccs.empty?
42
+ end
43
+ def has_excluded?
44
+ !excluded_ccs.empty?
45
+ end
46
+
47
+ # Use as follows:
48
+ #
49
+ # require 'phony/config'
50
+ #
51
+ # # Load only these:
52
+ # Phony::Config.load(only: ['41', '44'])
53
+ # # or all except these:
54
+ # Phony::Config.load(except: ['41', '44'])
55
+ # # or "only", in short form.
56
+ # Phony::Config.load('41', '44')
57
+ # # or even shorter form:
58
+ # Phony::Config.load(41, 44)
59
+ #
60
+ def self.load *options
61
+ # Extract options.
62
+ last = options.last
63
+ only, except = if last.respond_to?(:to_hash)
64
+ # We have the explicit form.
65
+ [last[:only], last[:except]]
66
+ elsif options.respond_to?(:to_ary)
67
+ # We have the convenience short forms.
68
+ [options, []]
69
+ end
70
+
71
+ # Set defaults.
72
+ only, except = [only || [], except || []]
73
+ # Convert to expected format if possible.
74
+ only, except = [only.map(&:to_s), except.map(&:to_s)]
75
+
76
+ # Check params.
77
+ raise "Params given to Phony::Config.load must be Array-like. The one given was: #{only}" unless only.respond_to?(:to_ary)
78
+ raise "Params given to Phony::Config.load must be Array-like. The one given was: #{except}" unless except.respond_to?(:to_ary)
79
+
80
+ # Configure Phony.
81
+ Phony.config = new(only, except)
82
+
83
+ # Load phony.
84
+ Kernel.load File.expand_path('../../phony.rb', __FILE__)
85
+
86
+ # Return the stored config data.
87
+ Phony.config
88
+ end
89
+
90
+ end
91
+ end