phony 1.9.0 → 2.19.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.textile +64 -112
- data/lib/phony/config.rb +91 -0
- data/lib/phony/countries/argentina.rb +355 -0
- data/lib/phony/countries/austria.rb +56 -22
- data/lib/phony/countries/bangladesh.rb +57 -0
- data/lib/phony/countries/belarus.rb +133 -0
- data/lib/phony/countries/brazil.rb +101 -95
- data/lib/phony/countries/cambodia.rb +131 -0
- data/lib/phony/countries/china.rb +13 -6
- data/lib/phony/countries/croatia.rb +23 -0
- data/lib/phony/countries/georgia.rb +94 -0
- data/lib/phony/countries/germany.rb +66 -42
- data/lib/phony/countries/guinea.rb +46 -0
- data/lib/phony/countries/india.rb +50 -0
- data/lib/phony/countries/indonesia.rb +55 -0
- data/lib/phony/countries/ireland.rb +35 -28
- data/lib/phony/countries/italy.rb +272 -166
- data/lib/phony/countries/japan.rb +468 -0
- data/lib/phony/countries/kyrgyzstan.rb +120 -0
- data/lib/phony/countries/latvia.rb +43 -0
- data/lib/phony/countries/libya.rb +116 -0
- data/lib/phony/countries/malaysia.rb +31 -7
- data/lib/phony/countries/moldova.rb +53 -0
- data/lib/phony/countries/montenegro.rb +30 -0
- data/lib/phony/countries/myanmar.rb +55 -0
- data/lib/phony/countries/namibia.rb +37 -0
- data/lib/phony/countries/nepal.rb +73 -0
- data/lib/phony/countries/netherlands.rb +17 -5
- data/lib/phony/countries/pakistan.rb +121 -0
- data/lib/phony/countries/paraguay.rb +147 -0
- data/lib/phony/countries/{russia_kazakhstan_abhasia_south_osetia.rb → russia_kazakhstan_abkhasia_south_ossetia.rb} +35 -24
- data/lib/phony/countries/saudi_arabia.rb +40 -0
- data/lib/phony/countries/serbia.rb +47 -0
- data/lib/phony/countries/somalia.rb +26 -0
- data/lib/phony/countries/south_korea.rb +19 -10
- data/lib/phony/countries/sweden.rb +58 -38
- data/lib/phony/countries/taiwan.rb +28 -0
- data/lib/phony/countries/tajikistan.rb +79 -0
- data/lib/phony/countries/turkmenistan.rb +76 -0
- data/lib/phony/countries/ukraine.rb +630 -0
- data/lib/phony/countries/united_kingdom.rb +639 -44
- data/lib/phony/countries/uruguay.rb +53 -0
- data/lib/phony/countries/vietnam.rb +133 -0
- data/lib/phony/countries/zimbabwe.rb +39 -0
- data/lib/phony/countries.rb +901 -301
- data/lib/phony/country.rb +177 -20
- data/lib/phony/country_codes.rb +119 -101
- data/lib/phony/dsl.rb +113 -68
- data/lib/phony/local_splitters/fixed.rb +25 -1
- data/lib/phony/local_splitters/regex.rb +16 -2
- data/lib/phony/national_code.rb +7 -7
- data/lib/phony/national_splitters/default.rb +35 -3
- data/lib/phony/national_splitters/dsl.rb +12 -7
- data/lib/phony/national_splitters/fixed.rb +7 -1
- data/lib/phony/national_splitters/none.rb +7 -3
- data/lib/phony/national_splitters/regex.rb +6 -0
- data/lib/phony/national_splitters/variable.rb +13 -9
- data/lib/phony/trunk_code.rb +57 -0
- data/lib/phony/vanity.rb +3 -3
- data/lib/phony.rb +239 -55
- data/spec/functional/config_spec.rb +44 -0
- data/spec/functional/plausibility_spec.rb +656 -0
- data/spec/lib/phony/countries_spec.rb +1207 -119
- data/spec/lib/phony/country_codes_spec.rb +99 -81
- data/spec/lib/phony/country_spec.rb +54 -14
- data/spec/lib/phony/dsl_spec.rb +2 -2
- data/spec/lib/phony/local_splitters/fixed_spec.rb +4 -4
- data/spec/lib/phony/local_splitters/regex_spec.rb +50 -2
- data/spec/lib/phony/national_code_spec.rb +34 -34
- data/spec/lib/phony/national_splitters/default_spec.rb +34 -0
- data/spec/lib/phony/national_splitters/fixed_spec.rb +12 -6
- data/spec/lib/phony/national_splitters/none_spec.rb +13 -3
- data/spec/lib/phony/national_splitters/regex_spec.rb +1 -1
- data/spec/lib/phony/national_splitters/variable_spec.rb +11 -5
- data/spec/lib/phony/trunk_code_spec.rb +85 -0
- data/spec/lib/phony/vanity_spec.rb +15 -19
- data/spec/lib/phony_spec.rb +59 -277
- metadata +67 -34
- data/lib/phony/validator.rb +0 -26
- data/lib/phony/validators.rb +0 -88
- 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
|
-
|
8
|
+
Disclaimer: Phony works with international numbers only, such as @61 412 345 678@!
|
6
9
|
|
7
|
-
|
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
|
-
|
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
|
-
|
14
|
+
h2(#memory). Runtime Memory Usage
|
43
15
|
|
44
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
28
|
+
It currently handles the countries listed at the end of this README.
|
83
29
|
|
84
|
-
|
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
|
-
|
33
|
+
h2. Installation
|
87
34
|
|
88
|
-
|
35
|
+
With Rails? Check out: https://github.com/joost/phony_rails.
|
89
36
|
|
90
|
-
@
|
37
|
+
With Bundler: Append @gem 'phony'@ to your @Gemfile@ and @bundle install@ it.
|
91
38
|
|
92
|
-
|
39
|
+
Without Bundler: Run @gem install phony@ from your command line.
|
93
40
|
|
94
|
-
|
41
|
+
h2(#usage). Usage docs
|
95
42
|
|
96
|
-
|
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
|
-
|
45
|
+
h3(#normalizing). Phony.normalize(number)
|
99
46
|
|
100
|
-
|
47
|
+
Normalize intelligently removes all non-numeric characters of a number. Do it before storing a number in a DB.
|
101
48
|
|
102
|
-
|
49
|
+
"Phony.normalize docs":./qed/normalize.md
|
103
50
|
|
104
|
-
@Phony.
|
51
|
+
@Phony.normalize('1-888-407-4747').assert == '18884074747'@
|
105
52
|
|
106
|
-
|
53
|
+
h3(#formatting). Phony.format(number, options = {})
|
107
54
|
|
108
|
-
|
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
|
-
|
57
|
+
"Phony.format docs":./qed/format.md
|
111
58
|
|
112
|
-
@Phony.
|
59
|
+
@Phony.format('41443643532').assert == '+41 44 364 35 32'@
|
113
60
|
|
114
|
-
|
61
|
+
h3(#plausibility). Phony.plausible?(number, options = {})
|
115
62
|
|
116
|
-
|
63
|
+
Is a number plausible?
|
117
64
|
|
118
|
-
|
65
|
+
"Phony.plausible? docs":./qed/plausibility.md
|
119
66
|
|
120
|
-
@Phony.
|
67
|
+
@Phony.assert.plausible?('+41 44 111 22 33')@
|
121
68
|
|
122
|
-
|
69
|
+
h3(#splitting). Phony.split(number)
|
123
70
|
|
124
|
-
|
71
|
+
Split a number into its parts: CC, NDC, local.
|
125
72
|
|
126
|
-
|
73
|
+
"Phony.split docs":./qed/split.md
|
127
74
|
|
128
|
-
@Phony.
|
75
|
+
@Phony.split('3928061371').assert == ['39', '2', '806', '1371']@
|
129
76
|
|
130
|
-
|
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.
|
79
|
+
@Phony.split('4512121212').assert == ['45', false, '12', '12', '12', '12']@
|
133
80
|
|
134
|
-
|
81
|
+
h3(#loading). Loading only a country subset (Phony 2.18.0+).
|
135
82
|
|
136
|
-
|
83
|
+
Use this in case you'd like to save "memory":#memory that is used by Phony's CC rules.
|
137
84
|
|
138
|
-
|
85
|
+
"Phony::Config.load docs":./qed/config.md
|
139
86
|
|
140
|
-
|
87
|
+
First, @require 'phony/config'@.
|
88
|
+
Then, one of the following, which will load the rest of Phony.
|
141
89
|
|
142
|
-
|
90
|
+
Load only these CCs:
|
91
|
+
@Phony::Config.load(only: ['41', '44'])@
|
143
92
|
|
144
|
-
|
93
|
+
Loads everything except these CCs:
|
94
|
+
@Phony::Config.load(except: ['41', '44'])@
|
145
95
|
|
146
|
-
|
96
|
+
Convenience form of @only@:
|
97
|
+
@Phony::Config.load('41', '44')@
|
147
98
|
|
148
|
-
|
99
|
+
Each of these loads the rest of Phony.
|
149
100
|
|
150
|
-
|
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
|
-
|
105
|
+
h2. List of Handled Countries
|
153
106
|
|
154
|
-
|
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
|
-
|
109
|
+
h2. Proud Sponsors
|
157
110
|
|
158
|
-
|
111
|
+
* Renuo AG (July 22 –): "Github":https://github.com/renuo, "Homepage":https://www.renuo.ch
|
159
112
|
|
160
|
-
|
113
|
+
h2. License
|
161
114
|
|
162
|
-
|
163
|
-
|
164
|
-
Just work only with the returned value, and you will be fine.
|
115
|
+
MIT.
|
116
|
+
See "LICENSE":./LICENSE file.
|
data/lib/phony/config.rb
ADDED
@@ -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
|