phonelib 0.6.21 → 0.6.22

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
  SHA1:
3
- metadata.gz: 340a5c0487adffe92db6fbba129f1d79d669e3ca
4
- data.tar.gz: 58b742b98ed74a4a8b47573f30b0eb4e4963e4ab
3
+ metadata.gz: 73b3d8a14f0b8759fd8b6bef0535a46b7a9d0731
4
+ data.tar.gz: ae495d47f1ad7b0d6c2cd28f54ce665cef40fc22
5
5
  SHA512:
6
- metadata.gz: 935c0feb67ce471cba7f9e2c4046deba98ea3800e5bbd864ef227ec5503a4fd1b8317d8a00cac190e02c8f161c9c72191f44b4c79129f1436ee2d4d7a7136cf7
7
- data.tar.gz: 5377cf7fa504d3538163f7eac8142e8729620c0f558a48f3488e93a4a5ddd786061e2366028b21d525f16d68145b3c295be1dc8a243e11b068c1c3b5555a71f9
6
+ metadata.gz: da7be7fee93026ad690e049a1e5afe825222756f82c9770a3918d1ef9e31f937c17e5af1fc0377c34b7b97da6a1b9296dfd33e4f4588bf634ca95a453a07a1c4
7
+ data.tar.gz: 1254c4b20dbdd079151f4bbbf0378192ba0b80d5af02dc9c5f20bc9b6549712bb82d3f596406817b9ff51550fc0192ab01e9051192904f20f41fb29c5dcc2058
data/README.md CHANGED
@@ -109,6 +109,9 @@ Refer to [Google libphonenumber](http://code.google.com/p/libphonenumber/) for m
109
109
  <tt>types: :mobile</tt> or <tt>types: [:voip, :mobile]</tt> - allows to validate against specific phone types patterns,
110
110
  if mixed with <tt>possible</tt> will check if number is possible for specified type
111
111
 
112
+ <tt>countries: :us</tt> or <tt>countries: [:us, :ca]</tt> - allows to validate against specific countries,
113
+ if mixed with <tt>possible</tt> will check if number is possible for specified countries
114
+
112
115
  <tt>country_specifier: -> phone { phone.country.try(:upcase) }</tt> - allows to specify country for validation dynamically for each validation.
113
116
 
114
117
  <tt>extensions: false</tt> - set to perform check for phone extension to be blank
Binary file
Binary file
@@ -18,7 +18,7 @@ module Phonelib
18
18
  include Phonelib::DataImporterHelper
19
19
 
20
20
  # countries that can have double country prefix in number
21
- DOUBLE_COUNTRY_CODES_COUNTRIES = %w(IN DE BR IT NO)
21
+ DOUBLE_COUNTRY_CODES_COUNTRIES = %w(IN DE BR IT NO PL)
22
22
 
23
23
  # main data file in repo
24
24
  MAIN_FILE = 'resources/PhoneNumberMetadata.xml'
@@ -1,4 +1,4 @@
1
1
  module Phonelib
2
2
  # @private
3
- VERSION = '0.6.21'
3
+ VERSION = '0.6.22'
4
4
  end
@@ -36,6 +36,13 @@
36
36
  # validates :mobile, phone: { possible: true, types: :mobile }
37
37
  # end
38
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
+ #
39
46
  # Validates that attribute does not include an extension.
40
47
  # The default setting is to allow extensions
41
48
  #
@@ -51,19 +58,9 @@ class PhoneValidator < ActiveModel::EachValidator
51
58
  # Validation method
52
59
  def validate_each(record, attribute, value)
53
60
  return if options[:allow_blank] && value.blank?
54
- allowed_extensions = options.has_key?(:extensions) ? options[:extensions] : true
55
61
 
56
- phone = parse(value, specified_country(record))
57
- valid = if simple_validation?
58
- phone.send(validate_method)
59
- else
60
- (phone_types(phone) & types).size > 0
61
- end
62
-
63
- # We default to not-allowing extensions for fax numbers
64
- if valid && !allowed_extensions && !phone.extension.empty?
65
- valid = false
66
- end
62
+ @phone = parse(value, specified_country(record))
63
+ valid = phone_valid? && valid_types? && valid_country? && valid_extensions?
67
64
 
68
65
  record.errors.add(attribute, message, options) unless valid
69
66
  end
@@ -74,8 +71,23 @@ class PhoneValidator < ActiveModel::EachValidator
74
71
  options[:message] || :invalid
75
72
  end
76
73
 
77
- def validate_method
78
- options[:possible] ? :possible? : :valid?
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?
79
91
  end
80
92
 
81
93
  def specified_country(record)
@@ -84,24 +96,30 @@ class PhoneValidator < ActiveModel::EachValidator
84
96
  end
85
97
 
86
98
  # @private
87
- def simple_validation?
88
- options[:types].nil?
89
- end
90
-
91
- # @private
92
- # @param phone [Phonelib::Phone] parsed phone
93
- def phone_types(phone)
99
+ def phone_types
94
100
  method = options[:possible] ? :possible_types : :types
95
- phone_types = phone.send(method)
101
+ phone_types = @phone.send(method)
96
102
  if (phone_types & [Phonelib::Core::FIXED_OR_MOBILE]).size > 0
97
103
  phone_types += [Phonelib::Core::FIXED_LINE, Phonelib::Core::MOBILE]
98
104
  end
99
105
  phone_types
100
106
  end
101
107
 
108
+ # @private
109
+ def phone_countries
110
+ method = options[:possible] ? :countries : :valid_countries
111
+ @phone.send(method)
112
+ end
113
+
102
114
  # @private
103
115
  def types
104
116
  types = options[:types].is_a?(Array) ? options[:types] : [options[:types]]
105
117
  types.map(&:to_sym)
106
118
  end
119
+
120
+ # @private
121
+ def countries
122
+ countries = options[:countries].is_a?(Array) ? options[:countries] : [options[:countries]]
123
+ countries.map { |c| c.to_s.upcase }
124
+ end
107
125
  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.21
4
+ version: 0.6.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-12 00:00:00.000000000 Z
11
+ date: 2018-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  requirements: []
171
171
  rubyforge_project:
172
- rubygems_version: 2.6.11
172
+ rubygems_version: 2.6.14
173
173
  signing_key:
174
174
  specification_version: 4
175
175
  summary: Gem validates phone numbers with Google libphonenumber database