phonelib 0.7.7 → 0.8.4

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: dcdc081295ac56df465d03c3ce7a6662248efd0435418bed4ca9d4b7af4b9177
4
- data.tar.gz: 95e6742ba9fe64deabd92d477e41f6a3dc95b9d6d97439721f68d7fb11b5078f
3
+ metadata.gz: cc68ad6683a9dbc13f88b825e589c1836a31e83b8f67c6e71d54afac44d0fceb
4
+ data.tar.gz: 39ed9dfdc4bf02a8850c18068be4e24407141734718a71e8fa4779f0f1c4ff17
5
5
  SHA512:
6
- metadata.gz: 1783e018ef2a0299251a30e9b228964e186c3c88b1603d1b6f9bd068d4ae240a731127730bd1b4748c149c68f394628d5aa8917337452277c8ec18245fe5a89f
7
- data.tar.gz: e94e439077eb2e33f1fcdb1d1e735a60968258240ba97fd3c730eb236eb7bb8319cf2f0d8dea40fb3c0a8b549a0730201dd17a8e74c837722edcd087255556a9
6
+ metadata.gz: 21e94b4cd849e0116189ef7b232dd2d6949b02b9e6e295dfe463272b035669a062d90902e7f4aa6c98001c05dfa2c803f9e83320fe1b32e16f654d88305c62ca
7
+ data.tar.gz: f05c697263e31b17a6946ac9fda9c52780fa2aaef23b4031ba7eced4b34b5d576a581dbeacda334d962cb64cd46edebc1aa1c0e5fbcdf9661ccc7d0f28448dbd
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -10,6 +10,16 @@ module Phonelib
10
10
  @@phone_data ||= load_data.freeze
11
11
  end
12
12
 
13
+ # @private getter for phone data indexed by country code (internal use only)
14
+ def data_by_country_codes
15
+ @@data_by_country_codes ||= phone_data.each_value.group_by { |d| d[COUNTRY_CODE] }.freeze
16
+ end
17
+
18
+ # @private getter for all international prefixes in phone_data
19
+ def phone_data_int_prefixes
20
+ @@all_int_prefixes ||= phone_data.map {|k,v| v[:international_prefix] }.select { |v| v != '' }.compact.uniq.join('|').freeze
21
+ end
22
+
13
23
  # @private used to cache frequently-used regular expressions
14
24
  @@phone_regexp_cache = {}
15
25
 
@@ -30,14 +40,14 @@ module Phonelib
30
40
  @@default_country = nil
31
41
 
32
42
  # getter method for default_country variable
33
- # @return [String|nil] Default country set for parsing or nil
43
+ # @return [String,Symbol,Array<String,Symbol>,nil] Default country ISO2 code or codes used for parsing
34
44
  def default_country
35
45
  @@default_country
36
46
  end
37
47
 
38
48
  # setter method for default_country variable
39
- # @param country [String|Symbol] default country ISO2 code used for parsing
40
- # @return [String|nil] Default country set for parsing or nil
49
+ # @param country [String,Symbol,Array<String,Symbol>] Default country ISO2 code or codes used for parsing
50
+ # @return [String,Symbol,Array<String,Symbol>] Default country ISO2 code or codes used for parsing
41
51
  def default_country=(country)
42
52
  @@default_country = country
43
53
  end
@@ -179,7 +189,7 @@ module Phonelib
179
189
  def add_additional_regex(country, type, national_regex)
180
190
  return unless Phonelib::Core::TYPES_DESC.keys.include?(type.to_sym)
181
191
  return unless national_regex.is_a?(String)
182
- @@phone_data = nil
192
+ @@phone_data = @@data_by_country_codes = nil
183
193
  @@additional_regexes[country.to_s.upcase] ||= {}
184
194
  @@additional_regexes[country.to_s.upcase][type] ||= []
185
195
  @@additional_regexes[country.to_s.upcase][type] << national_regex
@@ -45,6 +45,7 @@ module Phonelib
45
45
  'TA' => 'SH',
46
46
  'TC' => 'US',
47
47
  'TT' => 'US',
48
+ 'UM' => 'US',
48
49
  'VA' => 'IT',
49
50
  'VC' => 'US',
50
51
  'VG' => 'US',
@@ -160,6 +161,7 @@ module Phonelib
160
161
  # some countries missing formats, and are linking them to another countries
161
162
  def process_format_links
162
163
  FORMAT_SHARING.each do |destination, source|
164
+ next unless @data[destination]
163
165
  @data[destination][:formats] ||= []
164
166
  @data[destination][:formats] = @data[destination][:formats] + @data[source][:formats]
165
167
  end
@@ -178,6 +178,8 @@ module Phonelib
178
178
 
179
179
  # @private extracts extension from passed phone number if provided
180
180
  def separate_extension(original)
181
+ return [original, ''] unless Phonelib.extension_separate_symbols
182
+
181
183
  regex = if Phonelib.extension_separate_symbols.is_a?(Array)
182
184
  cr("#{Phonelib.extension_separate_symbols.join('|')}")
183
185
  else
@@ -124,15 +124,22 @@ module Phonelib
124
124
  #
125
125
  # * +phone+ - phone number for parsing
126
126
  def detect_and_parse(phone, country)
127
- result = {}
128
- Phonelib.phone_data.each do |key, data|
127
+ countries_data = country_code_candidates_for(phone).flat_map { |code|
128
+ Phonelib.data_by_country_codes[code] || []
129
+ }
130
+ countries_data.each_with_object({}) do |data, result|
131
+ key = data[:id]
129
132
  parsed = parse_single_country(phone, data)
130
133
  if (!Phonelib.strict_double_prefix_check || key == country) && double_prefix_allowed?(data, phone, parsed && parsed[key])
131
134
  parsed = parse_single_country(changed_dp_phone(key, phone), data)
132
135
  end
133
136
  result.merge!(parsed) unless parsed.nil?
134
- end
135
- result
137
+ end.compact
138
+ end
139
+
140
+ def country_code_candidates_for(phone)
141
+ stripped_phone = phone.gsub(/^(#{Phonelib.phone_data_int_prefixes})/, '')
142
+ ((1..3).map { |length| phone[0, length] } + (1..3).map { |length| stripped_phone[0, length] }).uniq
136
143
  end
137
144
 
138
145
  # Create phone representation in e164 format
@@ -7,6 +7,8 @@ module Phonelib
7
7
  if result.size == 1
8
8
  result
9
9
  else
10
+ matched_countries = country_or_default_country(nil) & result.keys
11
+ result = result.keep_if {|k, _v| matched_countries.include?(k) } if matched_countries
10
12
  Hash[result.take(1)]
11
13
  end
12
14
  end
@@ -56,7 +58,7 @@ module Phonelib
56
58
  def country_can_dp?(country)
57
59
  Phonelib.phone_data[country] &&
58
60
  Phonelib.phone_data[country][Core::DOUBLE_COUNTRY_PREFIX_FLAG] &&
59
- !original_starts_with_plus?
61
+ !original_starts_with_plus? && original_s.start_with?(Phonelib.phone_data[country][Core::COUNTRY_CODE])
60
62
  end
61
63
 
62
64
  # changes phone to with/without double country prefix
@@ -66,6 +68,7 @@ module Phonelib
66
68
 
67
69
  country_code = Phonelib.phone_data[country][Core::COUNTRY_CODE]
68
70
  if phone.start_with? country_code * 2
71
+ # remove double prefix in case it is there
69
72
  phone.gsub(cr("^#{country_code}"), '')
70
73
  else
71
74
  "#{country_code}#{phone}"
@@ -5,7 +5,7 @@ module Phonelib
5
5
  # @param formatted [Boolean] whether to return numbers only or formatted
6
6
  # @return [String] formatted national number
7
7
  def national(formatted = true)
8
- return @national_number unless valid?
8
+ return @national_number unless possible?
9
9
  format_match, format_string = formatting_data
10
10
 
11
11
  if format_match
@@ -52,7 +52,7 @@ module Phonelib
52
52
  def international(formatted = true, prefix = '+')
53
53
  prefix = formatted if formatted.is_a?(String)
54
54
  return nil if sanitized.empty?
55
- return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless valid?
55
+ return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible?
56
56
  return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
57
57
 
58
58
  fmt = @data[country][:format]
@@ -1,4 +1,4 @@
1
1
  module Phonelib
2
2
  # @private
3
- VERSION = '0.7.7'
3
+ VERSION = '0.8.4'
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.7.7
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-04 00:00:00.000000000 Z
11
+ date: 2023-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: benchmark-ips
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: benchmark-memory
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: rack-cache
99
127
  requirement: !ruby/object:Gem::Requirement
@@ -169,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
197
  - !ruby/object:Gem::Version
170
198
  version: '0'
171
199
  requirements: []
172
- rubygems_version: 3.0.8
200
+ rubygems_version: 3.0.9
173
201
  signing_key:
174
202
  specification_version: 4
175
203
  summary: Gem validates phone numbers with Google libphonenumber database