/
validators.rb
77 lines (60 loc) · 1.68 KB
/
validators.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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