phonelib 0.8.0 → 0.9.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: bdcde83b646a76dd337ecbb6e7ca8976a23729936d34a0b2734ac6dc76d758e0
4
- data.tar.gz: 5f0ef67a10c99e563f52885860e41aefa08c67df4d3593fc9fe515c1479ebc6b
3
+ metadata.gz: cfc1c4a9625861e072096320aec6b4867d509d47b170fcd0a92c0ffde521f7b5
4
+ data.tar.gz: f0d3db76ed4614f8667796d79b4166d2827801997b04986429d7b80418337420
5
5
  SHA512:
6
- metadata.gz: f27ccd04e753eb2349af42ea19710fec3c4d97975e477a380651f51c6d2c47202b5e029f93807659df33aac6cf3817ffda7fc6eeeceb81e02132b6d1d6294f1f
7
- data.tar.gz: 3c2049c708d6e938cbbc3459a3bec1ad20a409b797cd4a399586705231e0321a897a9ebe19fd1cfd7e13be9cae22515b08390a323262cb0015a591186632e845
6
+ metadata.gz: 03fd33d4683e7845bb94c5293ab0d9e28654cf4e9024a60221e62017d60a396c71167382ce40eef5ff60fcc1c2578845a6bdf996050f275e198dc04c280a54b3
7
+ data.tar.gz: 9fe16f0ecab0ce8f583a61c7516052b59394d11f4555b267c1602cd6c5113fda3b109a8e0e4d6c3624ce2e01c39b3e32e89b52b2f63ffb0e019e7359b6cddb5f
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -4,6 +4,19 @@ module Phonelib
4
4
  # @private variable will include hash with data for validation
5
5
  @@phone_data = nil
6
6
 
7
+ # eagerly initialize the gem, loads data into memory. not required, initialization is done lazily otherwise, but
8
+ # may be desirable in production enviroments to avoid initialization time on first use.
9
+ def eager_load!
10
+ return if @@skip_eager_loading
11
+ phone_data
12
+ phone_ext_data
13
+ end
14
+
15
+ @@skip_eager_loading = false
16
+ def skip_eager_loading!
17
+ @@skip_eager_loading = true
18
+ end
19
+
7
20
  # getter for phone data for other modules of gem, can be used outside
8
21
  # @return [Hash] all data for phone parsing
9
22
  def phone_data
@@ -40,14 +53,14 @@ module Phonelib
40
53
  @@default_country = nil
41
54
 
42
55
  # getter method for default_country variable
43
- # @return [String|nil] Default country set for parsing or nil
56
+ # @return [String,Symbol,Array<String,Symbol>,nil] Default country ISO2 code or codes used for parsing
44
57
  def default_country
45
58
  @@default_country
46
59
  end
47
60
 
48
61
  # setter method for default_country variable
49
- # @param country [String|Symbol] default country ISO2 code used for parsing
50
- # @return [String|nil] Default country set for parsing or nil
62
+ # @param country [String,Symbol,Array<String,Symbol>] Default country ISO2 code or codes used for parsing
63
+ # @return [String,Symbol,Array<String,Symbol>] Default country ISO2 code or codes used for parsing
51
64
  def default_country=(country)
52
65
  @@default_country = country
53
66
  end
@@ -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
@@ -195,7 +197,7 @@ module Phonelib
195
197
 
196
198
  require 'open-uri'
197
199
  require 'csv'
198
- io = open('http://api.geonames.org/countryInfoCSV?username=demo&style=full')
200
+ io = URI.open('http://download.geonames.org/export/dump/countryInfo.txt')
199
201
  csv = CSV.new(io, {col_sep: "\t"})
200
202
  csv.each do |row|
201
203
  next if row[0].nil? || row[0].start_with?('#') || row[0].empty? || row[0].size != 2
@@ -70,10 +70,11 @@ module Phonelib
70
70
  # replacing national prefix to simplified format
71
71
  def with_replaced_national_prefix(phone, data)
72
72
  return phone unless data[Core::NATIONAL_PREFIX_TRANSFORM_RULE]
73
+ phone = phone.gsub(/^#{data[Core::COUNTRY_CODE]}/, '') if phone.start_with?(data[Core::COUNTRY_CODE])
73
74
  pattern = cr("^(?:#{data[Core::NATIONAL_PREFIX_FOR_PARSING]})")
74
75
  match = phone.match pattern
75
76
  if match && match.captures.compact.size > 0
76
- phone.gsub(pattern, data[Core::NATIONAL_PREFIX_TRANSFORM_RULE])
77
+ data[Core::COUNTRY_CODE] + phone.gsub(pattern, data[Core::NATIONAL_PREFIX_TRANSFORM_RULE])
77
78
  else
78
79
  phone
79
80
  end
@@ -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.8.0'
3
+ VERSION = '0.9.0'
4
4
  end
data/lib/phonelib.rb CHANGED
@@ -17,4 +17,12 @@ if defined?(ActiveModel) || defined?(Rails)
17
17
  else
18
18
  autoload :PhoneValidator, 'validators/phone_validator'
19
19
  end
20
+
21
+ if defined?(Rails)
22
+ class Phonelib::Railtie < Rails::Railtie
23
+ initializer 'phonelib' do |app|
24
+ app.config.eager_load_namespaces << Phonelib
25
+ end
26
+ end
27
+ end
20
28
  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.8.0
4
+ version: 0.9.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: 2023-04-04 00:00:00.000000000 Z
11
+ date: 2024-07-17 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.13.0
33
+ version: '1.15'
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.13.0
40
+ version: '1.15'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 1.0.0
75
+ version: 1.0.9
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 1.0.0
82
+ version: 1.0.9
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: simplecov
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -197,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  - !ruby/object:Gem::Version
198
198
  version: '0'
199
199
  requirements: []
200
- rubygems_version: 3.0.8
200
+ rubygems_version: 3.1.6
201
201
  signing_key:
202
202
  specification_version: 4
203
203
  summary: Gem validates phone numbers with Google libphonenumber database