phonelib 0.10.4 → 0.10.5

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: fb1fdf7dccd5aea928ae10a90c388d762b030e8c4def7a65d877923eeb3a4ca8
4
- data.tar.gz: b59f64fb4a31d30dd168673fd1b7449821e49fdb561f41aa81a2441b7cd56cc1
3
+ metadata.gz: 91ac6f0678295402a73fbefd2e073f04a4fff9240c6fa4f93dbde0bccfe11014
4
+ data.tar.gz: d9fa26fa4d55bad564e0002ac17694ec20ce59ac5d8a1b20752bdcc6d97ce32c
5
5
  SHA512:
6
- metadata.gz: d66563c8732ed401d1eefc8e2a2bf4339b047e1e5c840f5811cf590901461c403172abbb2fbb820adfaf2fc12ace71f5579d6089f104f9c85b8bc420faf4f2a0
7
- data.tar.gz: 0ed58b39980e34e6bf3696c0513f44c284576bafec199cd56baa242589e1b1adce29cadd3621061c33e9bfe1feec2851a839ec665a813d7010660bd5a51e733b
6
+ metadata.gz: 8dd3cdc933f0f820f59eddd5bce35d4d81ec9b8c52085498f0750769a73fecb07f9522a97cfd59e27b29a766f19c422e2f571d59d6488b26898ae34b6fb2a099
7
+ data.tar.gz: f8c549c48c8d0695c1a204c03cf4cc0189d9a408b72a8bae005b4996eefdf666fdcaa4a4d707849cd51e34ee33e41637845cfa01c8da4225b07bb7de795df3c7
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -202,7 +202,7 @@ module Phonelib
202
202
  end
203
203
 
204
204
  def add_additional_regex(country, type, national_regex)
205
- return unless Phonelib::Core::TYPES_DESC.keys.include?(type.to_sym)
205
+ return unless Phonelib::Core::TYPES_DESC.key?(type.to_sym)
206
206
  return unless national_regex.is_a?(String)
207
207
  @@phone_data = @@data_by_country_codes = nil
208
208
  @@additional_regexes[country.to_s.upcase] ||= {}
@@ -381,6 +381,7 @@ module Phonelib
381
381
  carrier_selection_codes: 'Carrier Selection codes',
382
382
  area_code_optional: 'Are code optional'
383
383
  }.freeze
384
+ TYPES_DESC_KEYS = TYPES_DESC.keys.freeze
384
385
 
385
386
  # @private short codes types keys
386
387
  SHORT_CODES = [
@@ -407,6 +408,13 @@ module Phonelib
407
408
  # @private Extended data key for carrier in prefixes hash
408
409
  EXT_CARRIER_KEY = :c
409
410
 
411
+ # @private Static arrays used to avoid allocations
412
+ FIXED_OR_MOBILE_ARRAY = [Core::FIXED_OR_MOBILE].freeze
413
+ FIXED_LINE_OR_MOBILE_ARRAY = [Core::FIXED_LINE, Core::MOBILE].freeze
414
+ POSSIBLE_VALID_ARRAY = [:possible, :valid].freeze
415
+ VALID_POSSIBLE_ARRAY = [:valid, :possible].freeze
416
+ NIL_RESULT_ARRAY = [nil, nil].freeze
417
+
410
418
  # method for parsing phone number.
411
419
  # On first run fills @@phone_data with data present in yaml file
412
420
  # @param phone [String] the phone number to be parsed
@@ -103,7 +103,13 @@ module Phonelib
103
103
  line = str_clean line, false
104
104
  next if line.empty? || line[0] == '#'
105
105
  prefix, line_data = line.split('|')
106
- data[prefix] = line_data && line_data.strip.split('&')
106
+ if line_data
107
+ data[prefix] = if line_data.strip =~ /[^ ]{3,}&[^ ]{3,}/
108
+ line_data.strip.split('&')
109
+ else
110
+ line_data.strip
111
+ end
112
+ end
107
113
  end
108
114
  data
109
115
  end
@@ -33,7 +33,7 @@ module Phonelib
33
33
 
34
34
  # pick best result when several countries specified
35
35
  def pick_results(results)
36
- [:valid, :possible].each do |key|
36
+ Core::VALID_POSSIBLE_ARRAY.each do |key|
37
37
  final = results.select { |_k, v| v[key].any? }
38
38
  return decorate_analyze_result(final) if final.size > 0
39
39
  end
@@ -72,7 +72,7 @@ module Phonelib
72
72
  def with_replaced_national_prefix(passed_phone, data)
73
73
  return passed_phone unless data[Core::NATIONAL_PREFIX_TRANSFORM_RULE]
74
74
  phone = if passed_phone.start_with?(data[Core::COUNTRY_CODE]) && !data[Core::DOUBLE_COUNTRY_PREFIX_FLAG]
75
- passed_phone.gsub(/^#{data[Core::COUNTRY_CODE]}/, '')
75
+ passed_phone.delete_prefix(data[Core::COUNTRY_CODE])
76
76
  else
77
77
  passed_phone
78
78
  end
@@ -185,7 +185,7 @@ module Phonelib
185
185
  if Phonelib.additional_regexes.is_a?(Hash) && Phonelib.additional_regexes[data[:id]]
186
186
  index = Phonelib.additional_regexes[data[:id]].values.flatten.join('|').scan(/\(/).size
187
187
  end
188
- phone = country_match.to_a[-1 - index]
188
+ phone = country_match[-1 - index]
189
189
  result[:national] = phone
190
190
  result[:format] = number_format(phone, data[Core::FORMATS])
191
191
  result.merge! all_number_types(phone, data[Core::TYPES], not_valid)
@@ -226,7 +226,7 @@ module Phonelib
226
226
  def number_format(national, format_data)
227
227
  format_data && format_data.find do |format|
228
228
  (format[Core::LEADING_DIGITS].nil? || \
229
- national.match(cr("^(#{format[Core::LEADING_DIGITS]})"))) && \
229
+ national.match?(cr("^(#{format[Core::LEADING_DIGITS]})"))) && \
230
230
  national.match(cr("^(#{format[Core::PATTERN]})$"))
231
231
  end || Core::DEFAULT_NUMBER_FORMAT
232
232
  end
@@ -247,7 +247,7 @@ module Phonelib
247
247
  type_regex(patterns, Core::VALID_PATTERN)
248
248
  ]
249
249
  else
250
- [nil, nil]
250
+ Core::NIL_RESULT_ARRAY
251
251
  end
252
252
  end
253
253
  end
@@ -119,14 +119,15 @@ module Phonelib
119
119
  # * +data+ - country data hash
120
120
  # * +country_optional+ - whether to put country code as optional group
121
121
  def full_regex_for_data(data, type, country_optional = true)
122
- regex = []
123
- regex << '0{2}?'
124
- regex << "(#{data[Core::INTERNATIONAL_PREFIX]})?"
125
- regex << "(#{data[Core::COUNTRY_CODE]})#{country_optional ? '?' : ''}"
126
- regex << "(#{data[Core::NATIONAL_PREFIX_FOR_PARSING] || data[Core::NATIONAL_PREFIX]})?"
127
- regex << "(#{type_regex(data[Core::TYPES][Core::GENERAL], type)})" if data[Core::TYPES]
122
+ regex = +"^0{2}?(#{data[Core::INTERNATIONAL_PREFIX]})?(#{data[Core::COUNTRY_CODE]})#{country_optional ? '?' : ''}(#{data[Core::NATIONAL_PREFIX_FOR_PARSING] || data[Core::NATIONAL_PREFIX]})?"
123
+ if data[Core::TYPES]
124
+ regex << "("
125
+ regex << type_regex(data[Core::TYPES][Core::GENERAL], type)
126
+ regex << ")"
127
+ end
128
+ regex << "$"
128
129
 
129
- cr("^#{regex.join}$")
130
+ cr(regex)
130
131
  end
131
132
 
132
133
  # Returns regex for type with special types if needed
@@ -136,11 +137,11 @@ module Phonelib
136
137
  # * +data+ - country types data for single type
137
138
  # * +type+ - possible or valid regex type needed
138
139
  def type_regex(data, type)
139
- regex = [data[type]]
140
140
  if Phonelib.parse_special && data[Core::SHORT] && data[Core::SHORT][type]
141
- regex << data[Core::SHORT][type]
141
+ "#{data[type]}|#{data[Core::SHORT][type]}"
142
+ else
143
+ data[type]
142
144
  end
143
- regex.join('|')
144
145
  end
145
146
 
146
147
  # Check if phone match country data
@@ -150,9 +151,7 @@ module Phonelib
150
151
  # * +phone+ - phone number for parsing
151
152
  # * +data+ - country data
152
153
  def phone_match_data?(phone, data, possible = false)
153
- country_code = "#{data[Core::COUNTRY_CODE]}"
154
- inter_prefix = "(#{data[Core::INTERNATIONAL_PREFIX]})?"
155
- return unless phone.match cr("^0{2}?#{inter_prefix}#{country_code}")
154
+ return unless phone.match?(cr("^0{2}?(#{data[Core::INTERNATIONAL_PREFIX]})?#{data[Core::COUNTRY_CODE]}"))
156
155
 
157
156
  type = possible ? Core::POSSIBLE_PATTERN : Core::VALID_PATTERN
158
157
  phone.match full_regex_for_data(data, type, false)
@@ -161,10 +160,9 @@ module Phonelib
161
160
  # checks if types has both :mobile and :fixed_line and replaces it with
162
161
  # :fixed_or_mobile in case both present
163
162
  def sanitize_fixed_mobile(types)
164
- fixed_mobile = [Core::FIXED_LINE, Core::MOBILE]
165
- [:possible, :valid].each do |key|
166
- if (fixed_mobile - types[key]).empty?
167
- types[key] = types[key] - fixed_mobile + [Core::FIXED_OR_MOBILE]
163
+ Core::POSSIBLE_VALID_ARRAY.each do |key|
164
+ if (Core::FIXED_LINE_OR_MOBILE_ARRAY - types[key]).empty?
165
+ types[key] = types[key] - Core::FIXED_LINE_OR_MOBILE_ARRAY + Core::FIXED_OR_MOBILE_ARRAY
168
166
  end
169
167
  end
170
168
  types
@@ -178,7 +176,7 @@ module Phonelib
178
176
  def types_for_check(data)
179
177
  exclude_list = PhoneAnalyzer::NOT_FOR_CHECK
180
178
  exclude_list += Phonelib::Core::SHORT_CODES unless Phonelib.parse_special
181
- Core::TYPES_DESC.keys - exclude_list + fixed_and_mobile_keys(data)
179
+ Core::TYPES_DESC_KEYS - exclude_list + fixed_and_mobile_keys(data)
182
180
  end
183
181
 
184
182
  # Checks if fixed line pattern and mobile pattern are the same and returns
@@ -189,9 +187,9 @@ module Phonelib
189
187
  # * +data+ - country data
190
188
  def fixed_and_mobile_keys(data)
191
189
  if data[Core::FIXED_LINE] == data[Core::MOBILE]
192
- [Core::FIXED_OR_MOBILE]
190
+ Core::FIXED_OR_MOBILE_ARRAY
193
191
  else
194
- [Core::FIXED_LINE, Core::MOBILE]
192
+ Core::FIXED_LINE_OR_MOBILE_ARRAY
195
193
  end
196
194
  end
197
195
 
@@ -18,12 +18,19 @@ module Phonelib
18
18
  Phonelib::Core::EXT_GEO_NAME_KEY
19
19
  end
20
20
 
21
- # Returns timezone of parsed phone number or nil if number is invalid or
22
- # there is no timezone specified in db for this number
21
+ # Returns first timezone (in case several match) of parsed phone number or nil
22
+ # if number is invalid or there is no timezone specified in db for this number
23
23
  # @return [String|nil] timezone for parsed phone
24
24
  def timezone
25
- get_ext_name Phonelib::Core::EXT_TIMEZONES,
26
- Phonelib::Core::EXT_TIMEZONE_KEY
25
+ timezones.first
26
+ end
27
+
28
+ # Returns timezones of parsed phone number or nil if number is invalid or
29
+ # there is no timezone specified in db for this number
30
+ # @return [Array] timezones for parsed phone
31
+ def timezones
32
+ res = get_ext_name Phonelib::Core::EXT_TIMEZONES, Phonelib::Core::EXT_TIMEZONE_KEY
33
+ res.is_a?(Array) ? res : [res]
27
34
  end
28
35
 
29
36
  # Returns carrier of parsed phone number or nil if number is invalid or
@@ -53,9 +60,7 @@ module Phonelib
53
60
  def get_ext_name(names_key, id_key)
54
61
  return nil unless ext_data[id_key]
55
62
 
56
- res = Phonelib.phone_ext_data[names_key][ext_data[id_key]]
57
- return nil unless res
58
- res.size == 1 ? res.first : res
63
+ Phonelib.phone_ext_data[names_key][ext_data[id_key]]
59
64
  end
60
65
 
61
66
  # @private returns extended data ids for current number
@@ -54,7 +54,7 @@ module Phonelib
54
54
  def international(formatted = true, prefix = '+')
55
55
  prefix = formatted if formatted.is_a?(String)
56
56
  return nil if sanitized.empty?
57
- return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible?
57
+ return "#{prefix}#{sanitized}" unless possible?
58
58
  return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
59
59
 
60
60
  fmt = @data[country][:format]
@@ -137,12 +137,6 @@ module Phonelib
137
137
  true
138
138
  end
139
139
 
140
- # @private defines whether to put country prefix or not
141
- def country_prefix_or_not
142
- return '' unless data_country_code
143
- sanitized.start_with?(data_country_code) ? '' : data_country_code
144
- end
145
-
146
140
  # @private returns extension with separator defined
147
141
  def formatted_extension
148
142
  return '' if @extension.nil? || @extension.empty?
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Phonelib
4
4
  # @private
5
- VERSION = '0.10.4'
5
+ VERSION = '0.10.5'
6
6
  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.10.4
4
+ version: 0.10.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-10 00:00:00.000000000 Z
11
+ date: 2025-02-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Google libphonenumber library was taken as a basis for
@@ -40,7 +40,7 @@ homepage: https://github.com/daddyz/phonelib
40
40
  licenses:
41
41
  - MIT
42
42
  metadata:
43
- changelog_uri: https://github.com/daddyz/phonelib/releases/tag/v0.10.4
43
+ changelog_uri: https://github.com/daddyz/phonelib/releases/tag/v0.10.5
44
44
  post_install_message:
45
45
  rdoc_options:
46
46
  - " --no-private - CHANGELOG.md --readme README.md"