phonelib 0.6.48 → 0.6.58

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: 1cfc6e3a60c622177adbc8caf40673faebc7a4919c766d021a199ec52372a385
4
+ data.tar.gz: 759d7b9dfa7807dcf33a3b19fc6e3a8d5cb98febe1f9458dced2f7d8a96a40f6
5
5
  SHA512:
6
- metadata.gz: 54375aea5dd1cf1ddd0d3c51d7ac9a6db4b745e152ef5d1a11c45571ef7f4bfb579e1021a1a9c13dc83ba77e39cca12e19904aba71f632456d15ec1d8e9ec97a
7
- data.tar.gz: 85495f7efe0e2f9da9adbbe9b4b4f2e8219fc5f6842ae473a81626c9f8629557351ff025a98794f95ee79c1d661bf4f39ff5c3f4191db36d21d2c14849252d9c
6
+ metadata.gz: a56a4ad205176638db2a08bb79c8366df4a0d839f971e7b8b38162c2089ad98671602e6a4f128510de78cf4e9fc73762af60e940fe7dcd8b8b9bd00d52f2186e
7
+ data.tar.gz: b5be23901e6786c28dfa16cd83130256e88a2323dacc7f9b45c5f6baa342b9a379076af3d1a4629b1b4a33b63c5ae11dd91943374b5daa67f85c20ed40704dd7
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
@@ -237,7 +252,7 @@ phone.full_e164 # returns e164 phone representation with extension
237
252
  phone.full_international # returns formatted international number with extension
238
253
  ```
239
254
 
240
- You can pass <tt>false</tt> to <tt>national</tt> and <tt>international</tt> methods in order to get unformatted representaions
255
+ You can pass <tt>false</tt> to <tt>national</tt> and <tt>international</tt> methods in order to get unformatted representations
241
256
 
242
257
  ``` ruby
243
258
  phone.international(false) # returns unformatted international phone
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
 
@@ -19,6 +19,47 @@ module Phonelib
19
19
 
20
20
  # countries that can have double country prefix in number
21
21
  DOUBLE_COUNTRY_CODES_COUNTRIES = %w(IN DE BR IT NO PL CU VN)
22
+ FORMAT_SHARING = {
23
+ 'CA' => 'US',
24
+ 'CC' => 'AU',
25
+ 'CX' => 'AU',
26
+ 'DM' => 'US',
27
+ 'DO' => 'US',
28
+ 'EH' => 'MA',
29
+ 'GD' => 'US',
30
+ 'GG' => 'GB',
31
+ 'GU' => 'US',
32
+ 'IM' => 'GB',
33
+ 'JE' => 'GB',
34
+ 'JM' => 'US',
35
+ 'KN' => 'US',
36
+ 'KY' => 'US',
37
+ 'KZ' => 'RU',
38
+ 'LC' => 'US',
39
+ 'MF' => 'GP',
40
+ 'MP' => 'US',
41
+ 'MS' => 'US',
42
+ 'PR' => 'US',
43
+ 'SJ' => 'NO',
44
+ 'SX' => 'US',
45
+ 'TA' => 'SH',
46
+ 'TC' => 'US',
47
+ 'TT' => 'US',
48
+ 'VA' => 'IT',
49
+ 'VC' => 'US',
50
+ 'VG' => 'US',
51
+ 'VI' => 'US',
52
+ 'YT' => 'RE',
53
+ 'AG' => 'US',
54
+ 'AI' => 'US',
55
+ 'AS' => 'US',
56
+ 'AX' => 'FI',
57
+ 'BB' => 'US',
58
+ 'BL' => 'GP',
59
+ 'BM' => 'US',
60
+ 'BQ' => 'CW',
61
+ 'BS' => 'US',
62
+ }
22
63
 
23
64
  # main data file in repo
24
65
  MAIN_FILE = 'resources/PhoneNumberMetadata.xml'
@@ -55,6 +96,7 @@ module Phonelib
55
96
  import_main_data
56
97
  import_short_data
57
98
  import_alternate_formats
99
+ process_format_links
58
100
  import_geocoding_data
59
101
  import_timezone_data
60
102
  import_carrier_data
@@ -115,6 +157,14 @@ module Phonelib
115
157
  end
116
158
  end
117
159
 
160
+ # some countries missing formats, and are linking them to another countries
161
+ def process_format_links
162
+ FORMAT_SHARING.each do |destination, source|
163
+ @data[destination][:formats] ||= []
164
+ @data[destination][:formats] = @data[destination][:formats] + @data[source][:formats]
165
+ end
166
+ end
167
+
118
168
  # method parses geocoding data dir
119
169
  def import_geocoding_data
120
170
  puts 'IMPORTING GEOCODING DATA'
@@ -148,7 +198,7 @@ module Phonelib
148
198
  io = open('http://api.geonames.org/countryInfoCSV?username=demo&style=full')
149
199
  csv = CSV.new(io, {col_sep: "\t"})
150
200
  csv.each do |row|
151
- next if row[0].nil? || row[0].start_with?('#') || row[0].empty?
201
+ next if row[0].nil? || row[0].start_with?('#') || row[0].empty? || row[0].size != 2
152
202
 
153
203
  @countries[row[0]] = row[4]
154
204
  end
@@ -7,6 +7,9 @@ module Phonelib
7
7
  # @!attribute [r] extension
8
8
  # @return [String] phone extension passed for parsing after a number
9
9
  attr_reader :extension
10
+ # @!attribute [r] national_number
11
+ # @return [String] phone national number
12
+ attr_reader :national_number
10
13
 
11
14
  # including module that has all phone analyzing methods
12
15
  include Phonelib::PhoneAnalyzer
@@ -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.58'
4
4
  end
@@ -62,7 +62,7 @@ class PhoneValidator < ActiveModel::EachValidator
62
62
  @phone = parse(value, specified_country(record))
63
63
  valid = phone_valid? && valid_types? && valid_country? && valid_extensions?
64
64
 
65
- record.errors.add(attribute, message, options) unless valid
65
+ record.errors.add(attribute, message, **options) unless valid
66
66
  end
67
67
 
68
68
  private
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.58
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: 2022-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.10.8
33
+ version: 1.13.0
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.8
40
+ version: 1.13.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -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