Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

+ DSL rewrite almost finished

  • Loading branch information...
commit 43eed69dd76029dd257be1579055376c598bdb07 1 parent 8d7aeb4
@floere authored
Showing with 116 additions and 193 deletions.
  1. +2 −4 lib/phony.rb
  2. +29 −29 lib/phony/countries.rb
  3. +1 −1  lib/phony/countries/austria.rb
  4. +2 −2 lib/phony/countries/belgium.rb
  5. +2 −1  lib/phony/countries/chile.rb
  6. +2 −2 lib/phony/countries/china.rb
  7. +1 −1  lib/phony/countries/egypt.rb
  8. +3 −3 lib/phony/countries/germany.rb
  9. +2 −1  lib/phony/countries/hungary.rb
  10. +4 −2 lib/phony/countries/italy.rb
  11. +2 −2 lib/phony/countries/malaysia.rb
  12. +2 −1  lib/phony/countries/netherlands.rb
  13. +3 −3 lib/phony/countries/romania.rb
  14. +3 −3 lib/phony/countries/south_korea.rb
  15. +2 −1  lib/phony/countries/sweden.rb
  16. +4 −4 lib/phony/countries/united_kingdom.rb
  17. +1 −1  lib/phony/country.rb
  18. +21 −5 lib/phony/country_codes.rb
  19. +13 −12 lib/phony/country_definitions.rb
  20. +2 −8 lib/phony/dsl.rb
  21. +0 −33 spec/lib/phony/countries/belgium_spec.rb
  22. +0 −18 spec/lib/phony/countries/switzerland_spec.rb
  23. +0 −50 spec/lib/phony/countries/united_kingdom_spec.rb
  24. +1 −1  spec/lib/phony/country_codes_spec.rb
  25. +2 −4 spec/lib/phony/national_splitters/variable_spec.rb
  26. +12 −1 spec/lib/phony_spec.rb
View
6 lib/phony.rb
@@ -10,7 +10,7 @@
require File.expand_path '../phony/national_splitters/none', __FILE__
require File.expand_path '../phony/national_code', __FILE__
require File.expand_path '../phony/country', __FILE__
-
+require File.expand_path '../phony/country_codes', __FILE__
require File.expand_path '../phony/dsl', __FILE__
# Countries.
@@ -36,13 +36,11 @@
require File.expand_path '../phony/countries/united_kingdom', __FILE__
require File.expand_path '../phony/country_definitions', __FILE__
-require File.expand_path '../phony/country_codes', __FILE__
-
module Phony
# Phony uses a single country codes instance.
#
- @codes = CountryCodes.new
+ @codes = CountryCodes.instance
# Normalizes the given number.
#
View
58 lib/phony/countries.rb
@@ -3,38 +3,38 @@
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
# #
- # # TODO Remove.
+ # 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.fixed size, options = {}
- # options.merge! :ndc_length => size
- # Phony::Country.fixed options
+ # 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
View
2  lib/phony/countries/austria.rb
@@ -44,7 +44,7 @@
'667',
'668',
'669'
-],
+]
service = [
'710',
View
4 lib/phony/countries/belgium.rb
@@ -28,6 +28,6 @@
'909' # Premium
]
-Phony::Countries::Belgium = one_of(*service) >> format(3,3) or
- match(mobile_regex, 3) >> format(6) or
+Phony::Countries::Belgium = one_of(*service) >> format(3,3) |
+ match(mobile_regex, 3) >> format(6) |
one_of(*ndcs, :max_length => 2) >> format(3,5)
View
3  lib/phony/countries/chile.rb
@@ -8,5 +8,6 @@
#
service = %w{130 131 132 133 134 135 136 137 139 147 149}
-Phony::Countries::Chile = one_of(*service) >> format(3,3) or
+Phony::Countries::Chile = one_of(*service) >> format(3,3) |
+
one_of('2', '9', :max_length => 2) >> format(8) # Santiago (2) and mobile.
View
4 lib/phony/countries/china.rb
@@ -22,6 +22,6 @@
mobile = %w{ 130 131 132 133 135 136 137 138 139 145 150 151 152 153 155 156 157 158 159 185 186 187 188 189 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 }
service = %w{ 110 114 119 120 122 999 } # Probably not exhaustive. TODO Look at http://www.eoc.com.cn/?action-viewnews-itemid-11493.
-Phony::Countries::China = one_of(*service) >> format(8) or
- one_of(*mobile) >> format(4,4) or
+Phony::Countries::China = one_of(*service) >> format(8) |
+ one_of(*mobile) >> format(4,4) |
one_of(*ndcs, :max_length => 3) >> format(4,4)
View
2  lib/phony/countries/egypt.rb
@@ -13,5 +13,5 @@
# TODO Change how one_of works. max_length should be optional.
#
-Phony::Countries::Egypt = one_of('800') >> format(7) or
+Phony::Countries::Egypt = one_of('800') >> format(7) |
one_of(*ndcs, :max_length => 2) >> format(8)
View
6 lib/phony/countries/germany.rb
@@ -4067,6 +4067,6 @@
'9009',
]
-Phony::Countries::Germany = one_of(*service) >> format(3,10) or
- match(/^1[567]\d.*$/) >> format(3,10) or # Mobile
- one_of(*ndcs, :max_length => 5)
+Phony::Countries::Germany = one_of(*service) >> format(3,10) |
+ match(/^1[567]\d.*$/, 3) >> format(3,10) | # Mobile
+ one_of(*ndcs, :max_length => 5) >> format(3,10)
View
3  lib/phony/countries/hungary.rb
@@ -4,6 +4,7 @@
#
# http://en.wikipedia.org/wiki/Telephone_numbers_in_Hungary
#
-Phony::Countries::Hungary = one_of('104', '105', '107', '112') >> format(3,3) or # special
+Phony::Countries::Hungary = one_of('104', '105', '107', '112') >> format(3,3) |
+# special
# TODO mobile
one_of('1', :max_length => 2) >> format(3,4)
View
6 lib/phony/countries/italy.rb
@@ -155,6 +155,8 @@
# TODO Normalize!
#
-Phony::Countries::Italy = one_of(*service) >> format(3,3) or
- one_of(*mobile) >> format(3,4) or
+Phony::Countries::Italy = one_of(*service) >> format(3,3) |
+
+ one_of(*mobile) >> format(3,4) |
+
one_of(*ndcs, :max_length => 3) >> format(3,4)
View
4 lib/phony/countries/malaysia.rb
@@ -16,8 +16,8 @@
mobile = %w{ 10 11 12 13 14 153 154 156 158 16 17 18 19 }
service = %w{ 100 101 102 103 104 108 112 991 994 995 999 }
-Phony::Countries::Malaysia = one_of(*service) >> format(3,3)
- one_of(*mobile) >> format(8)
+Phony::Countries::Malaysia = one_of(*service) >> format(3,3) |
+ one_of(*mobile) >> format(8) |
one_of(*ndcs, :max_length => 2) >> format(8)
# :mobile => %w{ 10 11 12 13 14 153 154 156 158 16 17 18 19 },
View
3  lib/phony/countries/netherlands.rb
@@ -50,6 +50,7 @@
'909', # Betaalde informatienummers
]
-Phony::Countries::Netherlands = one_of(*service) >> format(3,3) or
+Phony::Countries::Netherlands = one_of(*service) >> format(3,3) |
+
one_of('6', *ndcs, :max_length => 3) >> format(8) # mobile + landline
# 8 is for mobile numbers, other numbers will work as well (they use 7).
View
6 lib/phony/countries/romania.rb
@@ -27,6 +27,6 @@
'906',
]
-Phony::Countries::Romania = one_of(*service) >> format(3,3) or
- match(/^7[1-8].*$/) >> format(3,4) or
- one_of('21', '31', :max_length => 3) >> format(3,4) # Bucureşti
+Phony::Countries::Romania = one_of(*service) >> format(3,3) |
+ match(/^7[1-8].*$/, 2) >> format(3,4) |
+ one_of('21', '31', :max_length => 3) >> format(3,4) # Bucureşti
View
6 lib/phony/countries/south_korea.rb
@@ -10,6 +10,6 @@
service = %w{ 30 50 60 70 80 100 101 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 125 127 128 129 131 132 134 141 182 188 }
mobile = ('10'..'19').to_a
-Phony::Countries::SouthKorea = one_of(*service) >> format(3,3) or
- one_of(*mobile) >> format(4,4) or
- one_of('2') >> format(4,4) # Seoul
+Phony::Countries::SouthKorea = one_of(*service) >> format(3,3) |
+ one_of(*mobile) >> format(4,4) |
+ one_of('2', :max_length => 2) >> format(4,4) # Seoul
View
3  lib/phony/countries/sweden.rb
@@ -53,5 +53,6 @@
'11414', # Police
]
-Phony::Countries::Sweden = one_of(*service) >> format(3,3) or
+Phony::Countries::Sweden = one_of(*service) >> format(3,3) |
+
one_of(*(ndcs + mobile), :max_length => 3) >> format(8)
View
8 lib/phony/countries/united_kingdom.rb
@@ -12,7 +12,7 @@
#
handlers = []
-area_code_2_national = Phony::NationalSplitters::Variable.new nil, :landline => [
+area_code_2_national = Phony::NationalSplitters::Variable.new nil, [
'20', # London
'23', # Southampton, Portsmith
'24', # Coventry
@@ -22,7 +22,7 @@
area_code_2_local = Phony::LocalSplitters::Fixed.instance_for [4, 4]
handlers << Phony::NationalCode.new(area_code_2_national, area_code_2_local)
-area_code_3_national = Phony::NationalSplitters::Variable.new nil, :landline => [
+area_code_3_national = Phony::NationalSplitters::Variable.new nil, [
# Geographical.
#
'113', # Leeds
@@ -61,7 +61,7 @@
# 6 is the fallback length.
#
-area_code_45_national = Phony::NationalSplitters::Variable.new 6, :landline => [
+area_code_45_national = Phony::NationalSplitters::Variable.new 6, [
# Geographical.
#
'1204', # Bolton
@@ -100,7 +100,7 @@
'17684', # Pooley Bridge
'17687', # Keswick
'19467', # Gosforth
-]
+].flatten
area_code_45_local = Phony::LocalSplitters::Fixed.instance_for [6]
handlers << Phony::NationalCode.new(area_code_45_national, area_code_45_local)
View
2  lib/phony/country.rb
@@ -16,7 +16,7 @@ def initialize *codes
#
# TODO
#
- def or(other)
+ def |(other)
self.codes = self.codes + other.codes
self
end
View
26 lib/phony/country_codes.rb
@@ -4,6 +4,12 @@ module Phony
#
class CountryCodes
+ attr_reader :mapping
+
+ def self.instance
+ @instance ||= new
+ end
+
def normalize number
# Remove non-digit chars.
#
@@ -80,12 +86,22 @@ def split_cc rest
# This line is never reached as CCs are in prefix code.
end
- # Cached mapping of all countries.
- #
- # TODO Ugh.
+ # # 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 mapping
- @mapping ||= Phony::Countries.mapping
+ def 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
end
View
25 lib/phony/country_definitions.rb
@@ -4,7 +4,8 @@
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) or
+country '20', one_of('800') >> format(7) |
+
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
@@ -17,16 +18,16 @@
country '36', Phony::Countries::Hungary
country '39', Phony::Countries::Italy
-# country '40', Phony::Countries::Romania
+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) or
+country '41', match(swiss_service_regex, 3) >> format(3,3) |
fixed(2) >> format(3,2,2)
-# country '43', Phony::Countries::Austria
-# country '44', Phony::Countries::UnitedKingdom
+country '43', Phony::Countries::Austria
+country '44', Phony::Countries::UnitedKingdom
country '45', none >> format(2,2,2,2) # Denmark
-# country '46', Phony::Countries::Sweden
+country '46', Phony::Countries::Sweden
country '47', # Norway
none >> matched_format(/^[1].*$/ => [3], /^[489].*$/ => [3,2,3], :fallback => [2,2,2,2])
# Poland (Republic of)
@@ -36,17 +37,17 @@
country '48', fixed(3) >> format(3,3)
country '49', Phony::Countries::Germany
-country '51', one_of('103', '105') >> format(3,3) or # Peru
+country '51', one_of('103', '105') >> format(3,3) | # Peru
one_of('1', '9', :max_length => 2) >> format(4,4) # Lima and mobile.
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) or # Brazil (Federative Republic of)
+country '55', match(brazilian_service, 3) >> 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
-# country '56', Phony::Countries::Chile
+country '56', Phony::Countries::Chile
country '57', fixed(2) >> format(3,2,2)
country '58', fixed(3) >> format(7) # Venezuela (Bolivarian Republic of)
@@ -161,9 +162,9 @@
country '299', fixed(2) >> format(3,2,2) # Greenland
country '350', fixed(2) >> format(3,2,2) # Gibraltar
-country '351', one_of('700', '800') >> format(3,3) or # Portugal
- one_of(*('90'..'99').to_a) >> format(3,3) or # mobile
- one_of('21', '22', :max_length => 3) >> format(3,4) # Lisboa & Porto
+country '351', one_of('700', '800') >> format(3,3) | # Portugal
+ one_of(*('90'..'99').to_a) >> format(3,4) | # mobile
+ one_of('21', '22', :max_length => 3) >> format(3,4) # Lisboa & Porto
country '352', fixed(2) >> format(3,2,2) # Luxembourg
country '353', fixed(2) >> format(3,2,2) # Ireland (0-3-4)
country '354', none >> format(3,4) # Iceland
View
10 lib/phony/dsl.rb
@@ -33,14 +33,8 @@ module DSL
#
#
- def country country_code, *splitters
- codes = []
- splitters.flatten!
- until splitters.empty?
- codes << Phony::NationalCode.new(splitters.shift, splitters.shift)
- end
- country = Phony::Country.new *codes
- Phony::Countries.add country_code, country
+ def country country_code, country
+ Phony::CountryCodes.instance.add country_code, country
end
# National matcher & splitters.
View
33 spec/lib/phony/countries/belgium_spec.rb
@@ -1,33 +0,0 @@
-# encoding: utf-8
-#
-
-require 'spec_helper'
-
-describe Phony::Countries::Belgium do
-
- before(:each) do
- @belgium = Phony::Countries::Belgium
- end
-
- describe "split" do
- it "should handle Antwerpen" do
- @belgium.split('35551212').should == ['3', '555', '1212']
- end
- it "should handle Brugge" do
- @belgium.split('505551212').should == ['50', '555', '1212']
- end
- it "should handle Brussels" do
- @belgium.split('25551212').should == ['2', '555', '1212']
- end
- it "should handle Gent" do
- @belgium.split('95551914').should == ['9', '555', '1914']
- end
- it "should handle Liège" do
- @belgium.split('45551414').should == ['4', '555', '1414']
- end
- it "should handle some mobile services" do
- @belgium.split('475279584').should == ['475', '279584']
- end
-
- end
-end
View
18 spec/lib/phony/countries/switzerland_spec.rb
@@ -1,18 +0,0 @@
-require 'spec_helper'
-
-describe 'Switzerland' do
-
- before(:each) do
- @switzerland = Phony::Countries.with_cc '41'
- end
-
- describe "split" do
- it "works" do
- @switzerland.split('443643532').should == ['44', '364', '35', '32']
- end
- it "works" do
- @switzerland.split('800123456').should == ['800', '123', '456']
- end
- end
-
-end
View
50 spec/lib/phony/countries/united_kingdom_spec.rb
@@ -1,50 +0,0 @@
-require 'spec_helper'
-
-describe Phony::Countries::UnitedKingdom do
-
- before(:each) do
- @uk = Phony::Countries::UnitedKingdom
- end
-
- # Note: Many Specs
- #
- describe "split" do
- it "should handle London" do
- @uk.split('2045671113').should == ['20', '4567', '1113']
- end
- it "should handle Cardiff" do
- @uk.split('2076229901').should == ['20', '7622', '9901']
- end
- it "should handle Leeds" do
- @uk.split('1136770011').should == ['113', '677', '0011']
- end
- it "should handle Dundee" do
- @uk.split('1382229845').should == ['1382', '229845']
- end
- it "should handle Bolten" do
- @uk.split('120499532').should == ['1204', '99532']
- end
- it "should handle Sedbergh" do
- @uk.split('1539618756').should ==['15396', '18756']
- end
- it "should handle Mobile Numbers" do
- @uk.split('7780605207').should == ['7780', '605207']
- end
- it "should handle Mobile Numbers" do
- @uk.split('7480605207').should == ['7480', '605207']
- end
- it "should handle service numbers with 800 [regression]" do
- @uk.split('8005878323').should == ['800', '587', '8323']
- end
- #
- # 44 116 xxx xxxx Leicester
- # 44 131 xxx xxxx Edinburgh
- # 44 151 xxx xxxx Liverpool
- # 44 1382 xxxxxx Dundee
- # 44 1386 xxxxxx Evesham
- # 44 1865 xxxxxx Oxford
- # 44 153 96 xxxxx Sedbergh
- # 44 169 77 xxxx Brampton
- end
-
-end
View
2  spec/lib/phony/country_codes_spec.rb
@@ -3,7 +3,7 @@
describe Phony::CountryCodes do
before(:all) do
- @countries = Phony::CountryCodes.new
+ @countries = Phony::CountryCodes.instance
end
describe 'split' do
View
6 spec/lib/phony/national_splitters/variable_spec.rb
@@ -5,9 +5,7 @@
describe 'split' do
context 'normal' do
before(:each) do
- @splitter = Phony::NationalSplitters::Variable.new 4, :normal => ['1', '316'],
- :mobile => ['67', '68',],
- :service => ['669', '711']
+ @splitter = Phony::NationalSplitters::Variable.new 4, ['1', '316', '67', '68', '669', '711']
end
it "handles Vienna" do
@splitter.split('198110').should == ['1', '98110']
@@ -24,7 +22,7 @@
end
context 'special handling for using the variable size splitter for Swiss service numbers' do
before(:each) do
- @splitter = Phony::NationalSplitters::Variable.new 2, :service => ['800']
+ @splitter = Phony::NationalSplitters::Variable.new 2, ['800']
end
it "should handle swiss service numbers" do
@splitter.split('800223344').should == ['800', '223344']
View
13 spec/lib/phony_spec.rb
@@ -127,7 +127,18 @@
it 'handles turkish numbers' do
Phony.split('903121234567').should == ['90', '312', '123', '4567'] # Ankara
end
- it "should handle US numbers" do
+ it 'handles UK numbers' do
+ Phony.split('442045671113').should == ['44', '20', '4567', '1113'] # London
+ Phony.split('442076229901').should == ['44', '20', '7622', '9901'] # Cardiff
+ Phony.split('441136770011').should == ['44', '113', '677', '0011'] # Leeds
+ Phony.split('441382229845').should == ['44', '1382', '229845'] # Dundee
+ Phony.split('44120499532').should == ['44', '1204', '99532'] # Bolten
+ Phony.split('441539618756').should == ['44', '15396', '18756'] # Sedbergh
+ Phony.split('447780605207').should == ['44', '7780', '605207'] # Mobile
+ Phony.split('447480605207').should == ['44', '7480', '605207'] # Mobile
+ Phony.split('448005878323').should == ['44', '800', '587', '8323'] # Service, regression
+ end
+ it "handles US numbers" do
Phony.split('15551115511').should == ['1', '555', '111', '5511']
end
it "should handle venezuelan numbers" do
Please sign in to comment.
Something went wrong with that request. Please try again.