phonelib 0.6.46 → 0.6.51

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f5702cfd1fd7ef6eecf8c589701a8be3879ffc29147df0915f05c40199ee43d
4
- data.tar.gz: 694c0a043d7d2681652dd4a9e0ea3a668ccd35e300813acc8cfac2d6980caa3a
3
+ metadata.gz: 461ed2f679d7bae69dac1e7b1a34881d2c707402b9e9ee58af9bb2c66efc0976
4
+ data.tar.gz: c8f28d5c23bede4ef25c902437b1ee5d2f15b92cf193b5f5c82948ca70cca1c0
5
5
  SHA512:
6
- metadata.gz: 37ea9d924c98e51c2879a395a7e56050ee63060ace543a60c69a6df7d4b92054af945532ec0b559bcbc298eca4597eb471d845a853caaae1538928e25b929b83
7
- data.tar.gz: 69df60d5eeecc7747cca844b12adcac43d03396d676749d49ee5b381c8fc4f7b2b44543ad5605cf2772302820c087e37482a4dc43c826a925f4135772649d5ec
6
+ metadata.gz: fd41588b26a3fca412c750b1747fddb5f06aa433909a7557276f1f73f1a676c0452e143d4b27c99e74c0f5649c876a7d7f86dd869c7f61c53d085d36672e2899
7
+ data.tar.gz: ce69360d6fb7a926f29f48751f8fe4fbdc51ec84aea060e96163da5a8c0cf5fd207d5b1a66810c79971f1dde8d66408c49e45a9c3bab332df34301e7dc9e3d49
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Built in integration with JetBrains RubyMine](https://github.com/daddyz/phonelib/blob/master/icon_RubyMine.png?raw=true)](https://www.jetbrains.com/ruby/)
4
4
  [![Gem Version](https://badge.fury.io/rb/phonelib.svg)](http://badge.fury.io/rb/phonelib)
5
- [![Build Status](https://travis-ci.org/daddyz/phonelib.png?branch=master)](http://travis-ci.org/daddyz/phonelib)
5
+ [![Build Status](https://travis-ci.com/daddyz/phonelib.svg?branch=master)](http://travis-ci.com/daddyz/phonelib)
6
6
  [![](https://codeclimate.com/github/daddyz/phonelib/badges/coverage.svg)](https://codeclimate.com/github/daddyz/phonelib/coverage)
7
7
  [![](https://codeclimate.com/github/daddyz/phonelib/badges/gpa.svg)](https://codeclimate.com/github/daddyz/phonelib)
8
8
  [![Inline docs](http://inch-ci.org/github/daddyz/phonelib.svg?branch=master)](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
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
- autoload :PhoneValidator, 'validators/phone_validator'
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://download.geonames.org/export/dump/countryInfo.txt')
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
@@ -51,7 +51,7 @@ module Phonelib
51
51
  def sanitized
52
52
  @sanitized ||=
53
53
  vanity_converted(@original).gsub(
54
- Phonelib.strict_check ? cr('^\+') : cr('[^0-9]+'),
54
+ Phonelib.strict_check ? cr('^\+') : cr(Phonelib.sanitize_regex),
55
55
  '')
56
56
  end
57
57
 
@@ -58,7 +58,7 @@ module Phonelib
58
58
 
59
59
  # @private returns extended data ids for current number
60
60
  def ext_data
61
- return @ext_data if @ext_data
61
+ return @ext_data if defined?(@ext_data) && @ext_data
62
62
 
63
63
  result = default_ext_data
64
64
  return result unless possible?
@@ -1,4 +1,4 @@
1
1
  module Phonelib
2
2
  # @private
3
- VERSION = '0.6.46'
3
+ VERSION = '0.6.51'
4
4
  end
@@ -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.46
4
+ version: 0.6.51
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2021-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -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.6
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