Module: Dialy
- Defined in:
- lib/dialy/data/at.rb,
lib/dialy/data.rb,
lib/dialy/data.rb,
lib/dialy/config.rb,
lib/dialy/data/ch.rb,
lib/dialy/data/de.rb,
lib/dialy/formatter.rb,
lib/dialy/data/countries.rb
Overview
Country calling codes Taken from de.wikipedia.org/wiki/L%C3%A4ndervorwahlliste_sortiert_nach_Nummern
Defined Under Namespace
Modules: Config
Constant Summary collapse
- AREA_CODES =
{}
- AC_RANGE =
{}
- CC_RANGE =
(min_length..max_length)
- COUNTRY_CODES =
[ 1, 20, 212, 213, 216, 218, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 27, 290, 291, 297, 298, 299, 30, 31, 32, 33, 34, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 36, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 385, 386, 387, 388, 389, 39, 40, 41, 420, 421, 423, 43, 44, 45, 46, 47, 48, 49, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 51, 52, 53, 54, 55, 56, 57, 58, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 60, 61, 62, 63, 64, 649, 65, 66, 670, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 685, 686, 687, 688, 689, 690, 691, 692, 7, 800, 808, 81, 82, 84, 850, 852, 853, 855, 856, 86, 870, 875, 876, 877, 878, 879, 880, 881, 882, 883, 886, 90, 91, 92, 93, 94, 95, 960, 961, 962, 963, 964, 965, 966, 967, 968, 970, 971, 972, 973, 974, 975, 976, 977, 979, 98, 991, 992, 993, 994, 995, 996, 998, 999 ].freeze
Class Method Summary collapse
Class Method Details
.format(value) ⇒ Object
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 |
# File 'lib/dialy/formatter.rb', line 2 def self.format(value) # Remove all but digits and + plain = value.gsub(/[^+0-9]/, '') # Error check: Plus (+) is only allowed as first character raise ArgumentError if plain.count('+') > 1 raise ArgumentError if plain.index('+').to_i > 0 # Step 1: Find country code country_code = nil if match = plain.match(/^(\+|00)(\d{1,3})/) plain.slice!(0,match[1].length) # Because the length of a country code is not fixed, we have to do # multiple searches. Start with the minimum length and go to the # maxium until an area code is found. CC_RANGE.each do |len| part = match[2][0,len].to_i if COUNTRY_CODES.include?(part) country_code = part plain.slice!(0,len) break end end raise ArgumentError.new("Unknown country code: #{match[2]}") unless country_code else country_code = Config[:default_country_code] end # Delete leading "0" plain.slice!(0,1) if plain.match /^0/ # Step 2: Find area code area_code = nil if AREA_CODES[country_code] # Because the length of an area code is not fixed, we have to do # multiple searches. Start with the minimum length and go to the # maxium until an area code is found. AC_RANGE[country_code].each do |len| part = plain[0,len].to_i if AREA_CODES[country_code].include?(part) area_code = part plain.slice!(0,len) break end end raise ArgumentError.new("Area code not found") unless area_code end # Finished. Build result "+#{country_code} #{[ area_code, plain ].compact.join(' ')}" end |