phonelib 0.10.28 → 0.11.0

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: e2ff5d2be5e11590bbe30ff999ef667a7724c21a8144289dd92c5f9501960663
4
- data.tar.gz: 71d31c02be419357e5bf9e8d8cf9640cfbcd9fb2c4ec2ed445b5ec19a2113fa3
3
+ metadata.gz: 5b7e4d19ed43a5ee5a41cb1aa99f7bd77ac8d627151811faf0a2475c9fafad78
4
+ data.tar.gz: c3c2d2e5c46d41295f00246164e445c4e4395aed14ffdc8a61dcb1689ab0655e
5
5
  SHA512:
6
- metadata.gz: b0678c12d7c235d27c799a90a1b7845f7acdbcaaa21669ad0240380cbf322c152d8fd9b1c2f9922f6b5e4b710fe94ce2a6e4d04aeb207bb550c4323f5f935447
7
- data.tar.gz: 4bed4ae1dcf54052bfa2ccb6ead73169e19ecd5a98ffc64c8a673a6cd0046caf5c7f66f9baee3ab963af6c01775f9813318aec101ea34164bf8d9752d1a5e6ae
6
+ metadata.gz: e491b62a977e6425ce96b81c9ba07fe5614e87789268c4c53c97a3c59ee5e360e1ab55c3f8d7117d81f2112f0ed44762fa23b9dac309b95a57b66f665daa896c
7
+ data.tar.gz: 2766566f2cf573c4bbbaf392118517b12cc4529c5ed42af267955c1b7d37962b61ca8916d893e1842294b95701444e41a386e23bd68abac1bc5ba50c67368e51
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -43,6 +43,14 @@ module Phonelib
43
43
  @@phone_regexp_cache
44
44
  end
45
45
 
46
+ # @private used to cache regular expressions matching a whole number
47
+ @@phone_anchored_regexp_cache = {}
48
+
49
+ # @private getter for anchored phone regexp cache (internal use only)
50
+ def phone_anchored_regexp_cache
51
+ @@phone_anchored_regexp_cache
52
+ end
53
+
46
54
  # @private variable for storing geo/carrier/timezone data
47
55
  @@phone_ext_data = nil
48
56
 
@@ -186,6 +194,7 @@ module Phonelib
186
194
  @@override_phone_data = file_path
187
195
  @@phone_data = @@data_by_country_codes = @@all_int_prefixes = nil
188
196
  @@phone_regexp_cache.clear
197
+ @@phone_anchored_regexp_cache.clear
189
198
  end
190
199
 
191
200
  def override_phone_data
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phonelib
4
+ module Locale
5
+ FILES = Dir[File.expand_path('locale/*.yml', __dir__)].freeze
6
+
7
+ def self.register
8
+ require 'yaml'
9
+
10
+ # Look up application translations before registering the default files:
11
+ # the lookup initializes a lazy backend using its existing load path.
12
+ FILES.each do |file|
13
+ YAML.load_file(file).each do |locale, data|
14
+ data['errors']['messages'].each do |key, message|
15
+ unless I18n.backend.exists?(locale, key, scope: [:errors, :messages])
16
+ I18n.backend.store_translations(locale, errors: { messages: { key => message } })
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ # Future reloads load gem defaults before application overrides, without
23
+ # reloading the current backend or discarding its stored translations.
24
+ I18n.load_path.replace(FILES | I18n.load_path)
25
+ end
26
+ end
27
+ end
@@ -27,7 +27,7 @@ module Phonelib
27
27
  # @return [Phonelib::Phone] parsed phone instance
28
28
  def initialize(phone, country = nil)
29
29
  @original, @extension = separate_extension(phone.to_s)
30
- @extension = @extension.gsub(/[^0-9]/, '') if @extension
30
+ @extension = @extension.delete('^0-9') if @extension
31
31
 
32
32
  if sanitized.empty?
33
33
  @data = {}
@@ -35,6 +35,8 @@ module Phonelib
35
35
  else
36
36
  @passed_country = passed_country(country)
37
37
  @data = analyze(sanitized, @passed_country)
38
+ # must run before anything memoizes validity or country
39
+ national_double_zero_fallback(country) unless any_valid?(@data)
38
40
  # the national number must come from the country #country reports:
39
41
  # another entry can be a possible-only match, and mixing entries makes
40
42
  # e164 splice one country's code onto another's number (issue #355)
@@ -206,6 +208,30 @@ module Phonelib
206
208
  @data[country]
207
209
  end
208
210
 
211
+ # @private checks whether analyze result has any valid country entry
212
+ def any_valid?(data)
213
+ data.values.any? { |d| d[:valid].any? }
214
+ end
215
+
216
+ # @private a leading 00 is read as an international prefix first; when
217
+ # that reading is not valid, retry it as national digits of the passed or
218
+ # default country
219
+ def national_double_zero_fallback(country)
220
+ return unless (country || Phonelib.default_country) &&
221
+ significant_original_s.start_with?('00') &&
222
+ original_starts_with_plus_or_double_zero?
223
+
224
+ @original_starts_with_plus_or_double_zero = false
225
+ national_country = passed_country(country)
226
+ national_data = analyze(sanitized, national_country)
227
+ if any_valid?(national_data)
228
+ @passed_country = national_country
229
+ @data = national_data
230
+ else
231
+ @original_starts_with_plus_or_double_zero = true
232
+ end
233
+ end
234
+
209
235
  # @private extracts extension from passed phone number if provided
210
236
  def separate_extension(original)
211
237
  return [original, ''] unless Phonelib.extension_separate_symbols
@@ -9,6 +9,14 @@ module Phonelib
9
9
  # array of types not included for validation check in cycle
10
10
  NOT_FOR_CHECK = [:general_desc, :fixed_line, :mobile, :fixed_or_mobile]
11
11
 
12
+ # @private types checked for each number, keyed by whether the fixed line
13
+ # and mobile patterns are the same, precomputed to avoid allocations
14
+ TYPES_FOR_CHECK_WITH_SPECIAL = {
15
+ true => (Core::TYPES_DESC_KEYS - NOT_FOR_CHECK + Core::FIXED_OR_MOBILE_ARRAY).freeze,
16
+ false => (Core::TYPES_DESC_KEYS - NOT_FOR_CHECK + Core::FIXED_LINE_OR_MOBILE_ARRAY).freeze
17
+ }.freeze
18
+ TYPES_FOR_CHECK = TYPES_FOR_CHECK_WITH_SPECIAL.transform_values { |types| (types - Core::SHORT_CODES).freeze }.freeze
19
+
12
20
  # parses provided phone if it is valid for country data and
13
21
  # returns result of analyze
14
22
  #
@@ -132,24 +140,40 @@ module Phonelib
132
140
  #
133
141
  # * +phone+ - phone number for parsing
134
142
  def detect_and_parse(phone, country)
135
- countries_data = country_code_candidates_for(phone).flat_map { |code|
136
- Phonelib.data_by_country_codes[code] || []
137
- }
138
- countries_data.each_with_object({}) do |data, result|
139
- key = data[:id]
140
- parsed = parse_single_country(phone, data)
141
- unless parsed && parsed[key] && parsed[key][:valid].size > 0
142
- replaced = with_replaced_national_prefix(phone, data)
143
- # re-parsing an unchanged phone cannot change the result
144
- replaced_parsed = parse_single_country(replaced, data) unless replaced == phone
145
- parsed = replaced_parsed unless replaced_parsed.nil?
143
+ country_code_candidates_for(phone).each_with_object({}) do |code, result|
144
+ countries_data = Phonelib.data_by_country_codes[code] || []
145
+ main = countries_data.find { |data| data[Core::MAIN_COUNTRY_FOR_CODE] == 'true' }
146
+ main_parsed = main && detect_and_parse_country(phone, country, main)
147
+ # other countries sharing the code are only checked when the code's
148
+ # main country (e.g. US for 1) does not give a valid result
149
+ if main_parsed && main_parsed[main[:id]] && main_parsed[main[:id]][:valid].any?
150
+ result.merge!(main_parsed)
151
+ next
146
152
  end
147
- if (!Phonelib.strict_double_prefix_check || key == country) && double_prefix_allowed?(data, phone, parsed && parsed[key])
148
- parsed2 = parse_single_country(changed_dp_phone(key, phone), data)
149
- parsed = parsed2 if parsed2 && parsed2[key] && parsed2[key][:valid].size > 0
153
+
154
+ countries_data.each do |data|
155
+ parsed = data.equal?(main) ? main_parsed : detect_and_parse_country(phone, country, data)
156
+ result.merge!(parsed) if parsed
150
157
  end
151
- result.merge!(parsed) unless parsed.nil?
152
- end.compact
158
+ end
159
+ end
160
+
161
+ # parses phone for single detected country, including national prefix
162
+ # replacement and double country prefix handling
163
+ def detect_and_parse_country(phone, country, data)
164
+ key = data[:id]
165
+ parsed = parse_single_country(phone, data)
166
+ unless parsed && parsed[key] && parsed[key][:valid].size > 0
167
+ replaced = with_replaced_national_prefix(phone, data)
168
+ # re-parsing an unchanged phone cannot change the result
169
+ replaced_parsed = parse_single_country(replaced, data) unless replaced == phone
170
+ parsed = replaced_parsed unless replaced_parsed.nil?
171
+ end
172
+ if (!Phonelib.strict_double_prefix_check || key == country) && double_prefix_allowed?(data, phone, parsed && parsed[key])
173
+ parsed2 = parse_single_country(changed_dp_phone(key, phone), data)
174
+ parsed = parsed2 if parsed2 && parsed2[key] && parsed2[key][:valid].size > 0
175
+ end
176
+ parsed
153
177
  end
154
178
 
155
179
  def country_code_candidates_for(phone)
@@ -17,7 +17,9 @@ module Phonelib
17
17
  end
18
18
 
19
19
  def original_starts_with_plus_or_double_zero?
20
- significant_original_s[0] == Core::PLUS_SIGN || significant_original_s[0..1] == '00' && significant_original_s[2..-1] =~ cr('[1-9]+')
20
+ return @original_starts_with_plus_or_double_zero if defined?(@original_starts_with_plus_or_double_zero)
21
+
22
+ @original_starts_with_plus_or_double_zero = significant_original_s[0] == Core::PLUS_SIGN || significant_original_s[0..1] == '00' && significant_original_s[2..-1] =~ cr('[1-9]+')
21
23
  end
22
24
 
23
25
  # converts symbols in phone to numbers
@@ -38,7 +40,7 @@ module Phonelib
38
40
  def passed_country(country)
39
41
  code = country_prefix(country)
40
42
  if !Phonelib.ignore_plus && original_starts_with_plus_or_double_zero? && code &&
41
- !sanitized.delete_prefix('00').start_with?(code)
43
+ !country_international_prefix?(country) && !sanitized.delete_prefix('00').start_with?(code)
42
44
  # in case number passed with + or 00 but it doesn't start with passed
43
45
  # country prefix
44
46
  country = nil
@@ -58,6 +60,11 @@ module Phonelib
58
60
  Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(block_given? ? yield(regexp) : regexp).freeze
59
61
  end
60
62
 
63
+ # caches regular expression matching the whole string against the pattern
64
+ def cr_anchored(regexp)
65
+ Phonelib.phone_anchored_regexp_cache[regexp] ||= Regexp.new("\\A(?:#{regexp})\\z").freeze
66
+ end
67
+
61
68
  # defines whether country can have double country prefix in number
62
69
  def country_can_dp?(country)
63
70
  Phonelib.phone_data[country] &&
@@ -114,7 +121,9 @@ module Phonelib
114
121
  #
115
122
  # * +country+ - country passed for parsing
116
123
  def country_or_default_country(country)
117
- country ||= (original_starts_with_plus_or_double_zero? ? nil : Phonelib.default_country)
124
+ country ||= if !original_starts_with_plus_or_double_zero? || country_international_prefix?(Phonelib.default_country)
125
+ Phonelib.default_country
126
+ end
118
127
 
119
128
  if country.is_a?(Array)
120
129
  country.compact.map { |e| normalize_passed_country(e) }
@@ -123,6 +132,18 @@ module Phonelib
123
132
  end
124
133
  end
125
134
 
135
+ # Keep the origin country long enough to strip its own IDD prefix.
136
+ # A leading plus is already international, not an origin-country IDD.
137
+ def country_international_prefix?(country)
138
+ return false if significant_original_s[0] == Core::PLUS_SIGN
139
+
140
+ Array(country).any? do |iso2|
141
+ data = Phonelib.phone_data[normalize_passed_country(iso2)]
142
+ prefix = data && data[Core::INTERNATIONAL_PREFIX]
143
+ prefix && !prefix.empty? && sanitized.match?(cr("^(?:#{prefix})"))
144
+ end
145
+ end
146
+
126
147
  # constructs full regex for phone validation for provided phone data
127
148
  # (international prefix, country code, national prefix, valid number)
128
149
  #
@@ -186,23 +207,9 @@ module Phonelib
186
207
  #
187
208
  # * +data+ - country data hash
188
209
  def types_for_check(data)
189
- exclude_list = PhoneAnalyzer::NOT_FOR_CHECK
190
- exclude_list += Phonelib::Core::SHORT_CODES unless Phonelib.parse_special
191
- Core::TYPES_DESC_KEYS - exclude_list + fixed_and_mobile_keys(data)
192
- end
193
-
194
- # Checks if fixed line pattern and mobile pattern are the same and returns
195
- # appropriate keys
196
- #
197
- # ==== Attributes
198
- #
199
- # * +data+ - country data
200
- def fixed_and_mobile_keys(data)
201
- if data[Core::FIXED_LINE] == data[Core::MOBILE]
202
- Core::FIXED_OR_MOBILE_ARRAY
203
- else
204
- Core::FIXED_LINE_OR_MOBILE_ARRAY
205
- end
210
+ fixed_or_mobile = data[Core::FIXED_LINE] == data[Core::MOBILE]
211
+ types = Phonelib.parse_special ? PhoneAnalyzer::TYPES_FOR_CHECK_WITH_SPECIAL : PhoneAnalyzer::TYPES_FOR_CHECK
212
+ types[fixed_or_mobile]
206
213
  end
207
214
 
208
215
  # Checks if passed number matches valid and possible patterns
@@ -229,8 +236,7 @@ module Phonelib
229
236
  # * +number+ - phone number for validation
230
237
  # * +regex+ - regex for perfoming a validation
231
238
  def number_match?(number, regex)
232
- match = number.match(cr("^(?:#{regex})$"))
233
- match && match.to_s.length == number.length
239
+ number.match?(cr_anchored(regex))
234
240
  end
235
241
 
236
242
  # normalization for passed country unless we are not talking about International XXX
@@ -12,7 +12,7 @@ module Phonelib
12
12
 
13
13
  if format_match
14
14
  out = format_string.gsub(/\$\d/) { |el| format_match[el[1].to_i] }
15
- formatted ? out : out.gsub(/[^0-9]/, '')
15
+ formatted ? out : out.delete('^0-9')
16
16
  else
17
17
  @national_number
18
18
  end
@@ -166,7 +166,7 @@ module Phonelib
166
166
  end
167
167
 
168
168
  @formatting_data =
169
- [@national_number.match(/#{format[Core::PATTERN]}/), format_string]
169
+ [@national_number.match(cr(format[Core::PATTERN])), format_string]
170
170
  end
171
171
  end
172
172
  end
@@ -5,8 +5,7 @@ module Phonelib
5
5
  initializer 'phonelib' do |app|
6
6
  app.config.eager_load_namespaces << Phonelib
7
7
 
8
- locale_files = Dir[File.expand_path('phonelib/locale/*.yml', __dir__)]
9
- app.config.i18n.load_path = locale_files | app.config.i18n.load_path if defined?(I18n)
8
+ app.config.i18n.load_path = Phonelib::Locale::FILES | app.config.i18n.load_path if defined?(I18n)
10
9
  end
11
10
  end
12
11
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Phonelib
4
4
  # @private
5
- VERSION = '0.10.28'
5
+ VERSION = '0.11.0'
6
6
  end
data/lib/phonelib.rb CHANGED
@@ -13,13 +13,13 @@ module Phonelib
13
13
  require 'phonelib/validation_result'
14
14
  require 'phonelib/phone_validation'
15
15
  require 'phonelib/phone'
16
+ require 'phonelib/locale'
16
17
 
17
18
  extend Core
18
19
  end
19
20
 
20
21
  autoload :PhoneValidator, 'validators/phone_validator'
21
22
 
22
- locale_files = Dir[File.expand_path('phonelib/locale/*.yml', __dir__)]
23
- I18n.load_path = locale_files | I18n.load_path if defined?(I18n)
23
+ Phonelib::Locale.register if defined?(I18n)
24
24
 
25
25
  require 'phonelib/railtie' if defined?(Rails::Railtie)
@@ -67,10 +67,7 @@
67
67
  # end
68
68
  #
69
69
 
70
- if defined?(I18n)
71
- locale_files = Dir[File.expand_path('../phonelib/locale/*.yml', __dir__)]
72
- I18n.load_path = locale_files | I18n.load_path
73
- end
70
+ Phonelib::Locale.register if defined?(I18n)
74
71
 
75
72
  class PhoneValidator < ActiveModel::EachValidator
76
73
  E164_PATTERN = /\A\+[1-9][0-9]{1,14}\z/
@@ -92,7 +89,7 @@ class PhoneValidator < ActiveModel::EachValidator
92
89
 
93
90
  phone = parse(value, specified_country(record))
94
91
  if detailed_errors?
95
- return validate_with_details(record, attribute, phone)
92
+ return validate_with_details(record, attribute, phone, value)
96
93
  end
97
94
 
98
95
  valid = phone_valid?(phone) && valid_types?(phone) && valid_country?(phone) &&
@@ -115,15 +112,13 @@ class PhoneValidator < ActiveModel::EachValidator
115
112
  options.reject { |key, _value| key == :detailed_errors }
116
113
  end
117
114
 
118
- def validate_with_details(record, attribute, phone)
115
+ def validate_with_details(record, attribute, phone, value)
119
116
  result = phone.validation(
120
117
  possible: !!options[:possible],
121
118
  types: detailed_types,
122
119
  countries: detailed_countries,
123
120
  extensions: extensions_allowed?
124
121
  )
125
- return if result.valid?
126
-
127
122
  result.errors.each do |error|
128
123
  record.errors.add(
129
124
  attribute,
@@ -131,6 +126,10 @@ class PhoneValidator < ActiveModel::EachValidator
131
126
  **detailed_error_options(error)
132
127
  )
133
128
  end
129
+
130
+ unless valid_international_prefix?(phone) && valid_input_format?(value)
131
+ record.errors.add(attribute, :phone_invalid, **legacy_error_options)
132
+ end
134
133
  end
135
134
 
136
135
  def detailed_error_options(error)
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.10.28
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-20 00:00:00.000000000 Z
11
+ date: 2026-11-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Google libphonenumber library was taken as a basis for
@@ -29,6 +29,7 @@ files:
29
29
  - lib/phonelib/data_importer.rb
30
30
  - lib/phonelib/data_importer_helper.rb
31
31
  - lib/phonelib/errors.rb
32
+ - lib/phonelib/locale.rb
32
33
  - lib/phonelib/locale/en.yml
33
34
  - lib/phonelib/phone.rb
34
35
  - lib/phonelib/phone_analyzer.rb
@@ -46,7 +47,7 @@ homepage: https://github.com/daddyz/phonelib
46
47
  licenses:
47
48
  - MIT
48
49
  metadata:
49
- changelog_uri: https://github.com/daddyz/phonelib/releases/tag/v0.10.28
50
+ changelog_uri: https://github.com/daddyz/phonelib/releases/tag/v0.11.0
50
51
  post_install_message:
51
52
  rdoc_options:
52
53
  - " --no-private - CHANGELOG.md --readme README.md"