phonelib 0.6.43 → 0.6.48

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: 5bf3cdd4ff69ed33302d75b80561fefc1c7ab061a4e7331d7353e421aa82cb3f
4
- data.tar.gz: aa1529c76dbc03a343f963c07e510c89fe06734e6052adbfc0bc1d091a43ecf2
3
+ metadata.gz: 9ab20ada741b6c807d528b1cd032b4e20ebfbde1bcc8fcd6eeeb7b351ddec3ed
4
+ data.tar.gz: cc73a6a56939c0ba09f8996fe17a6050db96af0c5e52b6a7b46c8c55bbf9869d
5
5
  SHA512:
6
- metadata.gz: 8ac94a66b16c7839ba921e18bb2940ad018551f1f63cb109d5a43f896b00cef69689d7b8aecff856d6a02dbff0bf135e33793564c020c9b7c6290d412c92f910
7
- data.tar.gz: af4513a6df820380d25282405e47267e5e39380c0ce683d121433467fac4f46573bce8606d2f549a14d8f9ca46cda73b7da6b6c1031d643b92e3a4de08d41f98
6
+ metadata.gz: 54375aea5dd1cf1ddd0d3c51d7ac9a6db4b745e152ef5d1a11c45571ef7f4bfb579e1021a1a9c13dc83ba77e39cca12e19904aba71f632456d15ec1d8e9ec97a
7
+ data.tar.gz: 85495f7efe0e2f9da9adbbe9b4b4f2e8219fc5f6842ae473a81626c9f8629557351ff025a98794f95ee79c1d661bf4f39ff5c3f4191db36d21d2c14849252d9c
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Built in integration with JetBrains RubyMine](https://github.com/daddyz/phonelib/blob/master/icon_RubyMine.png?raw=true)](https://www.jetbrains.com/ruby/)
4
4
  [![Gem Version](https://badge.fury.io/rb/phonelib.svg)](http://badge.fury.io/rb/phonelib)
5
- [![Build Status](https://travis-ci.org/daddyz/phonelib.png?branch=master)](http://travis-ci.org/daddyz/phonelib)
5
+ [![Build Status](https://travis-ci.com/daddyz/phonelib.svg?branch=master)](http://travis-ci.com/daddyz/phonelib)
6
6
  [![](https://codeclimate.com/github/daddyz/phonelib/badges/coverage.svg)](https://codeclimate.com/github/daddyz/phonelib/coverage)
7
7
  [![](https://codeclimate.com/github/daddyz/phonelib/badges/gpa.svg)](https://codeclimate.com/github/daddyz/phonelib)
8
8
  [![Inline docs](http://inch-ci.org/github/daddyz/phonelib.svg?branch=master)](http://inch-ci.org/github/daddyz/phonelib)
@@ -61,6 +61,12 @@ To disable sanitizing of passed phone number (keeping digits only)
61
61
  Phonelib.strict_check = true
62
62
  ```
63
63
 
64
+ To change sanitized symbols on parsed number, so non-specified symbols won't be wiped and will fail the parsing
65
+
66
+ ``` ruby
67
+ Phonelib.sanitize_regex = '[\.\-\(\) \;\+]'
68
+ ```
69
+
64
70
  To disable sanitizing of double prefix on passed phone number
65
71
 
66
72
  ```ruby
data/Rakefile CHANGED
@@ -22,6 +22,13 @@ end
22
22
  Bundler::GemHelper.install_tasks
23
23
 
24
24
  require 'rspec/core/rake_task'
25
+ module TempFixForRakeLastComment
26
+ def last_comment
27
+ last_description
28
+ end
29
+ end
30
+ Rake::Application.send :include, TempFixForRakeLastComment
31
+
25
32
  RSpec::Core::RakeTask.new(:spec) do |t|
26
33
  if defined? Rails
27
34
  puts 'Rails found! Running tests with Rails'
Binary file
Binary file
@@ -12,5 +12,9 @@ module Phonelib
12
12
  end
13
13
 
14
14
  if defined?(ActiveModel) || defined?(Rails)
15
- autoload :PhoneValidator, 'validators/phone_validator'
15
+ if RUBY_VERSION >= '3.0.0'
16
+ autoload :PhoneValidator, 'validators/phone_validator3'
17
+ else
18
+ autoload :PhoneValidator, 'validators/phone_validator'
19
+ end
16
20
  end
@@ -107,6 +107,22 @@ module Phonelib
107
107
  @@strict_check = strict
108
108
  end
109
109
 
110
+ # @private sanitizing regex, matching symbols will get removed from parsed number, must be string
111
+ @@sanitize_regex = '[^0-9]+'
112
+
113
+ # getter for sanitize regex
114
+ # @return [String] regex of symbols to wipe from parsed number
115
+ def sanitize_regex
116
+ @@sanitize_regex
117
+ end
118
+
119
+ # setter for sanitize regex
120
+ # @param regex [String] symbols to wipe from parsed number
121
+ # @return [String] regex of symbols to wipe from parsed number
122
+ def sanitize_regex=(regex)
123
+ @@sanitize_regex = regex.is_a?(String) ? regex : regex.to_s
124
+ end
125
+
110
126
  # @private strict double prefix check for validator, doesn't sanitize number
111
127
  @@strict_double_prefix_check = false
112
128
 
@@ -145,10 +145,10 @@ module Phonelib
145
145
 
146
146
  require 'open-uri'
147
147
  require 'csv'
148
- io = open('http://download.geonames.org/export/dump/countryInfo.txt')
148
+ io = open('http://api.geonames.org/countryInfoCSV?username=demo&style=full')
149
149
  csv = CSV.new(io, {col_sep: "\t"})
150
150
  csv.each do |row|
151
- next if row[0].start_with?('#') || row[0].empty?
151
+ next if row[0].nil? || row[0].start_with?('#') || row[0].empty?
152
152
 
153
153
  @countries[row[0]] = row[4]
154
154
  end
@@ -51,7 +51,7 @@ module Phonelib
51
51
  def sanitized
52
52
  @sanitized ||=
53
53
  vanity_converted(@original).gsub(
54
- Phonelib.strict_check ? cr('^\+') : cr('[^0-9]+'),
54
+ Phonelib.strict_check ? cr('^\+') : cr(Phonelib.sanitize_regex),
55
55
  '')
56
56
  end
57
57
 
@@ -44,7 +44,7 @@ module Phonelib
44
44
  prefix = formatted if formatted.is_a?(String)
45
45
  return nil if sanitized.empty?
46
46
  return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless valid?
47
- return country_code + @national_number unless formatted
47
+ return "#{prefix}#{country_code}#{@national_number}" unless formatted
48
48
 
49
49
  fmt = @data[country][:format]
50
50
  national = @national_number
@@ -1,4 +1,4 @@
1
1
  module Phonelib
2
2
  # @private
3
- VERSION = '0.6.43'
3
+ VERSION = '0.6.48'
4
4
  end
@@ -0,0 +1,130 @@
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
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.6.43
4
+ version: 0.6.48
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-07 00:00:00.000000000 Z
11
+ date: 2021-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "<"
18
18
  - !ruby/object:Gem::Version
19
- version: '11.0'
19
+ version: '14.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "<"
25
25
  - !ruby/object:Gem::Version
26
- version: '11.0'
26
+ version: '14.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: nokogiri
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.10.4
33
+ version: 1.10.8
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.10.4
40
+ version: 1.10.8
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - '='
116
116
  - !ruby/object:Gem::Version
117
- version: 1.8.6
117
+ version: 2.3.1
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - '='
123
123
  - !ruby/object:Gem::Version
124
- version: 1.8.6
124
+ version: 2.3.1
125
125
  description: |2
126
126
  Google libphonenumber library was taken as a basis for
127
127
  this gem. Gem uses its data file for validations and number formatting.
@@ -148,6 +148,7 @@ files:
148
148
  - lib/phonelib/version.rb
149
149
  - lib/tasks/phonelib_tasks.rake
150
150
  - lib/validators/phone_validator.rb
151
+ - lib/validators/phone_validator3.rb
151
152
  homepage: https://github.com/daddyz/phonelib
152
153
  licenses:
153
154
  - MIT
@@ -168,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
169
  - !ruby/object:Gem::Version
169
170
  version: '0'
170
171
  requirements: []
171
- rubygems_version: 3.0.6
172
+ rubygems_version: 3.0.9
172
173
  signing_key:
173
174
  specification_version: 4
174
175
  summary: Gem validates phone numbers with Google libphonenumber database