phonelib 0.6.44 → 0.6.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/Rakefile +7 -0
- data/data/extended_data.dat +0 -0
- data/data/phone_data.dat +0 -0
- data/lib/phonelib.rb +5 -1
- data/lib/phonelib/core.rb +67 -0
- data/lib/phonelib/data_importer.rb +2 -2
- data/lib/phonelib/phone.rb +1 -1
- data/lib/phonelib/phone_extended_data.rb +1 -1
- data/lib/phonelib/phone_formatter.rb +1 -1
- data/lib/phonelib/version.rb +1 -1
- data/lib/validators/phone_validator3.rb +130 -0
- metadata +10 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12f73e3e6226d24c7ab2fdd3deae4726ac707db3ae037181c8ad7b938927bb20
|
|
4
|
+
data.tar.gz: f21fb20997e7c37aa91353397ee4a3a049d9eb77be46da89e6d2e497ac1a5b88
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c09e9804e3ad4ebca358adad87e66a1284714c3ee66fb714f316da6cb70cd6de274bd77028ddf7561c90490db7b266eff9fbf0ef4e68092ed64ee0313e869433
|
|
7
|
+
data.tar.gz: 767db7a6c8a6f17c1101fcf150f2bd216040250adb316b015cf1162739ccedec532e4be40017d3b6ab5597e2e887fe06bc2e3b9f00e38b41b7397d015fcb6936
|
data/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.jetbrains.com/ruby/)
|
|
4
4
|
[](http://badge.fury.io/rb/phonelib)
|
|
5
|
-
[](http://travis-ci.com/daddyz/phonelib)
|
|
6
6
|
[](https://codeclimate.com/github/daddyz/phonelib/coverage)
|
|
7
7
|
[](https://codeclimate.com/github/daddyz/phonelib)
|
|
8
8
|
[](http://inch-ci.org/github/daddyz/phonelib)
|
|
@@ -61,6 +61,12 @@ To disable sanitizing of passed phone number (keeping digits only)
|
|
|
61
61
|
Phonelib.strict_check = true
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
+
To change sanitized symbols on parsed number, so non-specified symbols won't be wiped and will fail the parsing
|
|
65
|
+
|
|
66
|
+
``` ruby
|
|
67
|
+
Phonelib.sanitize_regex = '[\.\-\(\) \;\+]'
|
|
68
|
+
```
|
|
69
|
+
|
|
64
70
|
To disable sanitizing of double prefix on passed phone number
|
|
65
71
|
|
|
66
72
|
```ruby
|
|
@@ -86,6 +92,21 @@ In case you need to overwrite some Google's libphonenumber library data, you nee
|
|
|
86
92
|
Phonelib.override_phone_data = '/path/to/override_phone_data.dat'
|
|
87
93
|
```
|
|
88
94
|
|
|
95
|
+
In case you want to add some custom or still not updated regex patterns for certain type you can use additional regexes feature in a following way:
|
|
96
|
+
|
|
97
|
+
``` ruby
|
|
98
|
+
Phonelib.add_additional_regex :us, Phonelib::Core::MOBILE, '[5]{10}' # this will add number 1-555-555-5555 to be valid
|
|
99
|
+
Phonelib.add_additional_regex :gb, Phonelib::Core::MOBILE, '[1]{5}' # this will add number 44-11-111 to be valid
|
|
100
|
+
# you can also specify all regexes using this method
|
|
101
|
+
Phonelib.additional_regexes = [[:us, :mobile, "[5]{10}"], [:gb, :mobile, "[1]{5}"]]
|
|
102
|
+
# or just use dump method to keep them altogether
|
|
103
|
+
Phonelib.dump_additional_regexes # => [["US", :mobile, "[5]{10}"], ["GB", :mobile, "[1]{5}"]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
(!) For a list of available types refer to this readme.
|
|
107
|
+
|
|
108
|
+
(!) Please note that regex should be added as string
|
|
109
|
+
|
|
89
110
|
In case phone number that was passed for parsing has "+" sign in the beginning, library will try to detect a country regarding the provided one.
|
|
90
111
|
|
|
91
112
|
### ActiveRecord Integration
|
data/Rakefile
CHANGED
|
@@ -22,6 +22,13 @@ end
|
|
|
22
22
|
Bundler::GemHelper.install_tasks
|
|
23
23
|
|
|
24
24
|
require 'rspec/core/rake_task'
|
|
25
|
+
module TempFixForRakeLastComment
|
|
26
|
+
def last_comment
|
|
27
|
+
last_description
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
Rake::Application.send :include, TempFixForRakeLastComment
|
|
31
|
+
|
|
25
32
|
RSpec::Core::RakeTask.new(:spec) do |t|
|
|
26
33
|
if defined? Rails
|
|
27
34
|
puts 'Rails found! Running tests with Rails'
|
data/data/extended_data.dat
CHANGED
|
Binary file
|
data/data/phone_data.dat
CHANGED
|
Binary file
|
data/lib/phonelib.rb
CHANGED
|
@@ -12,5 +12,9 @@ module Phonelib
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
if defined?(ActiveModel) || defined?(Rails)
|
|
15
|
-
|
|
15
|
+
if RUBY_VERSION >= '3.0.0'
|
|
16
|
+
autoload :PhoneValidator, 'validators/phone_validator3'
|
|
17
|
+
else
|
|
18
|
+
autoload :PhoneValidator, 'validators/phone_validator'
|
|
19
|
+
end
|
|
16
20
|
end
|
data/lib/phonelib/core.rb
CHANGED
|
@@ -107,6 +107,22 @@ module Phonelib
|
|
|
107
107
|
@@strict_check = strict
|
|
108
108
|
end
|
|
109
109
|
|
|
110
|
+
# @private sanitizing regex, matching symbols will get removed from parsed number, must be string
|
|
111
|
+
@@sanitize_regex = '[^0-9]+'
|
|
112
|
+
|
|
113
|
+
# getter for sanitize regex
|
|
114
|
+
# @return [String] regex of symbols to wipe from parsed number
|
|
115
|
+
def sanitize_regex
|
|
116
|
+
@@sanitize_regex
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# setter for sanitize regex
|
|
120
|
+
# @param regex [String] symbols to wipe from parsed number
|
|
121
|
+
# @return [String] regex of symbols to wipe from parsed number
|
|
122
|
+
def sanitize_regex=(regex)
|
|
123
|
+
@@sanitize_regex = regex.is_a?(String) ? regex : regex.to_s
|
|
124
|
+
end
|
|
125
|
+
|
|
110
126
|
# @private strict double prefix check for validator, doesn't sanitize number
|
|
111
127
|
@@strict_double_prefix_check = false
|
|
112
128
|
|
|
@@ -133,6 +149,42 @@ module Phonelib
|
|
|
133
149
|
@@override_phone_data
|
|
134
150
|
end
|
|
135
151
|
|
|
152
|
+
@@additional_regexes = {}
|
|
153
|
+
# setter for data file to use
|
|
154
|
+
def additional_regexes=(data)
|
|
155
|
+
return unless data.is_a?(Array)
|
|
156
|
+
@@additional_regexes = {}
|
|
157
|
+
data.each do |row|
|
|
158
|
+
next if row.size != 3
|
|
159
|
+
add_additional_regex(*row)
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def add_additional_regex(country, type, national_regex)
|
|
164
|
+
return unless Phonelib::Core::TYPES_DESC.keys.include?(type.to_sym)
|
|
165
|
+
return unless national_regex.is_a?(String)
|
|
166
|
+
@@phone_data = nil
|
|
167
|
+
@@additional_regexes[country.to_s.upcase] ||= {}
|
|
168
|
+
@@additional_regexes[country.to_s.upcase][type] ||= []
|
|
169
|
+
@@additional_regexes[country.to_s.upcase][type] << national_regex
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def dump_additional_regexes
|
|
173
|
+
rows = []
|
|
174
|
+
@@additional_regexes.each do |country, types|
|
|
175
|
+
types.each do |type, regexes|
|
|
176
|
+
regexes.each do |regex|
|
|
177
|
+
rows << [country, type, regex]
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
rows
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def additional_regexes
|
|
185
|
+
@@additional_regexes
|
|
186
|
+
end
|
|
187
|
+
|
|
136
188
|
@@vanity_conversion = false
|
|
137
189
|
# setter for vanity phone numbers chars replacement
|
|
138
190
|
def vanity_conversion=(value)
|
|
@@ -377,6 +429,21 @@ module Phonelib
|
|
|
377
429
|
override_data_file = Marshal.load(File.binread(override_phone_data))
|
|
378
430
|
default_data.merge!(override_data_file)
|
|
379
431
|
end
|
|
432
|
+
additional_regexes.each do |country, types|
|
|
433
|
+
types.each do |type, regex|
|
|
434
|
+
default_data[country][Core::TYPES][type] ||= {}
|
|
435
|
+
[Core::VALID_PATTERN, Core::POSSIBLE_PATTERN].each do |key|
|
|
436
|
+
if default_data[country][Core::TYPES][type][key]
|
|
437
|
+
default_data[country][Core::TYPES][type][key] << "|#{regex.join('|')}"
|
|
438
|
+
else
|
|
439
|
+
default_data[country][Core::TYPES][type][key] = regex.join('|')
|
|
440
|
+
end
|
|
441
|
+
if type != Core::GENERAL
|
|
442
|
+
default_data[country][Core::TYPES][Core::GENERAL][key] << "|#{regex.join('|')}"
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
end
|
|
380
447
|
default_data
|
|
381
448
|
end
|
|
382
449
|
|
|
@@ -145,10 +145,10 @@ module Phonelib
|
|
|
145
145
|
|
|
146
146
|
require 'open-uri'
|
|
147
147
|
require 'csv'
|
|
148
|
-
io = open('http://
|
|
148
|
+
io = open('http://api.geonames.org/countryInfoCSV?username=demo&style=full')
|
|
149
149
|
csv = CSV.new(io, {col_sep: "\t"})
|
|
150
150
|
csv.each do |row|
|
|
151
|
-
next if row[0].start_with?('#') || row[0].empty?
|
|
151
|
+
next if row[0].nil? || row[0].start_with?('#') || row[0].empty?
|
|
152
152
|
|
|
153
153
|
@countries[row[0]] = row[4]
|
|
154
154
|
end
|
data/lib/phonelib/phone.rb
CHANGED
|
@@ -44,7 +44,7 @@ module Phonelib
|
|
|
44
44
|
prefix = formatted if formatted.is_a?(String)
|
|
45
45
|
return nil if sanitized.empty?
|
|
46
46
|
return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless valid?
|
|
47
|
-
return country_code
|
|
47
|
+
return "#{prefix}#{country_code}#{@national_number}" unless formatted
|
|
48
48
|
|
|
49
49
|
fmt = @data[country][:format]
|
|
50
50
|
national = @national_number
|
data/lib/phonelib/version.rb
CHANGED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Validator class for phone validations
|
|
2
|
+
#
|
|
3
|
+
# ==== Examples
|
|
4
|
+
#
|
|
5
|
+
# Validates that attribute is a valid phone number.
|
|
6
|
+
# If empty value passed for attribute it fails.
|
|
7
|
+
#
|
|
8
|
+
# class Phone < ActiveRecord::Base
|
|
9
|
+
# attr_accessible :number
|
|
10
|
+
# validates :number, phone: true
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# Validates that attribute is a possible phone number.
|
|
14
|
+
# If empty value passed for attribute it fails.
|
|
15
|
+
#
|
|
16
|
+
# class Phone < ActiveRecord::Base
|
|
17
|
+
# attr_accessible :number
|
|
18
|
+
# validates :number, phone: { possible: true }
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# Validates that attribute is a valid phone number.
|
|
22
|
+
# Empty value is allowed to be passed.
|
|
23
|
+
#
|
|
24
|
+
# class Phone < ActiveRecord::Base
|
|
25
|
+
# attr_accessible :number
|
|
26
|
+
# validates :number, phone: { allow_blank: true }
|
|
27
|
+
# end
|
|
28
|
+
#
|
|
29
|
+
# Validates that attribute is a valid phone number of specified type(s).
|
|
30
|
+
# It is also possible to check that attribute is a possible number of specified
|
|
31
|
+
# type(s). Symbol or array accepted.
|
|
32
|
+
#
|
|
33
|
+
# class Phone < ActiveRecord::Base
|
|
34
|
+
# attr_accessible :number, :mobile
|
|
35
|
+
# validates :number, phone: { types: [:mobile, :fixed], allow_blank: true }
|
|
36
|
+
# validates :mobile, phone: { possible: true, types: :mobile }
|
|
37
|
+
# end
|
|
38
|
+
#
|
|
39
|
+
# validates that phone is valid and it is from specified country or countries
|
|
40
|
+
#
|
|
41
|
+
# class Phone < ActiveRecord::Base
|
|
42
|
+
# attr_accessible :number
|
|
43
|
+
# validates :number, phone: { countries: [:us, :ca] }
|
|
44
|
+
# end
|
|
45
|
+
#
|
|
46
|
+
# Validates that attribute does not include an extension.
|
|
47
|
+
# The default setting is to allow extensions
|
|
48
|
+
#
|
|
49
|
+
# class Phone < ActiveRecord::Base
|
|
50
|
+
# attr_accessible :number
|
|
51
|
+
# validates :number, phone: { extensions: false }
|
|
52
|
+
# end
|
|
53
|
+
#
|
|
54
|
+
class PhoneValidator < ActiveModel::EachValidator
|
|
55
|
+
# Include all core methods
|
|
56
|
+
include Phonelib::Core
|
|
57
|
+
|
|
58
|
+
# Validation method
|
|
59
|
+
def validate_each(record, attribute, value)
|
|
60
|
+
return if options[:allow_blank] && value.blank?
|
|
61
|
+
|
|
62
|
+
@phone = parse(value, specified_country(record))
|
|
63
|
+
valid = phone_valid? && valid_types? && valid_country? && valid_extensions?
|
|
64
|
+
|
|
65
|
+
record.errors.add(attribute, message, **options) unless valid
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def message
|
|
71
|
+
options[:message] || :invalid
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def phone_valid?
|
|
75
|
+
@phone.send(options[:possible] ? :possible? : :valid?)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def valid_types?
|
|
79
|
+
return true unless options[:types]
|
|
80
|
+
(phone_types & types).size > 0
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def valid_country?
|
|
84
|
+
return true unless options[:countries]
|
|
85
|
+
(phone_countries & countries).size > 0
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def valid_extensions?
|
|
89
|
+
return true if !options.has_key?(:extensions) || options[:extensions]
|
|
90
|
+
@phone.extension.empty?
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def specified_country(record)
|
|
94
|
+
return unless options[:country_specifier]
|
|
95
|
+
|
|
96
|
+
if options[:country_specifier].is_a?(Symbol)
|
|
97
|
+
record.send(options[:country_specifier])
|
|
98
|
+
else
|
|
99
|
+
options[:country_specifier].call(record)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# @private
|
|
104
|
+
def phone_types
|
|
105
|
+
method = options[:possible] ? :possible_types : :types
|
|
106
|
+
phone_types = @phone.send(method)
|
|
107
|
+
if (phone_types & [Phonelib::Core::FIXED_OR_MOBILE]).size > 0
|
|
108
|
+
phone_types += [Phonelib::Core::FIXED_LINE, Phonelib::Core::MOBILE]
|
|
109
|
+
end
|
|
110
|
+
phone_types
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# @private
|
|
114
|
+
def phone_countries
|
|
115
|
+
method = options[:possible] ? :countries : :valid_countries
|
|
116
|
+
@phone.send(method)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# @private
|
|
120
|
+
def types
|
|
121
|
+
types = options[:types].is_a?(Array) ? options[:types] : [options[:types]]
|
|
122
|
+
types.map(&:to_sym)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# @private
|
|
126
|
+
def countries
|
|
127
|
+
countries = options[:countries].is_a?(Array) ? options[:countries] : [options[:countries]]
|
|
128
|
+
countries.map { |c| c.to_s.upcase }
|
|
129
|
+
end
|
|
130
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: phonelib
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.49
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vadim Senderovich
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-03-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -16,28 +16,28 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - "<"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '14.0'
|
|
20
20
|
type: :development
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - "<"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
26
|
+
version: '14.0'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: nokogiri
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - "~>"
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 1.10.
|
|
33
|
+
version: 1.10.8
|
|
34
34
|
type: :development
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 1.10.
|
|
40
|
+
version: 1.10.8
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: pry
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -114,14 +114,14 @@ dependencies:
|
|
|
114
114
|
requirements:
|
|
115
115
|
- - '='
|
|
116
116
|
- !ruby/object:Gem::Version
|
|
117
|
-
version:
|
|
117
|
+
version: 2.3.1
|
|
118
118
|
type: :development
|
|
119
119
|
prerelease: false
|
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
|
121
121
|
requirements:
|
|
122
122
|
- - '='
|
|
123
123
|
- !ruby/object:Gem::Version
|
|
124
|
-
version:
|
|
124
|
+
version: 2.3.1
|
|
125
125
|
description: |2
|
|
126
126
|
Google libphonenumber library was taken as a basis for
|
|
127
127
|
this gem. Gem uses its data file for validations and number formatting.
|
|
@@ -148,6 +148,7 @@ files:
|
|
|
148
148
|
- lib/phonelib/version.rb
|
|
149
149
|
- lib/tasks/phonelib_tasks.rake
|
|
150
150
|
- lib/validators/phone_validator.rb
|
|
151
|
+
- lib/validators/phone_validator3.rb
|
|
151
152
|
homepage: https://github.com/daddyz/phonelib
|
|
152
153
|
licenses:
|
|
153
154
|
- MIT
|
|
@@ -168,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
168
169
|
- !ruby/object:Gem::Version
|
|
169
170
|
version: '0'
|
|
170
171
|
requirements: []
|
|
171
|
-
rubygems_version: 3.0.
|
|
172
|
+
rubygems_version: 3.0.8
|
|
172
173
|
signing_key:
|
|
173
174
|
specification_version: 4
|
|
174
175
|
summary: Gem validates phone numbers with Google libphonenumber database
|