phonelib 0.7.6 → 0.8.0

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: '0683165e1d17e555f17a0ed2c73bd103b97e0c591485c75ae31f037c1111c62f'
4
- data.tar.gz: 9024af8cda171f5da213ba0c6de0832a3ba6900bde08f8cfb52c5c7f0baba2f8
3
+ metadata.gz: bdcde83b646a76dd337ecbb6e7ca8976a23729936d34a0b2734ac6dc76d758e0
4
+ data.tar.gz: 5f0ef67a10c99e563f52885860e41aefa08c67df4d3593fc9fe515c1479ebc6b
5
5
  SHA512:
6
- metadata.gz: 898910034bad98e8eb27383df6dac169353ff0906eeb4fcb0284ea6c3b890bae7343d456646047be3488dc023ae62b2fab7aeaa0ecd32ec0f989d42e8202add0
7
- data.tar.gz: c4588aa08410f57b735035365222d8256f996eb4b06d1a7f052ea987070df710d2d506e6b108c576856d609cd7785616231850e5da940d4f915caf68113379be
6
+ metadata.gz: f27ccd04e753eb2349af42ea19710fec3c4d97975e477a380651f51c6d2c47202b5e029f93807659df33aac6cf3817ffda7fc6eeeceb81e02132b6d1d6294f1f
7
+ data.tar.gz: 3c2049c708d6e938cbbc3459a3bec1ad20a409b797cd4a399586705231e0321a897a9ebe19fd1cfd7e13be9cae22515b08390a323262cb0015a591186632e845
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
 
@@ -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
@@ -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
@@ -1,4 +1,4 @@
1
1
  module Phonelib
2
2
  # @private
3
- VERSION = '0.7.6'
3
+ VERSION = '0.8.0'
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.6
4
+ version: 0.8.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: 2022-12-25 00:00:00.000000000 Z
11
+ date: 2023-04-04 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