phony 2.17.3 → 2.18.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.textile +27 -3
- data/lib/phony.rb +59 -57
- data/lib/phony/config.rb +89 -0
- data/lib/phony/country_codes.rb +1 -1
- data/lib/phony/dsl.rb +2 -0
- data/spec/functional/config_spec.rb +44 -0
- data/spec/lib/phony/dsl_spec.rb +2 -2
- data/spec/lib/phony/local_splitters/regex_spec.rb +7 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7060b3843f748b526a0242338501d1aabd32e12302a3c995b8dcfe7d7e51083b
|
4
|
+
data.tar.gz: d688a27ee525838a92a6d257809f32bc65129bac8d69624ac637e6e3a8e6042a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2b6963e048c2de9a2d30f9b5a5df655149058345a22d13ec354e2737f4d23b8009f48afcdaf92f4062183dabce3013d82c37a9c1122c52e6c7f40622e70a7f0
|
7
|
+
data.tar.gz: '094d6b09c3aa9b7db89d3eddfc27e5019c6a624df85bf9264eb8dc8aad1a7a99742302743c4c3dfbe49d5898f2157874d9615dfbbfc39d3b6e1917137e7de2bb'
|
data/README.textile
CHANGED
@@ -11,10 +11,10 @@ The (admittedly crazy) goal of this Gem is to be able to normalize/format/split
|
|
11
11
|
|
12
12
|
Used in: "airbnb.com":http://airbnb.com, "socialcam.com":http://socialcam.com, "zendesk.com":http://www.zendesk.com/ (and many, many others).
|
13
13
|
|
14
|
-
h2. Runtime Memory Usage
|
14
|
+
h2(#memory). Runtime Memory Usage
|
15
15
|
|
16
|
-
According to "memory_profiler":https://github.com/SamSaffron/memory_profiler, the Phony gem uses roughly 2 MBs of memory per Ruby process.
|
17
|
-
Usage was generated using: @ruby -e 'require "memory_profiler"; MemoryProfiler.report(allow_files: "phony"){ require "phony" }.pretty_print'@
|
16
|
+
According to "memory_profiler":https://github.com/SamSaffron/memory_profiler, the Phony gem uses roughly 1.2 MBs 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'@
|
18
18
|
|
19
19
|
h2. Description
|
20
20
|
|
@@ -78,6 +78,30 @@ NB If a country does not have an NDC, @#split@ will return @false@ in the NDC po
|
|
78
78
|
|
79
79
|
@Phony.split('4512121212').assert == ['45', false, '12', '12', '12', '12']@
|
80
80
|
|
81
|
+
h3(#loading). Loading only a country subset (Phony 2.18.0+).
|
82
|
+
|
83
|
+
Use this in case you'd like to save "memory":#memory that is used by Phony's CC rules.
|
84
|
+
|
85
|
+
"Phony::Config.load docs":./qed/config.md
|
86
|
+
|
87
|
+
First, @require 'phony/config'@.
|
88
|
+
Then, one of the following, which will load the rest of Phony.
|
89
|
+
|
90
|
+
Load only these CCs:
|
91
|
+
@Phony::Config.load(only: ['41', '44'])@
|
92
|
+
|
93
|
+
Loads everything except these CCs:
|
94
|
+
@Phony::Config.load(except: ['41', '44'])@
|
95
|
+
|
96
|
+
Convenience form of @only@:
|
97
|
+
@Phony::Config.load('41', '44')@
|
98
|
+
|
99
|
+
Each of these loads the rest of Phony.
|
100
|
+
|
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.
|
104
|
+
|
81
105
|
h2. List of Handled Countries
|
82
106
|
|
83
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, 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.
|
data/lib/phony.rb
CHANGED
@@ -1,70 +1,72 @@
|
|
1
|
+
# NOTE We use Kernel.load here, as it's possible to redefine Phony via Phony::Config.
|
2
|
+
|
1
3
|
# Framework.
|
2
4
|
#
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
5
|
+
load File.expand_path '../phony/config.rb', __FILE__
|
6
|
+
load File.expand_path '../phony/vanity.rb', __FILE__
|
7
|
+
load File.expand_path '../phony/local_splitters/fixed.rb', __FILE__
|
8
|
+
load File.expand_path '../phony/local_splitters/regex.rb', __FILE__
|
9
|
+
load File.expand_path '../phony/national_splitters/dsl.rb', __FILE__
|
10
|
+
load File.expand_path '../phony/national_splitters/fixed.rb', __FILE__
|
11
|
+
load File.expand_path '../phony/national_splitters/variable.rb', __FILE__
|
12
|
+
load File.expand_path '../phony/national_splitters/regex.rb', __FILE__
|
13
|
+
load File.expand_path '../phony/national_splitters/default.rb', __FILE__
|
14
|
+
load File.expand_path '../phony/national_splitters/none.rb', __FILE__
|
15
|
+
load File.expand_path '../phony/national_code.rb', __FILE__
|
16
|
+
load File.expand_path '../phony/country.rb', __FILE__
|
17
|
+
load File.expand_path '../phony/trunk_code.rb', __FILE__
|
18
|
+
load File.expand_path '../phony/country_codes.rb', __FILE__
|
19
|
+
load File.expand_path '../phony/dsl.rb', __FILE__
|
18
20
|
|
19
21
|
# Countries.
|
20
22
|
#
|
21
23
|
# The ones that need more space to define.
|
22
24
|
#
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
25
|
+
load File.expand_path '../phony/countries/austria.rb', __FILE__
|
26
|
+
load File.expand_path '../phony/countries/bangladesh.rb', __FILE__
|
27
|
+
load File.expand_path '../phony/countries/belarus.rb', __FILE__
|
28
|
+
load File.expand_path '../phony/countries/brazil.rb', __FILE__
|
29
|
+
load File.expand_path '../phony/countries/cambodia.rb', __FILE__
|
30
|
+
load File.expand_path '../phony/countries/croatia.rb', __FILE__
|
31
|
+
load File.expand_path '../phony/countries/china.rb', __FILE__
|
32
|
+
load File.expand_path '../phony/countries/georgia.rb', __FILE__
|
33
|
+
load File.expand_path '../phony/countries/germany.rb', __FILE__
|
34
|
+
load File.expand_path '../phony/countries/guinea.rb', __FILE__
|
35
|
+
load File.expand_path '../phony/countries/india.rb', __FILE__
|
36
|
+
load File.expand_path '../phony/countries/indonesia.rb', __FILE__
|
37
|
+
load File.expand_path '../phony/countries/ireland.rb', __FILE__
|
38
|
+
load File.expand_path '../phony/countries/italy.rb', __FILE__
|
39
|
+
load File.expand_path '../phony/countries/japan.rb', __FILE__
|
40
|
+
load File.expand_path '../phony/countries/kyrgyzstan.rb', __FILE__
|
41
|
+
load File.expand_path '../phony/countries/latvia.rb', __FILE__
|
42
|
+
load File.expand_path '../phony/countries/libya.rb', __FILE__
|
43
|
+
load File.expand_path '../phony/countries/malaysia.rb', __FILE__
|
44
|
+
load File.expand_path '../phony/countries/moldova.rb', __FILE__
|
45
|
+
load File.expand_path '../phony/countries/montenegro.rb', __FILE__
|
46
|
+
load File.expand_path '../phony/countries/myanmar.rb', __FILE__
|
47
|
+
load File.expand_path '../phony/countries/namibia.rb', __FILE__
|
48
|
+
load File.expand_path '../phony/countries/nepal.rb', __FILE__
|
49
|
+
load File.expand_path '../phony/countries/netherlands.rb', __FILE__
|
50
|
+
load File.expand_path '../phony/countries/pakistan.rb', __FILE__
|
51
|
+
load File.expand_path '../phony/countries/paraguay.rb', __FILE__
|
52
|
+
load File.expand_path '../phony/countries/russia_kazakhstan_abkhasia_south_ossetia.rb', __FILE__
|
53
|
+
load File.expand_path '../phony/countries/saudi_arabia.rb', __FILE__
|
54
|
+
load File.expand_path '../phony/countries/serbia.rb', __FILE__
|
55
|
+
load File.expand_path '../phony/countries/somalia.rb', __FILE__
|
56
|
+
load File.expand_path '../phony/countries/south_korea.rb', __FILE__
|
57
|
+
load File.expand_path '../phony/countries/sweden.rb', __FILE__
|
58
|
+
load File.expand_path '../phony/countries/taiwan.rb', __FILE__
|
59
|
+
load File.expand_path '../phony/countries/tajikistan.rb', __FILE__
|
60
|
+
load File.expand_path '../phony/countries/turkmenistan.rb', __FILE__
|
61
|
+
load File.expand_path '../phony/countries/vietnam.rb', __FILE__
|
62
|
+
load File.expand_path '../phony/countries/ukraine.rb', __FILE__
|
63
|
+
load File.expand_path '../phony/countries/united_kingdom.rb', __FILE__
|
64
|
+
load File.expand_path '../phony/countries/uruguay.rb', __FILE__
|
65
|
+
load File.expand_path '../phony/countries/zimbabwe.rb', __FILE__
|
64
66
|
|
65
67
|
# All other countries.
|
66
68
|
#
|
67
|
-
|
69
|
+
load File.expand_path '../phony/countries.rb', __FILE__
|
68
70
|
|
69
71
|
# Phony is the main module and is generally used to process
|
70
72
|
# E164 phone numbers directly.
|
data/lib/phony/config.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
module Phony
|
2
|
+
|
3
|
+
# Add config.
|
4
|
+
class << self
|
5
|
+
attr_writer :config
|
6
|
+
|
7
|
+
def config
|
8
|
+
# Default config includes all CCs.
|
9
|
+
@config ||= Config.new([], [])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# The Config class is only used to configure Phony and to load specific
|
14
|
+
# subsets of CCs.
|
15
|
+
#
|
16
|
+
class Config
|
17
|
+
|
18
|
+
attr_reader :included_ccs, :excluded_ccs
|
19
|
+
|
20
|
+
def initialize included_ccs, excluded_ccs
|
21
|
+
@included_ccs = included_ccs || []
|
22
|
+
@excluded_ccs = excluded_ccs || []
|
23
|
+
end
|
24
|
+
|
25
|
+
def load? cc
|
26
|
+
return false if has_excluded? && excluded_ccs.include?(cc)
|
27
|
+
|
28
|
+
if has_included?
|
29
|
+
# We have to check the included_ccs, otherwise false.
|
30
|
+
return true if included_ccs.include?(cc)
|
31
|
+
|
32
|
+
false
|
33
|
+
else
|
34
|
+
# It's not in excluded and no included was given.
|
35
|
+
true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
def has_included?
|
39
|
+
!included_ccs.empty?
|
40
|
+
end
|
41
|
+
def has_excluded?
|
42
|
+
!excluded_ccs.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
# Use as follows:
|
46
|
+
#
|
47
|
+
# require 'phony/config'
|
48
|
+
#
|
49
|
+
# # Load only these:
|
50
|
+
# Phony::Config.load(only: ['41', '44'])
|
51
|
+
# # or all except these:
|
52
|
+
# Phony::Config.load(except: ['41', '44'])
|
53
|
+
# # or "only", in short form.
|
54
|
+
# Phony::Config.load('41', '44')
|
55
|
+
# # or even shorter form:
|
56
|
+
# Phony::Config.load(41, 44)
|
57
|
+
#
|
58
|
+
def self.load *options
|
59
|
+
# Extract options.
|
60
|
+
last = options.last
|
61
|
+
only, except = if last.respond_to?(:to_hash)
|
62
|
+
# We have the explicit form.
|
63
|
+
[last[:only], last[:except]]
|
64
|
+
elsif options.respond_to?(:to_ary)
|
65
|
+
# We have the convenience short forms.
|
66
|
+
[options, []]
|
67
|
+
end
|
68
|
+
|
69
|
+
# Set defaults.
|
70
|
+
only, except = [only || [], except || []]
|
71
|
+
# Convert to expected format if possible.
|
72
|
+
only, except = [only.map(&:to_s), except.map(&:to_s)]
|
73
|
+
|
74
|
+
# Check params.
|
75
|
+
raise "Params given to Phony::Config.load must be Array-like. The one given was: #{only}" unless only.respond_to?(:to_ary)
|
76
|
+
raise "Params given to Phony::Config.load must be Array-like. The one given was: #{except}" unless except.respond_to?(:to_ary)
|
77
|
+
|
78
|
+
# Configure Phony.
|
79
|
+
Phony.config = new(only, except)
|
80
|
+
|
81
|
+
# Load phony.
|
82
|
+
Kernel.load File.expand_path('../../phony.rb', __FILE__)
|
83
|
+
|
84
|
+
# Return the stored config data.
|
85
|
+
Phony.config
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
data/lib/phony/country_codes.rb
CHANGED
data/lib/phony/dsl.rb
CHANGED
@@ -41,6 +41,8 @@ module Phony
|
|
41
41
|
# country '27', # CC, followed by rules, for example fixed(2) >> ...
|
42
42
|
#
|
43
43
|
def country country_code, definition, options = {}
|
44
|
+
return unless Phony.config.load?(country_code)
|
45
|
+
|
44
46
|
definition.with country_code, options
|
45
47
|
Phony::CountryCodes.instance.add country_code, definition
|
46
48
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
describe 'Phony::Config' do
|
4
|
+
describe 'load' do
|
5
|
+
before do
|
6
|
+
# NOTE We redefine Phony as if it was not loaded for this set of tests.
|
7
|
+
Object.__send__(:remove_const, :Phony) if Object.constants.include?(:Phony)
|
8
|
+
|
9
|
+
load 'phony/config.rb'
|
10
|
+
end
|
11
|
+
after(:all) do
|
12
|
+
# After running this suite, we load all of Phony for the following tests.
|
13
|
+
load 'phony/config.rb'
|
14
|
+
|
15
|
+
Phony::Config.load
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'does not fail when loading all' do
|
19
|
+
Phony::Config.load
|
20
|
+
|
21
|
+
Phony.split('15551115511').should == ['1', '555', '111', '5511']
|
22
|
+
end
|
23
|
+
it 'raises when a CC is used that has not been loaded.' do
|
24
|
+
Phony::Config.load('41')
|
25
|
+
|
26
|
+
expect { Phony.split('15551115511') }.to raise_error
|
27
|
+
end
|
28
|
+
it 'raises when a CC is used that has not been loaded.' do
|
29
|
+
Phony::Config.load(only: ['41'])
|
30
|
+
|
31
|
+
expect { Phony.split('15551115511') }.to raise_error
|
32
|
+
end
|
33
|
+
it 'raises when a CC is used that has not been loaded.' do
|
34
|
+
Phony::Config.load(except: ['1'])
|
35
|
+
|
36
|
+
expect { Phony.split('15551115511') }.to raise_error
|
37
|
+
end
|
38
|
+
it 'does not raise when a CC is used that has been loaded.' do
|
39
|
+
Phony::Config.load(except: ['41'])
|
40
|
+
|
41
|
+
Phony.split('15551115511').should == ['1', '555', '111', '5511']
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/lib/phony/dsl_spec.rb
CHANGED
@@ -16,8 +16,8 @@ describe Phony::DSL do
|
|
16
16
|
it 'checks for ( in regex' do
|
17
17
|
expect { dsl.match(/123/) }.to raise_error("Regexp /123/ needs a group in it that defines which digits belong to the NDC.")
|
18
18
|
end
|
19
|
-
it '
|
20
|
-
dsl.match(/(123)/).should
|
19
|
+
it 'should return a Phony::NationalSplitters::Regex' do
|
20
|
+
dsl.match(/(123)/).class.name.should == Phony::NationalSplitters::Regex.name
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -1,7 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
main = self
|
4
|
+
|
3
5
|
describe Phony::LocalSplitters::Regex do
|
4
6
|
|
7
|
+
before do
|
8
|
+
load 'spec_helper_extensions.rb'
|
9
|
+
main.send :include, SpecHelperExtensions
|
10
|
+
end
|
11
|
+
|
5
12
|
describe 'instance_for' do
|
6
13
|
it 'does not cache' do
|
7
14
|
described_class.instance_for({}).should_not equal(described_class.instance_for({}))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phony
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.18.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Hanke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Fast international phone number (E164 standard) normalizing, splitting
|
14
14
|
and formatting. Lots of formatting options: International (+.., 00..), national
|
@@ -21,6 +21,7 @@ extra_rdoc_files:
|
|
21
21
|
files:
|
22
22
|
- README.textile
|
23
23
|
- lib/phony.rb
|
24
|
+
- lib/phony/config.rb
|
24
25
|
- lib/phony/countries.rb
|
25
26
|
- lib/phony/countries/austria.rb
|
26
27
|
- lib/phony/countries/bangladesh.rb
|
@@ -77,6 +78,7 @@ files:
|
|
77
78
|
- lib/phony/national_splitters/variable.rb
|
78
79
|
- lib/phony/trunk_code.rb
|
79
80
|
- lib/phony/vanity.rb
|
81
|
+
- spec/functional/config_spec.rb
|
80
82
|
- spec/functional/plausibility_spec.rb
|
81
83
|
- spec/lib/phony/countries_spec.rb
|
82
84
|
- spec/lib/phony/country_codes_spec.rb
|
@@ -132,4 +134,5 @@ test_files:
|
|
132
134
|
- spec/lib/phony/local_splitters/regex_spec.rb
|
133
135
|
- spec/lib/phony/local_splitters/fixed_spec.rb
|
134
136
|
- spec/lib/phony/vanity_spec.rb
|
137
|
+
- spec/functional/config_spec.rb
|
135
138
|
- spec/functional/plausibility_spec.rb
|