forked from floere/phony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ experimental 1.6.6 release of plausibility checking
- Loading branch information
Showing
10 changed files
with
177 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# # Number: A possible phone number, E164 or not. | ||
# # Hints: Information that helps or constricts the plausibility check. | ||
# # | ||
# plausible? number, hints = {} | ||
# | ||
|
||
# plausible? number # Uses the definitions from the country definition to plausibility check. | ||
# plausible? number, cc: 1 # => Checks cc. | ||
# plausible? number, pattern: /[^5]/ # Uses def, checks against split. | ||
# plausible? number, country: 1, pattern: [3, 4, 3] # Uses given country – adds cc. | ||
# | ||
|
||
# Basic plausibility is: | ||
# * Max digits are 15. | ||
# * Min digits are 2 (?) | ||
# | ||
|
||
module Phony | ||
|
||
class Validators | ||
|
||
def initialize | ||
@validators = {} | ||
end | ||
|
||
def self.instance | ||
@instance ||= new | ||
end | ||
|
||
# Add a specific country validator. | ||
# | ||
def add cc, validator | ||
@validators[cc] = validator | ||
end | ||
|
||
# Is the given number plausible? | ||
# | ||
def plausible? number, hints = {} | ||
normalized = CountryCodes.instance.clean number | ||
|
||
# False if it fails the basic check. | ||
# | ||
return false unless (2..15) === normalized.size | ||
|
||
# Hint based checking. | ||
# | ||
cc, ndc, *rest = Phony.split normalized | ||
|
||
# CC. | ||
# | ||
cc_needed = hints[:cc] | ||
return false if cc_needed && cc_needed != cc | ||
|
||
# NDC. | ||
# | ||
ndc_needed = hints[:ndc] | ||
return false if ndc_needed && ndc_needed != ndc | ||
|
||
# Country specific checks. | ||
# | ||
validator = validator_for cc | ||
validator.plausible? ndc, rest | ||
rescue StandardError | ||
return false | ||
end | ||
|
||
def validator_for cc | ||
@validators[cc] || default_validator | ||
end | ||
|
||
def default_validator | ||
@default_validator ||= Validator.new | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'phony' | ||
s.version = '1.6.5' | ||
s.version = '1.6.6' | ||
s.authors = ['Florian Hanke'] | ||
s.email = '[email protected]' | ||
s.homepage = 'http://github.com/floere/phony' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# encoding: utf-8 | ||
# | ||
require 'spec_helper' | ||
|
||
describe 'validations' do | ||
|
||
describe 'plausible?' do | ||
|
||
it "is correct" do | ||
Phony.plausible?('0000000').should be_false | ||
end | ||
it "is correct" do | ||
Phony.plausible?('hello').should be_false | ||
end | ||
|
||
it "is correct" do | ||
Phony.plausible?('+41 44 111 22 33').should be_true | ||
end | ||
it "is correct for explicit checks" do | ||
Phony.plausible?('+41 44 111 22 33', cc: '41').should be_true | ||
end | ||
it "is correct for explicit checks" do | ||
Phony.plausible?('+41 44 111 22 33', ndc: '44').should be_true | ||
end | ||
it "is correct for explicit checks" do | ||
Phony.plausible?('+41 44 111 22 33', cc: '1').should be_false | ||
end | ||
it "is correct for explicit checks" do | ||
Phony.plausible?('+41 44 111 22 33', ndc: '43').should be_false | ||
end | ||
|
||
context 'countries' do | ||
|
||
it "is correct for US numbers" do | ||
Phony.plausible?('1-800-692-7753').should be_true | ||
Phony.plausible?('1-911').should be_false | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters