Browse files

+ 1.6.7, improved experimental validation

  • Loading branch information...
1 parent 3bbddb8 commit f1dfaefb71309941b31f289e1edf05b1b195350f @floere committed Mar 20, 2012
Showing with 52 additions and 9 deletions.
  1. +4 −0 history.textile
  2. +3 −3 lib/phony.rb
  3. +5 −1 lib/phony/countries.rb
  4. +1 −1 lib/phony/country_codes.rb
  5. +2 −2 lib/phony/validators.rb
  6. +1 −1 phony.gemspec
  7. +36 −1 spec/lib/phony/validations_spec.rb
View
4 history.textile
@@ -1,5 +1,9 @@
h2. Upcoming Version.
+h2. Version 1.6.7
+
+* hanke: Improvements to the experimental @plausible?(number, hints = {})@ feature. It is now possible to give the hints @cc@ and @ndc@ a regexp, as in @Phony.plausible?("1-434-123-1234", cc: /1|7/, ndc: /4(34|35)/)@.
+
h2. Version 1.6.6
* hanke: Experimental @plausible?@ feature. Checks if the given number is a plausible number. Returns @false@ if 100% not plausible, @true@ if probably true.
View
6 lib/phony.rb
@@ -39,8 +39,8 @@ module Phony
# Phony uses a single country codes instance.
#
- @codes = CountryCodes.instance
- @validator = Validators.instance
+ @codes = CountryCodes.instance
+ @validators = Validators.instance
class << self
@@ -85,7 +85,7 @@ def format! phone_number, options = {}
# leaning towards being plausible.
#
def plausible? number, hints = {}
- @validator.plausible? number, hints
+ @validators.plausible? number, hints
end
# def service? number
View
6 lib/phony/countries.rb
@@ -17,6 +17,10 @@
# * matched_split: Give a hash of regex => format array, with a :fallback => format option.
# (See Norway how it looks.)
#
+# The third parameter to country are validations.
+# Currently, there is one method available:
+# * invalid_ndcs: Give a regexp or string to describe invalid ndc(s).
+#
# Note: The ones that are commented are defined in their special files.
#
Phony.define do
@@ -29,7 +33,7 @@
#
country '1',
fixed(3) >> split(3,4),
- invalid_ndcs(/911/)
+ invalid_ndcs('911') # /911/ would also work.
# Kazakhstan (Republic of) & Russian Federation.
#
View
2 lib/phony/country_codes.rb
@@ -26,7 +26,7 @@ def self.instance
def clean number
# Remove non-digit chars.
#
- number.gsub! @@basic_normalizing_pattern, EMPTY_STRING
+ number.gsub!(@@basic_normalizing_pattern, EMPTY_STRING) || number
end
def normalize number
clean number
View
4 lib/phony/validators.rb
@@ -49,12 +49,12 @@ def plausible? number, hints = {}
# CC.
#
cc_needed = hints[:cc]
- return false if cc_needed && cc_needed != cc
+ return false if cc_needed && !(cc_needed === cc)
# NDC.
#
ndc_needed = hints[:ndc]
- return false if ndc_needed && ndc_needed != ndc
+ return false if ndc_needed && !(ndc_needed === ndc)
# Country specific checks.
#
View
2 phony.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'phony'
- s.version = '1.6.6'
+ s.version = '1.6.7'
s.authors = ['Florian Hanke']
s.email = 'florian.hanke+phony@gmail.com'
s.homepage = 'http://github.com/floere/phony'
View
37 spec/lib/phony/validations_spec.rb
@@ -28,12 +28,47 @@
it "is correct for explicit checks" do
Phony.plausible?('+41 44 111 22 33', ndc: '43').should be_false
end
+ it "is correct for explicit checks" do
+ Phony.plausible?('+41 44 111 22 33', cc: '41', ndc: '44').should be_true
+ end
+ it "works with regexps" do
+ Phony.plausible?('+41 44 111 22 33', cc: /4(0|2)/, ndc: /4(4|5)/).should be_false
+ end
+ it "works with regexps" do
+ Phony.plausible?('+41 44 111 22 33', cc: /4(0|1)/, ndc: /4(4|5)/).should be_true
+ end
- context 'countries' do
+ context 'specific countries' do
+
+ # TODO
+ #
+ # it "is correct for Swiss numbers" do
+ # Phony.plausible?('+41 44 111 22 33').should be_true
+ # end
it "is correct for US numbers" do
+ # Sorry, still need E164 conform numbers.
+ #
+ Phony.plausible?('4346667777', cc: '1').should be_false
+
+ # Automatic country checking.
+ #
+ Phony.plausible?('1-4346667777').should be_true
Phony.plausible?('1-800-692-7753').should be_true
Phony.plausible?('1-911').should be_false
+ Phony.plausible?('1-911-123-1234').should be_false
+
+ # With string constraints.
+ #
+ Phony.plausible?('14346667777', cc: '1').should be_true
+ Phony.plausible?('14346667777', ndc: '434').should be_true
+ Phony.plausible?('14346667777', cc: '1', ndc: '434').should be_true
+
+ # With regexp constraints.
+ #
+ Phony.plausible?('14346667777', cc: /[123]/).should be_true
+ Phony.plausible?('14346667777', ndc: /434|435/).should be_true
+ Phony.plausible?('14346667777', cc: /[123]/, ndc: /434|435/).should be_true
end
end

0 comments on commit f1dfaef

Please sign in to comment.