Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- old code (80% replaced), + preparation for new features

  • Loading branch information...
commit 03dc48d0113df8d604e66fa3dcb9b54e4b7a2f80 1 parent 794d4d6
@floere authored
View
3  README.textile
@@ -4,6 +4,9 @@ h2. Description
This gem can normalize, format and split E164 numbers.
+Currently handles AT, CH, DE, FR, LI, US numbers.
+And to some extend, all others. Just try if it works for you.
+
h2. Some examples
h3. Normalizing
View
8 lib/phony.rb
@@ -20,6 +20,8 @@
#
require File.expand_path '../phony/countries/austria', __FILE__
require File.expand_path '../phony/countries/germany', __FILE__
+require File.expand_path '../phony/countries/all', __FILE__
+
require File.expand_path '../phony/country_codes', __FILE__
module Phony
@@ -58,13 +60,13 @@ def self.formatted! phone_number, options = {}
end
def self.service? number
-
+ @codes.service? number.dup
end
def self.mobile? number
-
+ @codes.mobile? number.dup
end
def self.landline? number
-
+ @codes.landline? number.dup
end
# Returns true if there is a character in the number
View
392 lib/phony/countries/all.rb
@@ -0,0 +1,392 @@
+# Splits a national number into a fixed size NDC and rest.
+#
+module Phony
+ module Countries
+ class All
+
+ # Returns an anonymous fixed size ndc / format country handler.
+ #
+ def self.fixed size, options = {}
+ local_format = options[:local_format] || [3, 2, 2] # Default is the 3-2-2 format.
+
+ # If service ndsc, handle through non fixed national code splitter.
+ #
+ service_ndcs = options[:service_ndcs]
+ mobile_ndcs = options[:mobile_ndcs]
+ national_splitter = if service_ndcs || mobile_ndcs
+ NationalCodes::VariableSplitter.new size,
+ :service => [*service_ndcs],
+ :mobile => [*mobile_ndcs]
+ else
+ NationalCodes::FixedSplitter.instance_for size
+ end
+ local_splitter = LocalSplitter.instance_for local_format
+
+ NationalCode.new national_splitter, local_splitter
+ end
+
+ # Defines a mapping of country code to all splitters/formatters.
+ #
+ # Note: The above fixed helper is used.
+ # In some cases, we have explicit countries defined.
+ #
+ def self.mapping
+ {
+ 1 => {
+ '0' => fixed(1), # reserved
+ '1' => fixed(3, :local_format => [3, 10]), # USA, Canada, etc.
+ '7' => fixed(3), # Kazakhstan (Republic of) & Russian Federation
+ },
+ 2 => {
+ '20' => fixed(2), # Egypt
+ '27' => fixed(2), # South Africa
+
+ '30' => fixed(2), # Greece
+ '31' => fixed(2), # Netherlands
+ '32' => fixed(2), # Belgium
+ '33' => fixed(1,
+ :local_format => [2, 2, 2, 2],
+ :service_ndcs => %w{8},
+ :mobile_ndcs => %w{6 7}
+ ), # France
+ '34' => fixed(2), # Spain
+ '36' => fixed(2), # Hungary
+ '39' => fixed(3), # Italy
+
+ '40' => fixed(2), # Romania
+ '41' => fixed(2,
+ :local_format => [3, 2, 2],
+ :service_ndcs => %w{800 840 842 844 848},
+ :mobile_ndcs => %w{74 76 77 78 79}
+ ), # Switzerland (Confederation of)
+ '43' => Countries::Austria.new,
+ '44' => fixed(2), # United Kingdom of Great Britain and Northern Ireland
+ '45' => fixed(2), # Denmark
+ '46' => fixed(2), # Sweden
+ '47' => fixed(2), # Norway
+ '48' => fixed(2), # Poland (Republic of)
+ '49' => Countries::Germany.new,
+
+ '51' => fixed(2), # Peru
+ '52' => fixed(2), # Mexico
+ '53' => fixed(2), # Cuba
+ '54' => fixed(2), # Argentine Republic
+ '55' => fixed(2), # Brazil (Federative Republic of)
+ '56' => fixed(2), # Chile
+ '57' => fixed(2), # Colombia (Republic of)
+ '58' => fixed(2), # Venezuela (Bolivarian Republic of)
+
+ '60' => fixed(2), # Malaysia
+ '61' => fixed(2), # Australia
+ '62' => fixed(2), # Indonesia (Republic of)
+ '63' => fixed(2), # Philippines (Republic of the)
+ '64' => fixed(1, :local_format => [3, 4]), # New Zealand
+ '65' => fixed(2), # Singapore (Republic of)
+ '66' => fixed(2), # Thailand
+
+ '81' => fixed(2), # Japan
+ '82' => fixed(2), # Korea (Republic of)
+ '84' => fixed(2), # Viet Nam (Socialist Republic of)
+ '86' => fixed(2), # China (People's Republic of)
+
+ '90' => fixed(2), # Turkey
+ '91' => fixed(2), # India (Republic of)
+ '92' => fixed(2), # Pakistan (Islamic Republic of)
+ '93' => fixed(2), # Afghanistan
+ '94' => fixed(2), # Sri Lanka (Democratic Socialist Republic of)
+ '95' => fixed(2), # Myanmar (Union of)
+ '98' => fixed(2), #Iran (Islamic Republic of)
+ },
+ 3 => {
+ '210' => fixed(2), # -
+ '211' => fixed(2), # -
+ '212' => fixed(2), # Morocco
+ '213' => fixed(2), # Algeria
+ '214' => fixed(2), # -
+ '215' => fixed(2), # -
+ '216' => fixed(2), # Tunisia
+ '217' => fixed(2), # -
+ '218' => fixed(2), # Lybia
+ '219' => fixed(2), # -
+
+ '220' => fixed(2), # Gambia
+ '221' => fixed(2), # Senegal
+ '222' => fixed(2), # Mauritania
+ '223' => fixed(2), # Mali
+ '224' => fixed(2), # Guinea
+ '225' => fixed(2), # Côte d'Ivoire
+ '226' => fixed(2), # Burkina Faso
+ '227' => fixed(2), # Niger
+ '228' => fixed(2), # Togolese Republic
+ '229' => fixed(2), # Benin
+
+ '230' => fixed(2), # Mauritius
+ '231' => fixed(2), # Liberia
+ '232' => fixed(2), # Sierra Leone
+ '233' => fixed(2), # Ghana
+ '234' => fixed(2), # Nigeria
+ '235' => fixed(2), # Chad
+ '236' => fixed(2), # Central African Republic
+ '237' => fixed(2), # Cameroon
+ '238' => fixed(2), # Cape Verde
+ '239' => fixed(2), # Sao Tome and Principe
+
+ '240' => fixed(2), # Equatorial Guinea
+ '241' => fixed(2), # Gabonese Republic
+ '242' => fixed(2), # Congo
+ '243' => fixed(2), # Democratic Republic of the Congo
+ '244' => fixed(2), # Angola
+ '245' => fixed(2), # Guinea-Bissau
+ '246' => fixed(2), # Diego Garcia
+ '247' => fixed(2), # Ascension
+ '248' => fixed(2), # Seychelles
+ '249' => fixed(2), # Sudan
+
+ '250' => fixed(2), # Rwanda
+ '251' => fixed(2), # Ethiopia
+ '252' => fixed(2), # Somali Democratic Republic
+ '253' => fixed(2), # Djibouti
+ '254' => fixed(2), # Kenya
+ '255' => fixed(2), # Tanzania
+ '256' => fixed(2), # Uganda
+ '257' => fixed(2), # Burundi
+ '258' => fixed(2), # Mozambique
+ '259' => fixed(2), # -
+
+ '260' => fixed(2), # Zambia
+ '261' => fixed(2), # Madagascar
+ '262' => fixed(3), # Reunion / Mayotte (new)
+ '263' => fixed(2), # Zimbabwe
+ '264' => fixed(2), # Namibia
+ '265' => fixed(2), # Malawi
+ '266' => fixed(2), # Lesotho
+ '267' => fixed(2), # Botswana
+ '268' => fixed(2), # Swaziland
+ '269' => fixed(2), # Comoros
+
+ '280' => fixed(2), # -
+ '281' => fixed(2), # -
+ '282' => fixed(2), # -
+ '283' => fixed(2), # -
+ '284' => fixed(2), # -
+ '285' => fixed(2), # -
+ '286' => fixed(2), # -
+ '287' => fixed(2), # -
+ '288' => fixed(2), # -
+ '289' => fixed(2), # -
+ '290' => fixed(2), # Saint Helena
+
+ '291' => fixed(2), # Eritrea
+ '292' => fixed(2), # -
+ '293' => fixed(2), # -
+ '294' => fixed(2), # -
+ '295' => fixed(2), # -
+ '296' => fixed(2), # -
+ '297' => fixed(2), # Aruba
+ '298' => fixed(2), # Faroe Islands
+ '299' => fixed(2), # Greenland
+
+ '350' => fixed(2), # Gibraltar
+ '351' => fixed(2), # Portugal
+ '352' => fixed(2), # Luxembourg
+ '353' => fixed(2), # Ireland
+ '354' => fixed(2), # Iceland
+ '355' => fixed(2), # Albania
+ '356' => fixed(2), # Malta
+ '357' => fixed(2), # Cyprus
+ '358' => fixed(2), # Finland
+ '359' => fixed(2), # Bulgaria
+
+ '370' => fixed(2), # Lithuania
+ '371' => fixed(2), # Latvia
+ '372' => fixed(2), # Estonia
+ '373' => fixed(2), # Moldova
+ '374' => fixed(2), # Armenia
+ '375' => fixed(2), # Belarus
+ '376' => fixed(2), # Andorra
+ '377' => fixed(2), # Monaco
+ '378' => fixed(2), # San Marino
+ '379' => fixed(2), # Vatican City State
+
+ '380' => fixed(2), # Ukraine
+ '381' => fixed(2), # Serbia and Montenegro
+ '382' => fixed(2), # -
+ '383' => fixed(2), # -
+ '384' => fixed(2), # -
+ '385' => fixed(2), # Croatia
+ '386' => fixed(2), # Slovenia
+ '387' => fixed(2), # Bosnia and Herzegovina
+ '388' => fixed(2), # Group of countries, shared code
+ '389' => fixed(2), # The Former Yugoslav Republic of Macedonia
+
+ '420' => fixed(2), # Czech Republic
+ '421' => fixed(2), # Slovak Republic
+ '422' => fixed(2), # Spare code
+ '423' => fixed(0, :local_format => [3, 2, 2]), # Liechtenstein (Principality of)
+ '424' => fixed(2), # -
+ '425' => fixed(2), # -
+ '426' => fixed(2), # -
+ '427' => fixed(2), # -
+ '428' => fixed(2), # -
+ '429' => fixed(2), # -
+
+ '500' => fixed(2), # Falkland Islands (Malvinas)
+ '501' => fixed(2), # Belize
+ '502' => fixed(2), # Guatemala (Republic of)
+ '503' => fixed(2), # El Salvador (Republic of)
+ '504' => fixed(2), # Honduras (Republic of)
+ '505' => fixed(2), # Nicaragua
+ '506' => fixed(2), # Costa Rica
+ '507' => fixed(2), # Panama (Republic of)
+ '508' => fixed(2), # Saint Pierre and Miquelon (Collectivité territoriale de la République française)
+ '509' => fixed(2), # Haiti (Republic of)
+
+ '590' => fixed(3), # Guadeloupe (French Department of)
+ '591' => fixed(2), # Bolivia (Republic of)
+ '592' => fixed(2), # Guyana
+ '593' => fixed(2), # Ecuador
+ '594' => fixed(3), # French Guiana (French Department of)
+ '595' => fixed(2), # Paraguay (Republic of)
+ '596' => fixed(3), # Martinique (French Department of)
+ '597' => fixed(2), # Suriname (Republic of)
+ '598' => fixed(2), # Uruguay (Eastern Republic of)
+ '599' => fixed(2), # Netherlands Antilles
+
+ '670' => fixed(2), # Democratic Republic of Timor-Leste
+ '671' => fixed(2), # Spare code
+ '672' => fixed(2), # Australian External Territories
+ '673' => fixed(2), # Brunei Darussalam
+ '674' => fixed(2), # Nauru (Republic of)
+ '675' => fixed(2), # Papua New Guinea
+ '676' => fixed(2), # Tonga (Kingdom of)
+ '677' => fixed(2), # Solomon Islands
+ '678' => fixed(2), # Vanuatu (Republic of)
+ '679' => fixed(2), # Fiji (Republic of)
+
+ '680' => fixed(2), # Palau (Republic of)
+ '681' => fixed(2), # Wallis and Futuna (Territoire français d'outre-mer)
+ '682' => fixed(2), # Cook Islands
+ '683' => fixed(2), # Niue
+ '684' => fixed(2), # -
+ '685' => fixed(2), # Samoa (Independent State of)
+ '686' => fixed(2), # Kiribati (Republic of)
+ '687' => fixed(2), # New Caledonia (Territoire français d'outre-mer)
+ '688' => fixed(2), # Tuvalu
+ '689' => fixed(2), # French Polynesia (Territoire français d'outre-mer)
+
+ '690' => fixed(2), # Tokelau
+ '691' => fixed(2), # Micronesia (Federated States of)
+ '692' => fixed(2), # Marshall Islands (Republic of the)
+ '693' => fixed(2), # -
+ '694' => fixed(2), # -
+ '695' => fixed(2), # -
+ '696' => fixed(2), # -
+ '697' => fixed(2), # -
+ '698' => fixed(2), # -
+ '699' => fixed(2), # -
+
+ '800' => fixed(2), # International Freephone Service
+ '801' => fixed(2), # -
+ '802' => fixed(2), # -
+ '803' => fixed(2), # -
+ '804' => fixed(2), # -
+ '805' => fixed(2), # -
+ '806' => fixed(2), # -
+ '807' => fixed(2), # -
+ '808' => fixed(2), # International Shared Cost Service (ISCS)
+ '809' => fixed(2), # -
+
+ '830' => fixed(2), # -
+ '831' => fixed(2), # -
+ '832' => fixed(2), # -
+ '833' => fixed(2), # -
+ '834' => fixed(2), # -
+ '835' => fixed(2), # -
+ '836' => fixed(2), # -
+ '837' => fixed(2), # -
+ '838' => fixed(2), # -
+ '839' => fixed(2), # -
+
+ '850' => fixed(2), # Democratic People's Republic of Korea
+ '851' => fixed(2), # Spare code
+ '852' => fixed(2), # Hong Kong, China
+ '853' => fixed(2), # Macao, China
+ '854' => fixed(2), # Spare code
+ '855' => fixed(2), # Cambodia (Kingdom of)
+ '856' => fixed(2), # Lao People's Democratic Republic
+ '857' => fixed(2), # Spare code
+ '858' => fixed(2), # Spare code
+ '859' => fixed(2), # Spare code
+
+ '870' => fixed(2), # Inmarsat SNAC
+ '871' => fixed(2), # Inmarsat (Atlantic Ocean-East)
+ '872' => fixed(2), # Inmarsat (Pacific Ocean)
+ '873' => fixed(2), # Inmarsat (Indian Ocean)
+ '874' => fixed(2), # Inmarsat (Atlantic Ocean-West)
+ '875' => fixed(2), # Reserved - Maritime Mobile Service Applications
+ '876' => fixed(2), # Reserved - Maritime Mobile Service Applications
+ '877' => fixed(2), # Reserved - Maritime Mobile Service Applications
+ '878' => fixed(2), # Universal Personal Telecommunication Service (UPT)
+ '879' => fixed(2), # Reserved for national non-commercial purposes
+
+ '880' => fixed(2), # Bangladesh (People's Republic of)
+ '881' => fixed(2), # International Mobile, shared code
+ '882' => fixed(2), # International Networks, shared code
+ '883' => fixed(2), # -
+ '884' => fixed(2), # -
+ '885' => fixed(2), # -
+ '886' => fixed(2), # Reserved
+ '887' => fixed(2), # -
+ '888' => fixed(2), # Reserved for future global service
+ '889' => fixed(2), # -
+
+ '890' => fixed(2), # -
+ '891' => fixed(2), # -
+ '892' => fixed(2), # -
+ '893' => fixed(2), # -
+ '894' => fixed(2), # -
+ '895' => fixed(2), # -
+ '896' => fixed(2), # -
+ '897' => fixed(2), # -
+ '898' => fixed(2), # -
+ '899' => fixed(2), # -
+
+ '960' => fixed(2), # Maldives (Republic of)
+ '961' => fixed(2), # Lebanon
+ '962' => fixed(2), # Jordan (Hashemite Kingdom of)
+ '963' => fixed(2), # Syrian Arab Republic
+ '964' => fixed(2), # Iraq (Republic of)
+ '965' => fixed(2), # Kuwait (State of)
+ '966' => fixed(2), # Saudi Arabia (Kingdom of)
+ '967' => fixed(2), # Yemen (Republic of)
+ '968' => fixed(2), # Oman (Sultanate of)
+ '969' => fixed(2), # Reserved - reservation currently under investigation
+
+ '970' => fixed(2), # Reserved
+ '971' => fixed(2), # United Arab Emirates
+ '972' => fixed(2), # Israel (State of)
+ '973' => fixed(2), # Bahrain (Kingdom of)
+ '974' => fixed(2), # Qatar (State of)
+ '975' => fixed(2), # Bhutan (Kingdom of)
+ '976' => fixed(2), # Mongolia
+ '977' => fixed(2), # Nepal
+ '978' => fixed(2), # -
+ '979' => fixed(2), # International Premium Rate Service (IPRS)
+
+ '990' => fixed(2), # Spare code
+ '991' => fixed(2), # Trial of a proposed new international telecommunication public correspondence service, shared code
+ '992' => fixed(2), # Tajikistan (Republic of)
+ '993' => fixed(2), # Turkmenistan
+ '994' => fixed(2), # Azerbaijani Republic
+ '995' => fixed(2), # Georgia
+ '996' => fixed(2), # Kyrgyz Republic
+ '997' => fixed(2), # Spare code
+ '998' => fixed(2), # Uzbekistan (Republic of)
+ '999' => fixed(2), # Reserved for possible future use within the Telecommunications for Disaster Relief (TDR) concept
+ }
+ }
+ end
+
+ end
+ end
+end
View
5 lib/phony/countries/austria.rb
@@ -1,7 +1,8 @@
-# Splits a national number into a fixed size NDC and rest.
-#
module Phony
module Countries
+
+ # Numbers for Austria.
+ #
class Austria < Phony::NationalCode
def initialize
View
396 lib/phony/country_codes.rb
@@ -43,6 +43,21 @@ def format_local space, parts_ary
parts_ary.join space.to_s
end
+ # TODO Speed these methods up.
+ #
+ def service? number
+ national_handler, cc, rest = split_cc number
+ national_handler.service? rest
+ end
+ def mobile? number
+ national_handler, cc, rest = split_cc number
+ national_handler.mobile? rest
+ end
+ def landline? number
+ national_handler, cc, rest = split_cc number
+ national_handler.landline? rest
+ end
+
# Is the given number a vanity number?
#
def vanity? number
@@ -56,17 +71,6 @@ def vanity_to_number vanity_number
"#{cc}#{national_handler.vanity_to_number(rest)}"
end
- # # Removes 0s from partially normalized numbers such as:
- # # 410443643533
- # #
- # # Example:
- # # 410443643533 -> 41443643533
- # #
- # def self.remove_relative_zeros! number
- #
- # '%s%s' % split_cc(number).collect! { |code| code.gsub(/^0+/, '') }
- # end
-
def split_cc rest
presumed_cc = ''
1.upto(3) do |i|
@@ -77,380 +81,20 @@ def split_cc rest
# TODO raise
end
- # Removes 0s from partially normalized numbers such as:
- # 410443643533
+ # Removes 0s from partially normalized numbers
+ # such as 410443643533.
#
# Example:
# 410443643533 -> 41443643533
def remove_relative_zeros! phone_number
- _, cc, rest = split_cc(phone_number)
+ _, cc, rest = split_cc phone_number
'%s%s' % [cc, rest].collect! { |code| code.gsub(/^0+/, '') }
end
- # Returns an anonymous fixed size ndc / format country handler.
+ # Cached mapping of all countries.
#
- def fixed size, options = {}
- local_format = options[:local_format] || [3, 2, 2] # Default is the 3-2-2 format.
-
- # If service ndsc, handle through non fixed national code splitter.
- #
-
- national_splitter = if service_ndcs = options[:service_ndcs]
- NationalCodes::VariableSplitter.new size, :service => service_ndcs
- else
- NationalCodes::FixedSplitter.instance_for size
- end
- local_splitter = LocalSplitter.instance_for local_format
-
- NationalCode.new national_splitter, local_splitter
- end
def mapping
- @mapping ||= {
- 1 => {
- '0' => fixed(1), # reserved
- '1' => fixed(3, :local_format => [3, 10]), # USA, Canada, etc.
- '7' => fixed(3), # Kazakhstan (Republic of) & Russian Federation
- },
- 2 => {
- '20' => fixed(2), # Egypt
- '27' => fixed(2), # South Africa
-
- '30' => fixed(2), # Greece
- '31' => fixed(2), # Netherlands
- '32' => fixed(2), # Belgium
- '33' => fixed(1, :local_format => [2, 2, 2, 2], :service_ndcs => ['8']), # France
- '34' => fixed(2), # Spain
- '36' => fixed(2), # Hungary
- '39' => fixed(3), # Italy
-
- '40' => fixed(2), # Romania
- '41' => fixed(2, :local_format => [3, 2, 2], :service_ndcs => ['800', '840', '842', '844', '848']), # Switzerland (Confederation of)
- '43' => Countries::Austria.new,
- '44' => fixed(2), # United Kingdom of Great Britain and Northern Ireland
- '45' => fixed(2), # Denmark
- '46' => fixed(2), # Sweden
- '47' => fixed(2), # Norway
- '48' => fixed(2), # Poland (Republic of)
- '49' => Countries::Germany.new,
-
- '51' => fixed(2), # Peru
- '52' => fixed(2), # Mexico
- '53' => fixed(2), # Cuba
- '54' => fixed(2), # Argentine Republic
- '55' => fixed(2), # Brazil (Federative Republic of)
- '56' => fixed(2), # Chile
- '57' => fixed(2), # Colombia (Republic of)
- '58' => fixed(2), # Venezuela (Bolivarian Republic of)
-
- '60' => fixed(2), # Malaysia
- '61' => fixed(2), # Australia
- '62' => fixed(2), # Indonesia (Republic of)
- '63' => fixed(2), # Philippines (Republic of the)
- '64' => fixed(1, :local_format => [3, 4]), # New Zealand
- '65' => fixed(2), # Singapore (Republic of)
- '66' => fixed(2), # Thailand
-
- '81' => fixed(2), # Japan
- '82' => fixed(2), # Korea (Republic of)
- '84' => fixed(2), # Viet Nam (Socialist Republic of)
- '86' => fixed(2), # China (People's Republic of)
-
- '90' => fixed(2), # Turkey
- '91' => fixed(2), # India (Republic of)
- '92' => fixed(2), # Pakistan (Islamic Republic of)
- '93' => fixed(2), # Afghanistan
- '94' => fixed(2), # Sri Lanka (Democratic Socialist Republic of)
- '95' => fixed(2), # Myanmar (Union of)
- '98' => fixed(2), #Iran (Islamic Republic of)
- },
- 3 => {
- '210' => fixed(2), # -
- '211' => fixed(2), # -
- '212' => fixed(2), # Morocco
- '213' => fixed(2), # Algeria
- '214' => fixed(2), # -
- '215' => fixed(2), # -
- '216' => fixed(2), # Tunisia
- '217' => fixed(2), # -
- '218' => fixed(2), # Lybia
- '219' => fixed(2), # -
-
- '220' => fixed(2), # Gambia
- '221' => fixed(2), # Senegal
- '222' => fixed(2), # Mauritania
- '223' => fixed(2), # Mali
- '224' => fixed(2), # Guinea
- '225' => fixed(2), # Côte d'Ivoire
- '226' => fixed(2), # Burkina Faso
- '227' => fixed(2), # Niger
- '228' => fixed(2), # Togolese Republic
- '229' => fixed(2), # Benin
-
- '230' => fixed(2), # Mauritius
- '231' => fixed(2), # Liberia
- '232' => fixed(2), # Sierra Leone
- '233' => fixed(2), # Ghana
- '234' => fixed(2), # Nigeria
- '235' => fixed(2), # Chad
- '236' => fixed(2), # Central African Republic
- '237' => fixed(2), # Cameroon
- '238' => fixed(2), # Cape Verde
- '239' => fixed(2), # Sao Tome and Principe
-
- '240' => fixed(2), # Equatorial Guinea
- '241' => fixed(2), # Gabonese Republic
- '242' => fixed(2), # Congo
- '243' => fixed(2), # Democratic Republic of the Congo
- '244' => fixed(2), # Angola
- '245' => fixed(2), # Guinea-Bissau
- '246' => fixed(2), # Diego Garcia
- '247' => fixed(2), # Ascension
- '248' => fixed(2), # Seychelles
- '249' => fixed(2), # Sudan
-
- '250' => fixed(2), # Rwanda
- '251' => fixed(2), # Ethiopia
- '252' => fixed(2), # Somali Democratic Republic
- '253' => fixed(2), # Djibouti
- '254' => fixed(2), # Kenya
- '255' => fixed(2), # Tanzania
- '256' => fixed(2), # Uganda
- '257' => fixed(2), # Burundi
- '258' => fixed(2), # Mozambique
- '259' => fixed(2), # -
-
- '260' => fixed(2), # Zambia
- '261' => fixed(2), # Madagascar
- '262' => fixed(3), # Reunion / Mayotte (new)
- '263' => fixed(2), # Zimbabwe
- '264' => fixed(2), # Namibia
- '265' => fixed(2), # Malawi
- '266' => fixed(2), # Lesotho
- '267' => fixed(2), # Botswana
- '268' => fixed(2), # Swaziland
- '269' => fixed(2), # Comoros
-
- '280' => fixed(2), # -
- '281' => fixed(2), # -
- '282' => fixed(2), # -
- '283' => fixed(2), # -
- '284' => fixed(2), # -
- '285' => fixed(2), # -
- '286' => fixed(2), # -
- '287' => fixed(2), # -
- '288' => fixed(2), # -
- '289' => fixed(2), # -
- '290' => fixed(2), # Saint Helena
-
- '291' => fixed(2), # Eritrea
- '292' => fixed(2), # -
- '293' => fixed(2), # -
- '294' => fixed(2), # -
- '295' => fixed(2), # -
- '296' => fixed(2), # -
- '297' => fixed(2), # Aruba
- '298' => fixed(2), # Faroe Islands
- '299' => fixed(2), # Greenland
-
- '350' => fixed(2), # Gibraltar
- '351' => fixed(2), # Portugal
- '352' => fixed(2), # Luxembourg
- '353' => fixed(2), # Ireland
- '354' => fixed(2), # Iceland
- '355' => fixed(2), # Albania
- '356' => fixed(2), # Malta
- '357' => fixed(2), # Cyprus
- '358' => fixed(2), # Finland
- '359' => fixed(2), # Bulgaria
-
- '370' => fixed(2), # Lithuania
- '371' => fixed(2), # Latvia
- '372' => fixed(2), # Estonia
- '373' => fixed(2), # Moldova
- '374' => fixed(2), # Armenia
- '375' => fixed(2), # Belarus
- '376' => fixed(2), # Andorra
- '377' => fixed(2), # Monaco
- '378' => fixed(2), # San Marino
- '379' => fixed(2), # Vatican City State
-
- '380' => fixed(2), # Ukraine
- '381' => fixed(2), # Serbia and Montenegro
- '382' => fixed(2), # -
- '383' => fixed(2), # -
- '384' => fixed(2), # -
- '385' => fixed(2), # Croatia
- '386' => fixed(2), # Slovenia
- '387' => fixed(2), # Bosnia and Herzegovina
- '388' => fixed(2), # Group of countries, shared code
- '389' => fixed(2), # The Former Yugoslav Republic of Macedonia
-
- '420' => fixed(2), # Czech Republic
- '421' => fixed(2), # Slovak Republic
- '422' => fixed(2), # Spare code
- '423' => fixed(0, :local_format => [3, 2, 2]), # Liechtenstein (Principality of)
- '424' => fixed(2), # -
- '425' => fixed(2), # -
- '426' => fixed(2), # -
- '427' => fixed(2), # -
- '428' => fixed(2), # -
- '429' => fixed(2), # -
-
- '500' => fixed(2), # Falkland Islands (Malvinas)
- '501' => fixed(2), # Belize
- '502' => fixed(2), # Guatemala (Republic of)
- '503' => fixed(2), # El Salvador (Republic of)
- '504' => fixed(2), # Honduras (Republic of)
- '505' => fixed(2), # Nicaragua
- '506' => fixed(2), # Costa Rica
- '507' => fixed(2), # Panama (Republic of)
- '508' => fixed(2), # Saint Pierre and Miquelon (Collectivité territoriale de la République française)
- '509' => fixed(2), # Haiti (Republic of)
-
- '590' => fixed(3), # Guadeloupe (French Department of)
- '591' => fixed(2), # Bolivia (Republic of)
- '592' => fixed(2), # Guyana
- '593' => fixed(2), # Ecuador
- '594' => fixed(3), # French Guiana (French Department of)
- '595' => fixed(2), # Paraguay (Republic of)
- '596' => fixed(3), # Martinique (French Department of)
- '597' => fixed(2), # Suriname (Republic of)
- '598' => fixed(2), # Uruguay (Eastern Republic of)
- '599' => fixed(2), # Netherlands Antilles
-
- '670' => fixed(2), # Democratic Republic of Timor-Leste
- '671' => fixed(2), # Spare code
- '672' => fixed(2), # Australian External Territories
- '673' => fixed(2), # Brunei Darussalam
- '674' => fixed(2), # Nauru (Republic of)
- '675' => fixed(2), # Papua New Guinea
- '676' => fixed(2), # Tonga (Kingdom of)
- '677' => fixed(2), # Solomon Islands
- '678' => fixed(2), # Vanuatu (Republic of)
- '679' => fixed(2), # Fiji (Republic of)
-
- '680' => fixed(2), # Palau (Republic of)
- '681' => fixed(2), # Wallis and Futuna (Territoire français d'outre-mer)
- '682' => fixed(2), # Cook Islands
- '683' => fixed(2), # Niue
- '684' => fixed(2), # -
- '685' => fixed(2), # Samoa (Independent State of)
- '686' => fixed(2), # Kiribati (Republic of)
- '687' => fixed(2), # New Caledonia (Territoire français d'outre-mer)
- '688' => fixed(2), # Tuvalu
- '689' => fixed(2), # French Polynesia (Territoire français d'outre-mer)
-
- '690' => fixed(2), # Tokelau
- '691' => fixed(2), # Micronesia (Federated States of)
- '692' => fixed(2), # Marshall Islands (Republic of the)
- '693' => fixed(2), # -
- '694' => fixed(2), # -
- '695' => fixed(2), # -
- '696' => fixed(2), # -
- '697' => fixed(2), # -
- '698' => fixed(2), # -
- '699' => fixed(2), # -
-
- '800' => fixed(2), # International Freephone Service
- '801' => fixed(2), # -
- '802' => fixed(2), # -
- '803' => fixed(2), # -
- '804' => fixed(2), # -
- '805' => fixed(2), # -
- '806' => fixed(2), # -
- '807' => fixed(2), # -
- '808' => fixed(2), # International Shared Cost Service (ISCS)
- '809' => fixed(2), # -
-
- '830' => fixed(2), # -
- '831' => fixed(2), # -
- '832' => fixed(2), # -
- '833' => fixed(2), # -
- '834' => fixed(2), # -
- '835' => fixed(2), # -
- '836' => fixed(2), # -
- '837' => fixed(2), # -
- '838' => fixed(2), # -
- '839' => fixed(2), # -
-
- '850' => fixed(2), # Democratic People's Republic of Korea
- '851' => fixed(2), # Spare code
- '852' => fixed(2), # Hong Kong, China
- '853' => fixed(2), # Macao, China
- '854' => fixed(2), # Spare code
- '855' => fixed(2), # Cambodia (Kingdom of)
- '856' => fixed(2), # Lao People's Democratic Republic
- '857' => fixed(2), # Spare code
- '858' => fixed(2), # Spare code
- '859' => fixed(2), # Spare code
-
- '870' => fixed(2), # Inmarsat SNAC
- '871' => fixed(2), # Inmarsat (Atlantic Ocean-East)
- '872' => fixed(2), # Inmarsat (Pacific Ocean)
- '873' => fixed(2), # Inmarsat (Indian Ocean)
- '874' => fixed(2), # Inmarsat (Atlantic Ocean-West)
- '875' => fixed(2), # Reserved - Maritime Mobile Service Applications
- '876' => fixed(2), # Reserved - Maritime Mobile Service Applications
- '877' => fixed(2), # Reserved - Maritime Mobile Service Applications
- '878' => fixed(2), # Universal Personal Telecommunication Service (UPT)
- '879' => fixed(2), # Reserved for national non-commercial purposes
-
- '880' => fixed(2), # Bangladesh (People's Republic of)
- '881' => fixed(2), # International Mobile, shared code
- '882' => fixed(2), # International Networks, shared code
- '883' => fixed(2), # -
- '884' => fixed(2), # -
- '885' => fixed(2), # -
- '886' => fixed(2), # Reserved
- '887' => fixed(2), # -
- '888' => fixed(2), # Reserved for future global service
- '889' => fixed(2), # -
-
- '890' => fixed(2), # -
- '891' => fixed(2), # -
- '892' => fixed(2), # -
- '893' => fixed(2), # -
- '894' => fixed(2), # -
- '895' => fixed(2), # -
- '896' => fixed(2), # -
- '897' => fixed(2), # -
- '898' => fixed(2), # -
- '899' => fixed(2), # -
-
- '960' => fixed(2), # Maldives (Republic of)
- '961' => fixed(2), # Lebanon
- '962' => fixed(2), # Jordan (Hashemite Kingdom of)
- '963' => fixed(2), # Syrian Arab Republic
- '964' => fixed(2), # Iraq (Republic of)
- '965' => fixed(2), # Kuwait (State of)
- '966' => fixed(2), # Saudi Arabia (Kingdom of)
- '967' => fixed(2), # Yemen (Republic of)
- '968' => fixed(2), # Oman (Sultanate of)
- '969' => fixed(2), # Reserved - reservation currently under investigation
-
- '970' => fixed(2), # Reserved
- '971' => fixed(2), # United Arab Emirates
- '972' => fixed(2), # Israel (State of)
- '973' => fixed(2), # Bahrain (Kingdom of)
- '974' => fixed(2), # Qatar (State of)
- '975' => fixed(2), # Bhutan (Kingdom of)
- '976' => fixed(2), # Mongolia
- '977' => fixed(2), # Nepal
- '978' => fixed(2), # -
- '979' => fixed(2), # International Premium Rate Service (IPRS)
-
- '990' => fixed(2), # Spare code
- '991' => fixed(2), # Trial of a proposed new international telecommunication public correspondence service, shared code
- '992' => fixed(2), # Tajikistan (Republic of)
- '993' => fixed(2), # Turkmenistan
- '994' => fixed(2), # Azerbaijani Republic
- '995' => fixed(2), # Georgia
- '996' => fixed(2), # Kyrgyz Republic
- '997' => fixed(2), # Spare code
- '998' => fixed(2), # Uzbekistan (Republic of)
- '999' => fixed(2), # Reserved for possible future use within the Telecommunications for Disaster Relief (TDR) concept
- }
- }
+ @mapping ||= Phony::Countries::All.mapping
end
end
View
10 lib/phony/national_code.rb
@@ -21,6 +21,16 @@ def split national_number
[ndc, *@local_splitter.split(rest)]
end
+ def service? national_number
+ @national_splitter.service? national_number
+ end
+ def mobile? national_number
+ @national_splitter.mobile? national_number
+ end
+ def landline? national_number
+ !mobile?(ndc) && !service?(ndc)
+ end
+
# Is this national number a vanity number?
#
def vanity? national_number
View
18 lib/phony/national_codes/fixed_splitter.rb
@@ -4,6 +4,8 @@ module NationalCodes
class FixedSplitter
+ attr_writer :service_ndcs, :mobile_ndcs
+
@mapping = {}
# Get a splitter for the given format.
@@ -16,6 +18,8 @@ def self.instance_for size
def initialize size
@size = size
+ @service_ndcs = []
+ @mobile_ndcs = []
end
# Takes a national number and splits it into ndc and rest.
@@ -24,6 +28,20 @@ def split national_number
[national_number.slice!(0...@size), national_number]
end
+
+ def service? number
+ ndc, _ = split number
+ @service_ndcs.include? ndc
+ end
+ def mobile? number
+ ndc, _ = split number
+ @mobile_ndcs.include? ndc
+ end
+ def landline? number
+ ndc, _ = split number
+ !mobile?(ndc) && !service?(ndc)
+ end
+
end
end
View
22 lib/phony/national_codes/variable_splitter.rb
@@ -8,7 +8,7 @@ class VariableSplitter < FixedSplitter
#
def initialize fallback, ndc_map
super fallback
- @ndcs, @services = restructure ndc_map
+ @ndcs, @special_ndcs = restructure ndc_map
end
# Takes a national number and splits it into ndc and rest.
@@ -35,11 +35,29 @@ def split national_number
return super(fallback_number)
end
+ def service? number
+ ndc, _ = split number
+ @special_ndcs[:service].include? ndc
+ end
+ def mobile? number
+ ndc, _ = split number
+ @special_ndcs[:mobile].include? ndc
+ end
+ def landline? number
+ ndc, _ = split number
+ !mobile?(ndc) && !service?(ndc)
+ end
private
def restructure ndc_map
- [optimize(ndc_map.values.flatten), nil] # TODO extract_services ndc_map
+ [optimize(ndc_map.values.flatten), extract_special(ndc_map)] # TODO extract_services ndc_map
+ end
+
+ def extract_special ndc_map
+ ndc_map.each_pair do |type, ndc|
+
+ end
end
# Optimizes and restructures the given ndcs array.
View
72 lib/phony/ndc/austria.rb
@@ -1,72 +0,0 @@
-# Splits a national number into a fixed size NDC and rest.
-#
-module Phony
- module NDC
- class Austria < Prefix
-
- ndc_length 1..4
- local_format 10
- ndcs :normal => ['1',# Vienna
- '57', # -
- '59', # -
- '89', # Routing Number
- '316', # Graz
- '501', # -
- '502', # -
- '503', # -
- '504', # -
- '505', # -
- '506', # -
- '507', # -
- '508', # -
- '509', # -
- '512', # Innsbruck
- '517', # -
- '662', # Salzburg
- '720', #
- '732', # Linz
- ],
- :mobile => [
- '67', # Mobile Services
- '68', # Mobile Services
- '644', # Mobile Services
- '650', # Mobile Services
- '651', # Mobile Services
- '652', # Mobile Services
- '653', # Mobile Services
- '655', # Mobile Services
- '657', # Mobile Services
- '659', # Mobile Services
- '660', # Mobile Services
- '661', # Mobile Services
- '663', # Mobile Services
- '664', # Mobile Services
- '665', # Mobile Services
- '666', # Mobile Services
- '667', # Mobile Services
- '668', # Mobile Services
- '669', # Mobile Services
- ],
- :service => [
- '710', # Service Number
- '711', # Service Number
- '718', # Service Number
- '730', # Service Number
- '740', # Service Number
- '780', # Service Number
- '800', # Service Number
- '802', # Service Number
- '804', # Service Number
- '810', # Service Number
- '820', # Service Number
- '821', # Service Number
- '828', # Service Number
- '900', # Service Number
- '901', # Service Number
- '930', # Service Number
- '931', # Service Number
- '939' # Service Number
- ]
- end
- end
-end
View
113 lib/phony/ndc/fixed_size.rb
@@ -1,113 +0,0 @@
-# Splits a national number into a fixed size NDC and rest.
-#
-module Phony
- module NDC
-
- def self.fixed(national_code_length = 2, options = {})
- klass = Class.new FixedSize
- split_sizes, service_ndcs = klass.extract options
- klass.service_ndc_strategy Phony::NDC::ServiceNdcStrategy.new(national_code_length, service_ndcs)
- klass.fixed_ndc_strategy Phony::NDC::FixedNdcStrategy.new(national_code_length, split_sizes)
- klass.format options[:format] || '%s%s%s%s%s'
- klass.local_format split_sizes
- klass
- end
-
- class FixedSize < Splitter
-
- def self.extract options
- [options[:local] || [3, 2, 2], options[:service_ndcs] || []]
- end
-
- # Sets the strategy object to handle service numbers
- def self.service_ndc_strategy strategy
- @service_ndc_strategy = strategy
- end
-
- # Sets the strategy object to handle normal numbers (non service)
- def self.fixed_ndc_strategy strategy
- @fixed_ndc_strategy = strategy
- @ndc_strategy = @fixed_ndc_strategy
- end
-
- # Returns true if the number is a service number
- def self.service_ndc? number
- @service_ndc_strategy.service_ndc? number
- end
-
- # Define a format for the country's national format.
- #
- # Default is NN followed by a space.
- #
- def self.ndc_format
- @ndc_format || @ndc_format = @ndc_strategy.ndc_format
- end
-
- # A faster split method than the prefix splitter offers.
- #
- def self.split_ndc number
- number = number.dup
- @ndc_strategy = if service_ndc? number
- @service_ndc_strategy
- else
- @fixed_ndc_strategy
- end
- @ndc_strategy.split_ndc number
- end
-
- def self.split_local number
- @ndc_strategy.split_local number
- end
-
- end
-
- # Base class for all Strategies
- class NdcStrategy
- attr_reader :split_sizes
-
- def initialize national_code_length, split_sizes
- @national_code_length = national_code_length
- @split_sizes = split_sizes
- end
-
- def split_local number
- local = []
- split_sizes.each do |size|
- local << number.slice!(0..size-1)
- break if number.empty?
- end
- local
- end
-
- def ndc_format
- '%s' + (@national_code_length == 0 ? '' : '%s')
- end
-
- end
-
- class ServiceNdcStrategy < NdcStrategy
- def initialize national_code_length, service_ndcs
- super national_code_length, [2, 2, 2]
- @service_ndcs = service_ndcs.flatten
- @service_ndc_regexp = Regexp.compile "^(#{@service_ndcs.join('|')})"
- end
-
- def service_ndc? number
- @service_ndcs && !@service_ndcs.empty? && @match = @service_ndc_regexp.match(number)
- end
-
- def split_ndc number
- [number.slice!(0..@match[0].length-1), number]
- end
- end
-
- class FixedNdcStrategy < NdcStrategy
-
- def split_ndc number
- @national_code_length.zero? ? ['', number] : [number.slice!(0..@national_code_length-1), number]
- end
-
- end
-
- end
-end
View
159 lib/phony/ndc/germany.rb
@@ -1,159 +0,0 @@
-# Splits a german national number into a fixed size NDC and rest.
-#
-# Note: We do ignore places with 5 numbers as NDC, like Groß Glienicke (33201)
-#
-module Phony
- module NDC
- class Germany < Prefix
-
- ndc_length 2..4
- local_format 3, 10
- ndcs :normal => ['10', # Call-By-Call
- '11', # formerly Value Added Services
- '12', # Innovative Services
- '13', # Voting and Lottery Numbers
- '30', # Berlin
- '32', # Non Geographical Numbers
- '40', # Hamburg
- '69', # Frankfurt am Main
- '89', # München
- '201', # Essen
- '202', # Wuppertal
- '203', # Duisburg
- '208', # Mühlheim an der Ruhr
- '209', # Gelsenkirchen
- '211', # Düsseldorf
- '212', # Solingen
- '214', # Leverkusen
- '221', # Köln
- '228', # Bonn
- '231', # Dortmund
- '234', # Bochum
- '241', # Aachen
- '251', # Münster
- '261', # Koblenz
- '271', # Siegen
- '281', # Wesel
- '291', # Meschede
- '310', # Test Number Long Distance
- '311', # Test Number Local
- '335', # Frankfurt (Oder)
- '340', # Dessau
- '341', # Leipzig
- '345', # Halle (Saale)
- '351', # Dresden
- '355', # Cottbus
- '361', # Erfurt
- '365', # Gera
- '371', # Chemnitz
- '375', # Zwickau
- '381', # Rostock
- '385', # Schwerin
- '391', # Magdeburg
- '395', # Neubrandenburg
- '421', # Bremen
- '431', # Kiel
- '441', # Oldenburg
- '461', # Flensburg
- '471', # Bremerhaven
- '481', # Heide
- '491', # Leer
- '511', # Hannover
- '521', # Bielefeld
- '531', # Braunschweig
- '541', # Osnabrück
- '551', # Göttingen
- '561', # Kassel
- '571', # Minden
- '581', # Uelzen
- '591', # Lingen (Ems)
- '611', # Wiesbaden
- '621', # Mannheim / Ludwigshafen
- '631', # Kaiserslautern
- '641', # Gießen
- '651', # Trier
- '661', # Fulda
- '671', # Bad Kreuznach
- '681', # Saarbrücken
- '700', # Personal Numbers
- '701', # Personal Numbers, reserved
- '711', # Stuttgart
- '721', # Karlsruhe
- '731', # Ulm
- '741', # Rottweil
- '751', # Ravensburg
- '761', # Freiburg im Breisgau
- '771', # Donaueschingen
- '781', # Offenburg
- '791', # Schwäbisch Hall
- '800', # Toll-Free Numbers
- '801', # Toll-Free Numbers, reserved
- '811', # Hallbergmoos
- '821', # Augsburg
- '831', # Kempten
- '841', # Ingolstadt
- '851', # Passau
- '861', # Traunstein
- '871', # Landshut
- '881', # Weilheim in Oberbayern
- '900', # Premium Rate Numbers
- '901', # Premium Rate Numbers, reserved
- '902', # Replacement for 0137/0138
- '906', # Donauwörth
- '911', # Nürnberg
- '921', # Bayreuth
- '931', # Würzburg
- '941', # Regensburg
- '951', # Bamberg
- '961', # Weiden
- '971', # Bad Kissingen
- '981', # Ansbach
- '991' # Deggendorf
- ],
- :mobile => [
- '150', # Group3G/Quam
- '151', # T-Mobile
- '152', # Vodafone
- '155', # E-Plus
- '156', # Mobilcom
- '157', # E-Plus
- '159', # O2
- '160', # T-Mobile
- '161', # C-Netz
- '162', # Vodafone
- '163', # E-Plus
- '164', # Cityruf
- '165', # Quix
- '166', # Telmi
- '168', # Scall
- '169', # Cityruf, Scall, Skyper (e*cityruf, e*message, e*skyper)
- '170', # T-Mobile
- '171', # T-Mobile
- '172', # Vodafone
- '173', # Vodafone
- '174', # Vodafone
- '175', # T-Mobile
- '176', # O2 Germany
- '177', # E-Plus
- '178', # E-Plus
- '179', # O2 Germany
- '181', # IVPNs
- ],
- :service => [
- '182', # Closed User Group
- '183', # Closed User Group
- '184', # Closed User Group
- '185', # Closed User Group
- '186', # Closed User Group
- '187', # Closed User Group
- '189', # Closed User Group
- '190', # former Premium Rate Services
- '191', # Online Services
- '192', # Online Services
- '193', # Online Services
- '194', # Online Services
- '199', # Network-Internal Traffic Control
- ]
- end
- end
-end
View
67 lib/phony/ndc/prefix.rb
@@ -1,67 +0,0 @@
-# Splits a national number using a prefix code.
-#
-# DSL:
-# ndc_length: the length range of the ndcs. For austria it is 1..4.
-# format: the format of the dcs and rest. For austria it is '%s %s', default is '%s %s %s %s'.
-# ndcs: an array of the prefix codes. For germany it starts with ['10', '11', '12', '13', '30'…
-#
-module Phony
- module NDC
- class Prefix < Splitter
-
- # Splits the number into ndc and rest.
- #
- def self.split_ndc(number)
- number = number.dup
- presumed_code = ''
- presumed_code << number.slice!(0..@min_ndc_length-2) if @min_ndc_length && @min_ndc_length > 1
- (@min_ndc_length || 1).upto(@max_ndc_length) do |i|
- presumed_code << number.slice!(0..0)
- sized_ndcs = @ndcs[i]
- break unless sized_ndcs && !sized_ndcs.include?(presumed_code)
- end
- return [presumed_code, number]
- end
-
- protected
-
- # Define the length range for the country's ndcs.
- #
- def self.ndc_length(range)
- @min_ndc_length, @max_ndc_length = range.min, range.max
- end
-
- # Define the list of NDCs.
- #
- # Note: For optimization, if the country has a max ndc length of 4,
- # only enter ndcs up to the length of 3 letters.
- # If the algorithm fails to match an ndc, it will assume it is a 4 digit ndc.
- #
- # e.g. We have NDCs '1', '22', '333', and '4444'.
- # Only set
- # ndcs '1', '22', '333'
- # and
- # length 1..4
- # '4444' will be "recognized" because the algorithm does not find a shorter ndc (assuming it
- # is a correct prefix code) and stop at the max ndc length of 4, given by the length method.
- #
- def self.ndcs(*ndcs_ary)
- @ndcs = optimize(ndcs_ary)
- end
-
- private
-
- # Optimizes and restructures the given ndcs array.
- #
- def self.optimize(ndcs_ary)
- ndcs = {}
- ndcs_ary.each do |ndc|
- ndcs[ndc.length] ||= []
- ndcs[ndc.length] << ndc
- end
- ndcs
- end
-
- end
- end
-end
View
81 lib/phony/ndc/splitter.rb
@@ -1,81 +0,0 @@
-# Splits a national number into a fixed size NDC and rest.
-#
-module Phony
- module NDC
- class Splitter
-
- # Sets the local grouping format.
- #
- # Examples
- # * local_format 3, 2, 2 # Switzerland, 364 35 33, thus: 3-2-2.
- # * local_format 2, 2, 2, 2 # France, 12 34 56 78, thus: 2-2-2-2.
- #
- def self.local_format *split_sizes
- @split_sizes = split_sizes.flatten
- format((['%s']*@split_sizes.size).join('%s'))
- end
-
- # Define a format for the country's local format.
- #
- # Examples
- # * format '%s%s%s%s%s' # Switzerland, spaces between the groups, e.g. 364˽35˽32
- #
- def self.format format
- @format = format
- @format_with_ndc = ndc_format + @format
- end
-
- # Define a format for the country's national format.
- #
- # Default is NN followed by a space character.
- #
- def self.ndc_format
- '%s%s'
- end
-
- # Split an E164 number without country code into its NDC-Local parts.
- #
- # Examples
- # * split '443643533' # => ['44', '364', '35', '33'] # (Switzerland)
- #
- def self.split number
- ndc_part = split_ndc number
- [ndc_part, split_local(ndc_part.pop)].flatten # TODO ndc_part + split_local(ndc_part.pop)
- end
-
- # Split a local number according to an assumed country specific format.
- #
- # Examples
- # * split '3643533' # => ['364', '35', '33'] # (Switzerland)
- #
- def self.split_local number
- local = []
- @split_sizes.each do |size|
- local << number.slice!(0..size-1)
- break if number.empty?
- end
- local
- end
-
- # Formats the given E164 Number (NDC-Local without CC) according to the country specific format / ndcs splitting.
- #
- def self.formatted number, space = ' '
- formatted_with_spaces @format_with_ndc, split(number), space
- end
-
- # Formats the given local number according to the country specific format.
- #
- def self.locally_formatted number, space = ' '
- formatted_with_spaces @format, split_local(number), space
- end
-
- def self.formatted_with_spaces format, split_number, space
- space ||= ' '
- number_and_spaces = (split_number).zip([space]*split_number.size).flatten
- format % number_and_spaces
- end
-
- end
-
- end
-end
View
40 spec/lib/ndc/austria_spec_old.rb
@@ -1,40 +0,0 @@
-require 'spec_helper'
-
-describe Phony::NDC::Austria do
-
- attr_reader :splitter
- before(:each) do
- @splitter = Phony::NDC::Austria
- end
-
- describe "split" do
- it "should handle Vienna" do
- splitter.split('198110').should == ['1', '98110']
- end
- it "should handle some mobile services" do
- splitter.split('66914093902').should == ['669', '14093902']
- end
- it "should handle Graz" do
- splitter.split('3161234567891').should == ['316', '1234567891']
- end
- it "should handle Rohrau" do
- splitter.split('2164123456789').should == ['2164', '123456789']
- end
- end
-
- describe "formatted" do
- it "should format Vienna" do
- splitter.formatted('198110').should == '1 98110'
- end
- it "should format some mobile services" do
- splitter.formatted('671234567891').should == '67 1234567891'
- end
- it "should format Graz" do
- splitter.formatted('316123456789').should == '316 123456789'
- end
- it "should format Rohrau" do
- splitter.formatted('216412345678').should == '2164 12345678'
- end
- end
-
-end
View
77 spec/lib/ndc/fixed_size_spec_old.rb
@@ -1,77 +0,0 @@
-require 'spec_helper'
-
-describe Phony::NDC::FixedSize do
-
- attr_reader :splitter
-
- # describe 'size 0' do
- # before(:each) do
- # @splitter = Phony::NDC.fixed 0
- # end
- #
- # describe "formatted" do
- # it "should format correctly" do
- # # TODO not perfect, but does this case make sense?
- # splitter.formatted('1234567').should == '123 45 67 '
- # end
- # end
- # end
-
- # describe "service numbers" do
- # before(:each) do
- # @ndc = Phony::NDC.fixed(1, :local => [2, 2, 2, 2], :service_ndcs => ['8']) # france
- # end
- # it "recognizes a number as a service" do
- # @ndc.service_ndc?('33812345678').should == true
- # end
- # it "does recognize a number as a non service" do
- # @ndc.service_ndc?('33112345678').should == false
- # end
- # end
-
- describe "size 1" do
- before(:each) do
- @splitter = Phony::NDC.fixed 1
- end
-
- describe "formatted" do
- it "should format correctly" do
- splitter.formatted('12345678').should == '1 234 56 78'
- end
- end
- end
- describe "size 2" do
- before(:each) do
- @splitter = Phony::NDC.fixed 2
- end
-
- describe "formatted" do
- it "should format correctly" do
- splitter.formatted('123456789').should == '12 345 67 89'
- end
- end
- end
- describe "size 3" do
- before(:each) do
- @splitter = Phony::NDC.fixed 3
- end
-
- describe "formatted" do
- it "should format correctly" do
- splitter.formatted('1234567890').should == '123 456 78 90'
- end
- end
- end
- describe "size 4" do
- before(:each) do
- @splitter = Phony::NDC.fixed 4
- end
-
- describe "formatted" do
- it "should format correctly" do
- splitter.formatted('12345678901').should == '1234 567 89 01'
- end
- end
- end
-
-end
View
40 spec/lib/ndc/germany_spec_old.rb
@@ -1,40 +0,0 @@
-require 'spec_helper'
-
-describe Phony::NDC::Germany do
-
- attr_reader :splitter
- before(:each) do
- @splitter = Phony::NDC::Germany
- end
-
- describe "split" do
- it "should handle Berlin" do
- splitter.split('3038625454').should == ['30', '386', '25454']
- end
- it "should handle Cologne" do
- splitter.split('22137683323').should == ['221', '376', '83323']
- end
- it "should handle Freiburg im Breisgau" do
- splitter.split('7614767676').should == ['761', '476', '7676']
- end
- it "should handle Nettetal-Lobberich" do
- splitter.split('21535100').should == ['2153', '510', '0']
- end
- end
-
- describe "formatted" do
- it "should format Berlin" do
- splitter.formatted('3038625454').should == '30 386 25454'
- end
- it "should format Cologne" do
- splitter.formatted('22137683323').should == '221 376 83323'
- end
- it "should format Freiburg im Breisgau" do
- splitter.formatted('7614767676').should == '761 476 7676'
- end
- it "should format Nettetal-Lobberich" do
- splitter.formatted('21535100').should == '2153 510 0'
- end
- end
-
-end
View
25 spec/lib/ndc/prefix_spec_old.rb
@@ -1,25 +0,0 @@
-require 'spec_helper'
-
-describe Phony::NDC::Prefix do
-
- attr_reader :prefix
- before(:each) do
- @prefix = Phony::NDC::Prefix
- end
-
- describe "optimize" do
- it "should convert an array into a hash" do
- prefix.optimize(Array.new).should be_kind_of(Hash)
- end
- it "should convert an empty array into an empty thing" do
- prefix.optimize([]).should be_empty
- end
- it "should convert an empty array into an empty hash" do
- prefix.optimize([]).should == {}
- end
- it "should convert a various string length array into a hash(size=>array(entries))" do
- prefix.optimize(['1', '22', '333', '4444']).should == { 1 => ['1'], 2 => ['22'], 3 => ['333'], 4 => ['4444'] }
- end
- end
-
-end
View
59 spec/lib/ndc/splitter_spec_old.rb
@@ -1,59 +0,0 @@
-require 'spec_helper'
-
-describe Phony::NDC::Splitter do
-
- attr_reader :splitter
-
- describe "DSL" do
- before(:each) do
- @splitter = Phony::NDC::Splitter
- end
- describe "local" do
- it "should set @split_sizes" do
- splitter.local 3,2,2
- splitter.instance_variable_get(:@split_sizes).should == [3,2,2]
- end
- it "should call format" do
- splitter.should_receive(:format).once.with('%s%s%s%s%s')
- splitter.local 3,2,2
- end
- end
- describe "format" do
- it "should set @format" do
- splitter.format 'any format'
- splitter.instance_variable_get(:@format).should == 'any format'
- end
- it "should set @format_with_ndc" do
- splitter.format 'any format'
- splitter.instance_variable_get(:@format_with_ndc).should == '%s%sany format'
- end
- end
- end
-
- describe "split_local" do
- describe "with local" do
- before(:each) do
- @splitter = Phony::NDC.fixed(2, :local => [3, 2, 2])
- end
- it "should split it according to the local format" do
- splitter.split_local('3643533').should == ['364', '35', '33']
- end
- end
- end
-
- describe 'formatted_with_spaces' do
- before(:each) do
- @splitter = Phony::NDC.fixed(2, :local => [3, 2, 2])
- end
- it 'should format with spaces' do
- splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], ' ').should == '364 35 33'
- end
- it 'should format with spaces' do
- splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], '').should == '3643533'
- end
- it 'should format with dashes' do
- splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], :-).should == '364-35-33'
- end
- end
-
-end
View
14 spec/lib/phony_spec.rb
@@ -171,6 +171,20 @@
end
end
+ describe "service?" do
+
+ end
+ describe "mobile?" do
+ # TODO Test dirty numbers.
+ #
+ it 'is correct' do
+ Phony.mobile?('49172123456').should == true
+ end
+ it 'is correct' do
+ Phony.mobile?('12172123456').should == false
+ end
+ end
+
# describe 'regarding vanity' do
# describe 'vanity_number?' do
# it {Phony.vanity?('41800 WEGGLI').should be_true}

0 comments on commit 03dc48d

Please sign in to comment.
Something went wrong with that request. Please try again.