Permalink
Browse files

+ Mid rewrite 2

  • Loading branch information...
1 parent 8836b36 commit 3551b1e2d805b151736f09ad9ec82eb474f197ef @floere committed Mar 24, 2013
View
12 lib/phony.rb
@@ -99,19 +99,9 @@ def format! phone_number, options = {}
# leaning towards being plausible.
#
def plausible? number, hints = {}
- @validators.plausible? number, hints
+ @codes.plausible? number, hints
end
- # def service? number
- # @codes.service? number.dup
- # end
- # def mobile? number
- # @codes.mobile? number.dup
- # end
- # def landline? number
- # @codes.landline? number.dup
- # end
-
# Returns true if there is a character in the number
# after the first four numbers.
#
View
5 lib/phony/countries.rb
@@ -46,9 +46,8 @@
#
country '20', one_of('800') >> split(7) | # Egypt
one_of('2', '3') >> split(8) | # Cairo/Giza, Alexandria
- fixed(2) >> split(8),
+ fixed(2) >> split(8)
# :mobile? => /^1[01246-9]\d+$/, :service? => /^800\d+$/
- length( 3 => 10, 1 => 9, 2 => 10 )
# South Africa.
#
@@ -81,8 +80,6 @@
# country '36' # Hungary, see special file..
-
-
# country '39' # Italy, see special file.
# Romania.
View
31 lib/phony/country.rb
@@ -54,11 +54,32 @@ def normalize national_number
#
#
- def plausible? ndc, rest
- @codes.each do |code|
- zero, ndc, *rest = code.split national_number
- return [zero, ndc, *rest] if rest && !rest.empty?
- end
+ def plausible? rest, hints = {}
+ local, _, ndc, *rest = split_ndc rest
+
+ # Element based checking.
+ #
+ # Note: ndc == false means the country has none.
+ #
+ return false if ndc.nil?
+ return false if ndc && ndc.empty?
+
+ rest_size = rest.inject(0) { |total, part| total + part.size } # ndc ? rest.size - ndc.size : rest.size
+
+ # A valid range for the rest is 0 or 3+ total digits.
+ #
+ return false if (1..2) === rest_size
+
+ # National destination code plausible?
+ #
+ ndc_needed = hints[:ndc]
+ return false if ndc_needed && !(ndc_needed === ndc)
+
+ p [ndc, rest, rest_size]
+
+ # Local code specific checks.
+ #
+ return local.plausible? rest, rest_size, hints
end
# Is this national number a vanity number?
View
27 lib/phony/country_codes.rb
@@ -112,7 +112,10 @@ def vanity_to_number vanity_number
country_handler, cc, rest = split_cc vanity_number
"#{cc}#{country_handler.vanity_to_number(rest)}"
end
-
+
+ # TODO Rewrite this to just find the country handler and
+ # then call a method on the country handler?
+ #
def split_cc rest
presumed_cc = ''
1.upto(3) do |i|
@@ -122,6 +125,28 @@ def split_cc rest
end
# This line is never reached as CCs are in prefix code.
end
+
+ def plausible? number, hints = {}
+ normalized = clean number
+
+ # False if it fails the basic check.
+ #
+ return false unless (4..15) === normalized.size
+
+ country, cc, rest = split_cc normalized
+
+ # Country code plausible?
+ #
+ cc_needed = hints[:cc]
+ return false if cc_needed && !(cc_needed === cc)
+
+ # Country specific tests.
+ #
+ country.plausible? rest, hints
+ rescue StandardError => e
+ puts e.message
+ return false
+ end
# # TODO
# #
View
11 lib/phony/dsl.rb
@@ -51,13 +51,6 @@ class DSL
#
def country country_code, definition, *validators
Phony::CountryCodes.instance.add country_code, definition
- validators.each do |validator|
- # codes = definition.codes
- # codes.each do |code|
- # p [code.instance_variable_get(:@national_splitter).length, code.instance_variable_get(:@local_splitter).length]
- # end
- Phony::Validators.instance.add country_code, validator
- end
end
# National matcher & splitters.
@@ -179,8 +172,8 @@ def matched_split options = {}
# fixed(3, :zero => false) >> split(3,4),
# invalid_ndcs('911') # The regexp /911/ would also work.
#
- def invalid_ndcs ndc
- Validators::NDC.new.invalid ndc
+ def invalid_ndcs *ndc
+ Validators::NDC.new invalid: ndc
end
# checks to see, if number is exactly length
View
11 lib/phony/local_splitters/fixed.rb
@@ -27,7 +27,8 @@ def self.instance_for format = nil
# The format [3, 2, 2] splits a number like '3332222' into ['333', '22', '22'].
#
def initialize format = nil
- @format = format || [3, 2, 2]
+ @format = format && format.dup || [3, 2, 2]
+ # @format << @format.pop + 10 # Allow for call-through numbers with an arbitrary size.
end
# Split a local number according to an assumed country specific format.
@@ -43,6 +44,14 @@ def split number
end
end
+ # TODO
+ #
+ def plausible? rest, size, hints = {}
+ p [rest, size, length - 10]
+
+ return (length - 10) === size
+ end
+
# A valid length.
#
def length
View
8 lib/phony/national_splitters/default.rb
@@ -1,7 +1,9 @@
module Phony
module NationalSplitters
-
+
+ # TODO Default = Fixed.new(...)?
+ #
class Default < DSL
def self.instance_for
@@ -12,6 +14,10 @@ def split national_number
[national_number]
end
+ def plausible? rest, size, hints = {}
+ true
+ end
+
# A valid length.
#
def length
View
7 lib/phony/validators.rb
@@ -75,9 +75,10 @@ def plausible? number, hints = {}
# Country specific checks.
#
- # validators_for(cc).each do |validator|
- # return false unless validator.plausible? ndc, rest
- # end
+ validators_for(cc).each do |validator|
+ p [validator, validator.plausible?(ndc, rest)]
+ return false unless validator.plausible? ndc, rest
+ end
return false unless (local.length-10) === rest_size
true
View
12 lib/phony/validators/length.rb
@@ -17,12 +17,12 @@ def initialize lengths
end
def plausible? ndc, rest
- ndc ||= '' # handle 'false' ndc's (like Denmark)
- lengths = [*@lengths[ndc.length]] # we can have one or more lengths, make them into an array
-
- length = ndc.size + rest.inject(0) { |sum, part| part ? sum + part.size : sum }
-
- return false unless lengths.include? length
+ # ndc ||= '' # handle 'false' ndc's (like Denmark)
+ # lengths = [*@lengths[ndc.length]] # we can have one or more lengths, make them into an array
+ #
+ # length = ndc.size + rest.inject(0) { |sum, part| part ? sum + part.size : sum }
+ #
+ # return false unless lengths.include? length
true
end
View
13 lib/phony/validators/ndc.rb
@@ -6,22 +6,17 @@ class NDC
attr_reader :invalids
- def initialize
- @invalids = []
+ def initialize options = {}
+ @invalids = options[:invalid] || []
end
- def plausible? ndc, rest
- @invalids && @invalids.each do |invalid|
+ def plausible? ndc, _
+ @invalids.each do |invalid|
return false if invalid === ndc
end
true
end
-
- def invalid ndc
- @invalids << ndc
- self
- end
end

0 comments on commit 3551b1e

Please sign in to comment.