Permalink
Browse files

+ countries definitions extracted, + regex

  • Loading branch information...
1 parent 9171e98 commit b136423679fa683f0b707b9792ada5a42802372c @floere committed Feb 26, 2011
@@ -1,11 +1,25 @@
include Phony::DSL
+# All countries, ordered by country code.
+#
+# Available matching/formatting methods:
+# * fixed: Always splits off a fixed length ndc. (Always use last in a | chain)
+# * none: Does not have a national destination code, e.g. Denmark, Iceland.
+# * one_of: Matches one of the following numbers. Splits if it does.
+# Options:
+# * max_length: Will try to match up to length max_length, then stop.
+# (Always use one_of with this option last in a | chain)
+# * match: Try to match the regex, and if it matches, splits it off.
+# Options:
+# * max_length: If it does not match, split of max_length, then stop.
+# (Always use one_of with this option last in a | chain)
+#
+
country '0', fixed(1) >> format(10) # Reserved.
country '1', fixed(3) >> format(3,4) # USA, Canada, etc.
country '7', fixed(3) >> format(3,2,2) # Kazakhstan (Republic of) & Russian Federation
-country '20', one_of('800') >> format(7) |
-
+country '20', one_of('800') >> format(7) | # Egypt
one_of('2', '3', :max_length => 2) >> format(8) # Cairo/Giza, Alexandria
# :mobile? => /^10|11|12|14|16|17|18|19*$/, :service? => /^800.*$/
country '27', fixed(2) >> format(3,4) # South Africa
@@ -21,9 +35,9 @@
country '40', Phony::Countries::Romania
# :service => %w{800 840 842 844 848}, :mobile => %w{74 76 77 78 79
-swiss_service_regex = /^800|840|842|844|848.*$/
-country '41', match(swiss_service_regex, 3) >> format(3,3) |
- fixed(2) >> format(3,2,2)
+swiss_service_regex = /^(800|840|842|844|848)\d+$/
+country '41', match(swiss_service_regex) >> format(3,3) | # Switzerland
+ fixed(2) >> format(3,2,2)
country '43', Phony::Countries::Austria
country '44', Phony::Countries::UnitedKingdom
country '45', none >> format(2,2,2,2) # Denmark
@@ -42,8 +56,8 @@
country '52', fixed(2) >> format(3,2,2) # TODO Mexico
country '53', fixed(2) >> format(3,2,2) # TODO Cuba
country '54', fixed(2) >> format(3,2,2) # TODO Argentine Republic
-brazilian_service = /^100|128|190|191|192|193|194|197|198|199.*$/
-country '55', match(brazilian_service, 3) >> format(3,3) | # Brazil (Federative Republic of)
+brazilian_service = /^(100|128|190|191|192|193|194|197|198|199)\d+$/
+country '55', match(brazilian_service) >> format(3,3) | # Brazil (Federative Republic of)
fixed(2) >> format(4,4)
# :service? => brazilian_service, :mobile? => ?
# http://en.wikipedia.org/wiki/Telephone_numbers_in_Brazil
View
@@ -15,7 +15,7 @@
# Countries.
#
-# Note: See country_codes.rb for a complete mapping.
+# The ones that need more space to define.
#
require File.expand_path '../phony/countries', __FILE__
require File.expand_path '../phony/countries/austria', __FILE__
@@ -34,7 +34,10 @@
require File.expand_path '../phony/countries/south_korea', __FILE__
require File.expand_path '../phony/countries/sweden', __FILE__
require File.expand_path '../phony/countries/united_kingdom', __FILE__
-require File.expand_path '../phony/country_definitions', __FILE__
+#
+# All other countries.
+#
+require File.expand_path '../countries', __FILE__
module Phony
View
@@ -1,40 +1 @@
-# Splits a national number into a fixed size NDC and rest.
-#
-module Phony
- module Countries
-
- # # TODO
- # #
- # def self.with_cc cc
- # mapping[cc.size][cc.to_s]
- # end
- #
- # # Add the given country to the mapping under the
- # # given country code.
- # #
- # def self.add country_code, country
- # country_code = country_code.to_s
- # optimized_country_code_access = country_code.size
- #
- # @mapping ||= {}
- # @mapping[optimized_country_code_access] ||= {}
- # @mapping[optimized_country_code_access][country_code] = country
- # end
- #
- # # Accessor for the mapping.
- # #
- # def self.mapping
- # @mapping
- # end
- #
- # # # Returns an anonymous fixed size ndc / format country handler.
- # # #
- # # # TODO Remove.
- # # #
- # # def self.fixed size, options = {}
- # # options.merge! :ndc_length => size
- # # Phony::Country.fixed options
- # # end
-
- end
-end
+module Phony::Countries end
@@ -11,7 +11,7 @@
'9', # Gent (Ghent/Gand)
]
-mobile_regex = /^4[789]\d{7}$/ # Mobistar, Base, Proximus
+mobile_regex = /^(4[789]\d)\d{6}$/ # Mobistar, Base, Proximus
service = [
'70', # Specialty numbers, i.e. bus information or bank information
@@ -29,5 +29,5 @@
]
Phony::Countries::Belgium = one_of(*service) >> format(3,3) |
- match(mobile_regex, 3) >> format(6) |
+ match(mobile_regex) >> format(6) |
one_of(*ndcs, :max_length => 2) >> format(3,5)
@@ -4068,5 +4068,5 @@
]
Phony::Countries::Germany = one_of(*service) >> format(3,10) |
- match(/^1[567]\d.*$/, 3) >> format(3,10) | # Mobile
+ match(/^(1[567]\d)\d*$/) >> format(3,10) | # Mobile
one_of(*ndcs, :max_length => 5) >> format(3,10)
@@ -28,5 +28,5 @@
]
Phony::Countries::Romania = one_of(*service) >> format(3,3) |
- match(/^7[1-8].*$/, 2) >> format(3,4) |
+ match(/^(7[1-8])\d*$/) >> format(3,4) |
one_of('21', '31', :max_length => 3) >> format(3,4) # Bucureşti
@@ -50,6 +50,8 @@ def format_local space, parts_ary
parts_ary.join space.to_s
end
+ #
+ #
def service? number
national_handler, cc, rest = split_cc number
national_handler.service? rest
View
@@ -53,8 +53,8 @@ def one_of *ndcs
NationalSplitters::Variable.new options[:max_length], ndcs
end
- def match regex, max_length
- NationalSplitters::Regex.instance_for regex, max_length
+ def match regex, options = {}
+ NationalSplitters::Regex.instance_for regex, options[:on_fail_take]
end
# Local splitters.
@@ -1,7 +1,5 @@
module Phony
- # This is the superclass of all special national number handlers.
- #
# NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.
#
class NationalCode
@@ -11,7 +9,7 @@ class NationalCode
def initialize national_splitter, local_splitter, normalize = nil
@national_splitter = national_splitter
@local_splitter = local_splitter
- @normalize = !(normalize == false) # if nil, true (default), if false, false, if true, true.
+ @normalize = normalize != false
end
# Split gets a number without country code and splits it into
@@ -11,21 +11,20 @@ module NationalSplitters
#
class Regex < Fixed
- attr_reader :max_length, :regex
+ attr_reader :on_fail_take, :regex
# Get a splitter for the given format.
#
# Note: Not cached.
#
- def self.instance_for regex, max_length = nil
- new regex, max_length
+ def self.instance_for regex, on_fail_take = nil
+ new regex, on_fail_take
end
-
- # Initialize with a regex => format mapping.
- #
- def initialize regex, max_length = nil
- @max_length = max_length
- @regex = regex
+
+ def initialize regex, on_fail_take = nil
+ super on_fail_take
+
+ @regex = regex
end
# Split a local number according to an assumed country specific format.
@@ -34,11 +33,13 @@ def initialize regex, max_length = nil
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
#
def split number
- return [number.slice!(0..max_length-1), number] if number =~ regex
+ # Improve matching.
+ #
+ return [number.slice!(0..$1.size-1), number] if number =~ regex
# Not found.
#
- super max_length
+ super number
end
end
View
@@ -1,4 +1,7 @@
module Phony
+
+ # Helper module that maps vanity numbers to digit numbers.
+ #
module Vanity
# Returns a char to number mapping string for the String#tr method.
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe Phony::NationalSplitters::Regex do
+
+ describe 'instance_for' do
+ it 'caches correctly (not)' do
+ described_class.instance_for(//).should_not equal(described_class.instance_for(//))
+ end
+ end
+
+ describe 'split' do
+ context 'without on_fail' do
+ let(:splitter) { described_class.instance_for(/^(123)\d+$/) }
+ end
+ context 'with on_fail 2' do
+ let(:splitter) { described_class.instance_for(/^(123)\d+$/, 2) }
+ it 'uses the on_fail_take' do
+ splitter.split('23456789').should == ['23', '456789']
+ end
+ end
+ end
+
+end

0 comments on commit b136423

Please sign in to comment.