phonelib 0.6.48 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ab20ada741b6c807d528b1cd032b4e20ebfbde1bcc8fcd6eeeb7b351ddec3ed
4
- data.tar.gz: cc73a6a56939c0ba09f8996fe17a6050db96af0c5e52b6a7b46c8c55bbf9869d
3
+ metadata.gz: 12f73e3e6226d24c7ab2fdd3deae4726ac707db3ae037181c8ad7b938927bb20
4
+ data.tar.gz: f21fb20997e7c37aa91353397ee4a3a049d9eb77be46da89e6d2e497ac1a5b88
5
5
  SHA512:
6
- metadata.gz: 54375aea5dd1cf1ddd0d3c51d7ac9a6db4b745e152ef5d1a11c45571ef7f4bfb579e1021a1a9c13dc83ba77e39cca12e19904aba71f632456d15ec1d8e9ec97a
7
- data.tar.gz: 85495f7efe0e2f9da9adbbe9b4b4f2e8219fc5f6842ae473a81626c9f8629557351ff025a98794f95ee79c1d661bf4f39ff5c3f4191db36d21d2c14849252d9c
6
+ metadata.gz: c09e9804e3ad4ebca358adad87e66a1284714c3ee66fb714f316da6cb70cd6de274bd77028ddf7561c90490db7b266eff9fbf0ef4e68092ed64ee0313e869433
7
+ data.tar.gz: 767db7a6c8a6f17c1101fcf150f2bd216040250adb316b015cf1162739ccedec532e4be40017d3b6ab5597e2e887fe06bc2e3b9f00e38b41b7397d015fcb6936
data/README.md CHANGED
@@ -92,6 +92,21 @@ In case you need to overwrite some Google's libphonenumber library data, you nee
92
92
  Phonelib.override_phone_data = '/path/to/override_phone_data.dat'
93
93
  ```
94
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
+
95
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.
96
111
 
97
112
  ### ActiveRecord Integration
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -149,6 +149,42 @@ module Phonelib
149
149
  @@override_phone_data
150
150
  end
151
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
+
152
188
  @@vanity_conversion = false
153
189
  # setter for vanity phone numbers chars replacement
154
190
  def vanity_conversion=(value)
@@ -393,6 +429,21 @@ module Phonelib
393
429
  override_data_file = Marshal.load(File.binread(override_phone_data))
394
430
  default_data.merge!(override_data_file)
395
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
396
447
  default_data
397
448
  end
398
449
 
@@ -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.48'
3
+ VERSION = '0.6.49'
4
4
  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.48
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: 2021-01-31 00:00:00.000000000 Z
11
+ date: 2021-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  - !ruby/object:Gem::Version
170
170
  version: '0'
171
171
  requirements: []
172
- rubygems_version: 3.0.9
172
+ rubygems_version: 3.0.8
173
173
  signing_key:
174
174
  specification_version: 4
175
175
  summary: Gem validates phone numbers with Google libphonenumber database