phonelib 0.9.3 → 0.10.1

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: 2e5e37ac67277523ec2768078a0c398df1c9d672f6fa1d95f0fb1ac396eb5472
4
- data.tar.gz: 331f23d2f63729492ef0f036df74cf6cebab5e61a44adbc294ab949d5ac7f2b0
3
+ metadata.gz: 6f74da621590af3b6ba09374d12dcad33370f71e38d8ed5122baafd63ad12e32
4
+ data.tar.gz: bc722c0d765c923f5f815e77a9803635f80ca58d8dd0792190484d00a4340933
5
5
  SHA512:
6
- metadata.gz: bebcbf09637c5416820600b368dde2ebe2dfb281c4573ecf310ac7af381dbb35229e3b657e9dd0ec3820c43d7ec70be4afd62f91c1fa9a99d1731e2ae934ebc2
7
- data.tar.gz: ae47e8e2b78ec48f76b922bb4d429388b621714c5c14beb6ed89da54568ed2cac5474939928111a5b8d672319ada35f06e5bd05537c2434e3d752157ce1ea2a9
6
+ metadata.gz: 1c17592015bf5060d0cafa50227a6c8f3f6a97bef71d8993e1915e1a2a6b0e118bb54f26f5ef0f509d631ca8938fccfb3465fcc3561829dd8e30db6652989f5f
7
+ data.tar.gz: 11fe31137e6468adafe4daecfbefd91c9a246489bcf02f483c1066e41f9a6a343849cd43e0f080187cb6d4eb5fdf924bad0c7d03e7e653c25de7928645856c72
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # main module that includes all basic data and methods
3
5
  module Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'phonelib/data_importer_helper'
2
4
 
3
5
  module Phonelib
@@ -126,6 +128,9 @@ module Phonelib
126
128
  if country[Core::NATIONAL_PREFIX_TRANSFORM_RULE]
127
129
  country[Core::NATIONAL_PREFIX_TRANSFORM_RULE].gsub!('$', '\\')
128
130
  end
131
+ if country[:id] == '001'
132
+ country[:id] = 'International ' + country[:country_code]
133
+ end
129
134
  @data[country[:id]] = country
130
135
  end
131
136
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # @private helper module for parsing raw libphonenumber data
3
5
  module DataImporterHelper
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # class for parsed phone number, includes validation and formatting methods
3
5
  class Phone
@@ -24,7 +26,7 @@ module Phonelib
24
26
  # @return [Phonelib::Phone] parsed phone instance
25
27
  def initialize(phone, country = nil)
26
28
  @original, @extension = separate_extension(phone.to_s)
27
- @extension.gsub!(/[^0-9]/, '') if @extension
29
+ @extension = @extension.gsub(/[^0-9]/, '') if @extension
28
30
 
29
31
  if sanitized.empty?
30
32
  @data = {}
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # @private phone analyzing methods module
3
5
  module PhoneAnalyzer
@@ -139,7 +141,7 @@ module Phonelib
139
141
  end
140
142
 
141
143
  def country_code_candidates_for(phone)
142
- stripped_phone = phone.gsub(/^(#{Phonelib.phone_data_int_prefixes})/, '')
144
+ stripped_phone = phone.gsub(cr("Phonelib.phone_data_int_prefixes") { /^(#{Phonelib.phone_data_int_prefixes})/ }, '')
143
145
  ((1..3).map { |length| phone[0, length] } + (1..3).map { |length| stripped_phone[0, length] }).uniq
144
146
  end
145
147
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # @private helper methods for analyser
3
5
  module PhoneAnalyzerHelper
@@ -52,7 +54,7 @@ module Phonelib
52
54
 
53
55
  # caches regular expression, reusing it for later lookups
54
56
  def cr(regexp)
55
- Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(regexp).freeze
57
+ Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(block_given? ? yield(regexp) : regexp).freeze
56
58
  end
57
59
 
58
60
  # defines whether country can have double country prefix in number
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # module provides extended data methods for parsed phone
3
5
  module PhoneExtendedData
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # module includes all formatting methods
3
5
  module PhoneFormatter
@@ -159,7 +161,7 @@ module Phonelib
159
161
  data[Core::NATIONAL_PREFIX_RULE] || '$1'
160
162
 
161
163
  # change rule's constants to values
162
- rule.gsub!(/(\$NP|\$FG)/, '$NP' => prefix, '$FG' => '$1')
164
+ rule = rule.gsub(/(\$NP|\$FG)/, '$NP' => prefix, '$FG' => '$1')
163
165
 
164
166
  # add space to format groups, change first group to rule,
165
167
  format_string = format[:format].gsub(/(\d)\$/, '\\1 $')
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phonelib
2
4
  # @private
3
- VERSION = '0.9.3'
5
+ VERSION = '0.10.1'
4
6
  end
data/lib/phonelib.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # main Phonelib module definition
2
4
  module Phonelib
3
5
  # load phonelib classes/modules
@@ -12,11 +14,7 @@ module Phonelib
12
14
  end
13
15
 
14
16
  if defined?(ActiveModel) || defined?(Rails)
15
- if RUBY_VERSION >= '3.0.0'
16
- autoload :PhoneValidator, 'validators/phone_validator3'
17
- else
18
- autoload :PhoneValidator, 'validators/phone_validator'
19
- end
17
+ autoload :PhoneValidator, 'validators/phone_validator'
20
18
 
21
19
  if defined?(Rails)
22
20
  class Phonelib::Railtie < Rails::Railtie
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  namespace :phonelib do
2
4
  desc 'Create database for tests in Rails dummy application'
3
5
  task :create_test_db do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Validator class for phone validations
2
4
  #
3
5
  # ==== Examples
metadata CHANGED
@@ -1,155 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phonelib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-27 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "<"
18
- - !ruby/object:Gem::Version
19
- version: '14.0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "<"
25
- - !ruby/object:Gem::Version
26
- version: '14.0'
27
- - !ruby/object:Gem::Dependency
28
- name: nokogiri
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.15'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.15'
41
- - !ruby/object:Gem::Dependency
42
- name: pry
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - '='
60
- - !ruby/object:Gem::Version
61
- version: 2.14.1
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - '='
67
- - !ruby/object:Gem::Version
68
- version: 2.14.1
69
- - !ruby/object:Gem::Dependency
70
- name: codeclimate-test-reporter
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.0.9
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.0.9
83
- - !ruby/object:Gem::Dependency
84
- name: simplecov
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
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'
125
- - !ruby/object:Gem::Dependency
126
- name: rack-cache
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - '='
130
- - !ruby/object:Gem::Version
131
- version: '1.2'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - '='
137
- - !ruby/object:Gem::Version
138
- version: '1.2'
139
- - !ruby/object:Gem::Dependency
140
- name: json
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - '='
144
- - !ruby/object:Gem::Version
145
- version: 2.3.1
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - '='
151
- - !ruby/object:Gem::Version
152
- version: 2.3.1
11
+ date: 2024-11-20 00:00:00.000000000 Z
12
+ dependencies: []
153
13
  description: |2
154
14
  Google libphonenumber library was taken as a basis for
155
15
  this gem. Gem uses its data file for validations and number formatting.
@@ -176,11 +36,11 @@ files:
176
36
  - lib/phonelib/version.rb
177
37
  - lib/tasks/phonelib_tasks.rake
178
38
  - lib/validators/phone_validator.rb
179
- - lib/validators/phone_validator3.rb
180
39
  homepage: https://github.com/daddyz/phonelib
181
40
  licenses:
182
41
  - MIT
183
- metadata: {}
42
+ metadata:
43
+ changelog_uri: https://github.com/daddyz/phonelib/releases/tag/v0.10.1
184
44
  post_install_message:
185
45
  rdoc_options:
186
46
  - " --no-private - CHANGELOG.md --readme README.md"
@@ -1,130 +0,0 @@
1
- # Validator class for phone validations
2
- #
3
- # ==== Examples
4
- #
5
- # Validates that attribute is a valid phone number.
6
- # If empty value passed for attribute it fails.
7
- #
8
- # class Phone < ActiveRecord::Base
9
- # attr_accessible :number
10
- # validates :number, phone: true
11
- # end
12
- #
13
- # Validates that attribute is a possible phone number.
14
- # If empty value passed for attribute it fails.
15
- #
16
- # class Phone < ActiveRecord::Base
17
- # attr_accessible :number
18
- # validates :number, phone: { possible: true }
19
- # end
20
- #
21
- # Validates that attribute is a valid phone number.
22
- # Empty value is allowed to be passed.
23
- #
24
- # class Phone < ActiveRecord::Base
25
- # attr_accessible :number
26
- # validates :number, phone: { allow_blank: true }
27
- # end
28
- #
29
- # Validates that attribute is a valid phone number of specified type(s).
30
- # It is also possible to check that attribute is a possible number of specified
31
- # type(s). Symbol or array accepted.
32
- #
33
- # class Phone < ActiveRecord::Base
34
- # attr_accessible :number, :mobile
35
- # validates :number, phone: { types: [:mobile, :fixed], allow_blank: true }
36
- # validates :mobile, phone: { possible: true, types: :mobile }
37
- # end
38
- #
39
- # validates that phone is valid and it is from specified country or countries
40
- #
41
- # class Phone < ActiveRecord::Base
42
- # attr_accessible :number
43
- # validates :number, phone: { countries: [:us, :ca] }
44
- # end
45
- #
46
- # Validates that attribute does not include an extension.
47
- # The default setting is to allow extensions
48
- #
49
- # class Phone < ActiveRecord::Base
50
- # attr_accessible :number
51
- # validates :number, phone: { extensions: false }
52
- # end
53
- #
54
- class PhoneValidator < ActiveModel::EachValidator
55
- # Include all core methods
56
- include Phonelib::Core
57
-
58
- # Validation method
59
- def validate_each(record, attribute, value)
60
- return if options[:allow_blank] && value.blank?
61
-
62
- @phone = parse(value, specified_country(record))
63
- valid = phone_valid? && valid_types? && valid_country? && valid_extensions?
64
-
65
- record.errors.add(attribute, message, **options) unless valid
66
- end
67
-
68
- private
69
-
70
- def message
71
- options[:message] || :invalid
72
- end
73
-
74
- def phone_valid?
75
- @phone.send(options[:possible] ? :possible? : :valid?)
76
- end
77
-
78
- def valid_types?
79
- return true unless options[:types]
80
- (phone_types & types).size > 0
81
- end
82
-
83
- def valid_country?
84
- return true unless options[:countries]
85
- (phone_countries & countries).size > 0
86
- end
87
-
88
- def valid_extensions?
89
- return true if !options.has_key?(:extensions) || options[:extensions]
90
- @phone.extension.empty?
91
- end
92
-
93
- def specified_country(record)
94
- return unless options[:country_specifier]
95
-
96
- if options[:country_specifier].is_a?(Symbol)
97
- record.send(options[:country_specifier])
98
- else
99
- options[:country_specifier].call(record)
100
- end
101
- end
102
-
103
- # @private
104
- def phone_types
105
- method = options[:possible] ? :possible_types : :types
106
- phone_types = @phone.send(method)
107
- if (phone_types & [Phonelib::Core::FIXED_OR_MOBILE]).size > 0
108
- phone_types += [Phonelib::Core::FIXED_LINE, Phonelib::Core::MOBILE]
109
- end
110
- phone_types
111
- end
112
-
113
- # @private
114
- def phone_countries
115
- method = options[:possible] ? :countries : :valid_countries
116
- @phone.send(method)
117
- end
118
-
119
- # @private
120
- def types
121
- types = options[:types].is_a?(Array) ? options[:types] : [options[:types]]
122
- types.map(&:to_sym)
123
- end
124
-
125
- # @private
126
- def countries
127
- countries = options[:countries].is_a?(Array) ? options[:countries] : [options[:countries]]
128
- countries.map { |c| c.to_s.upcase }
129
- end
130
- end