phonie 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile.lock +10 -0
  4. data/LICENSE +20 -0
  5. data/Rakefile +13 -0
  6. data/Readme.rdoc +153 -0
  7. data/lib/phonie.rb +7 -0
  8. data/lib/phonie/country.rb +120 -0
  9. data/lib/phonie/data/phone_countries.yml +1683 -0
  10. data/lib/phonie/phone.rb +215 -0
  11. data/lib/phonie/support.rb +78 -0
  12. data/lib/phonie/version.rb +3 -0
  13. data/phonie.gemspec +18 -0
  14. data/test/countries/ae_test.rb +15 -0
  15. data/test/countries/af_test.rb +12 -0
  16. data/test/countries/al_test.rb +12 -0
  17. data/test/countries/ar_test.rb +12 -0
  18. data/test/countries/at_test.rb +14 -0
  19. data/test/countries/au_test.rb +48 -0
  20. data/test/countries/ba_test.rb +9 -0
  21. data/test/countries/bd_test.rb +17 -0
  22. data/test/countries/be_test.rb +120 -0
  23. data/test/countries/bg_test.rb +13 -0
  24. data/test/countries/bo_test.rb +12 -0
  25. data/test/countries/br_test.rb +12 -0
  26. data/test/countries/bt_test.rb +9 -0
  27. data/test/countries/by_test.rb +12 -0
  28. data/test/countries/bz_test.rb +12 -0
  29. data/test/countries/ca_test.rb +20 -0
  30. data/test/countries/cr_test.rb +12 -0
  31. data/test/countries/cy_test.rb +12 -0
  32. data/test/countries/cz_test.rb +12 -0
  33. data/test/countries/de_test.rb +18 -0
  34. data/test/countries/dk_test.rb +12 -0
  35. data/test/countries/dz_test.rb +12 -0
  36. data/test/countries/ec_test.rb +12 -0
  37. data/test/countries/ee_test.rb +12 -0
  38. data/test/countries/eg_test.rb +9 -0
  39. data/test/countries/et_test.rb +11 -0
  40. data/test/countries/fi_test.rb +12 -0
  41. data/test/countries/fr_test.rb +22 -0
  42. data/test/countries/gb_test.rb +262 -0
  43. data/test/countries/ge_test.rb +12 -0
  44. data/test/countries/gh_test.rb +9 -0
  45. data/test/countries/gr_test.rb +9 -0
  46. data/test/countries/gt_test.rb +12 -0
  47. data/test/countries/gu_test.rb +9 -0
  48. data/test/countries/gy_test.rb +9 -0
  49. data/test/countries/hr_test.rb +75 -0
  50. data/test/countries/hu_test.rb +12 -0
  51. data/test/countries/il_test.rb +12 -0
  52. data/test/countries/in_test.rb +45 -0
  53. data/test/countries/ir_test.rb +13 -0
  54. data/test/countries/ke_test.rb +12 -0
  55. data/test/countries/lk_test.rb +9 -0
  56. data/test/countries/ng_test.rb +9 -0
  57. data/test/countries/nl_test.rb +383 -0
  58. data/test/countries/no_test.rb +12 -0
  59. data/test/countries/np_test.rb +15 -0
  60. data/test/countries/ph_test.rb +9 -0
  61. data/test/countries/pk_test.rb +9 -0
  62. data/test/countries/pt_test.rb +129 -0
  63. data/test/countries/qa_test.rb +9 -0
  64. data/test/countries/rs_test.rb +15 -0
  65. data/test/countries/sa_test.rb +9 -0
  66. data/test/countries/se_test.rb +478 -0
  67. data/test/countries/si_test.rb +19 -0
  68. data/test/countries/sv_test.rb +12 -0
  69. data/test/countries/to_test.rb +12 -0
  70. data/test/countries/ua_test.rb +17 -0
  71. data/test/countries/us_test.rb +24 -0
  72. data/test/countries/uy_test.rb +20 -0
  73. data/test/countries/za_test.rb +19 -0
  74. data/test/countries/zw_test.rb +12 -0
  75. data/test/country_test.rb +27 -0
  76. data/test/extension_test.rb +30 -0
  77. data/test/phone_test.rb +137 -0
  78. data/test/test_helper.rb +37 -0
  79. data/test_usa_phones_with_extensions.csv +99 -0
  80. metadata +130 -0
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## Slovenia
4
+ class SITest < Phonie::TestCase
5
+ def test_local
6
+ #Maribor
7
+ parse_test('+ 386 2 23 46 611', '386', '2', '2346611')
8
+ end
9
+
10
+ def test_local_2
11
+ # Koper
12
+ parse_test('+ 386 5 23 46 611', '386', '5', '2346611', "Slovenia", false)
13
+ end
14
+
15
+ def test_mobile
16
+ # Mobitel
17
+ parse_test('+386 51 258999', '386', '51', '258999', "Slovenia", true)
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## El Salvador
4
+ class SVTest < Phonie::TestCase
5
+ def test_local
6
+ parse_test('+50321234567', '503', '2', '1234567', 'El Salvador', false)
7
+ end
8
+
9
+ def test_mobile
10
+ parse_test('+50371234567', '503', '7', '1234567', 'El Salvador', true)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## Tonga
4
+ class TOTest < Phonie::TestCase
5
+ def test_local
6
+ parse_test('+67679321', '676', '79', '321', "Tonga", false)
7
+ end
8
+
9
+ def test_mobile
10
+ parse_test('67688321', '676', '88', '321', "Tonga", true)
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## Ukraine
4
+ class UATest < Phonie::TestCase
5
+
6
+ def test_local
7
+ parse_test('+380 57 711 22 33', '380', '57', '7112233', "Ukraine", false)
8
+ end
9
+
10
+ def test_mobile
11
+ parse_test('+380-50-111-22-33', '380', '50', '1112233', "Ukraine", true)
12
+ end
13
+
14
+ def test_mobile2
15
+ parse_test('+380-66-042-22-01', '380', '66', '0422201', "Ukraine", true)
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## United States
4
+ class USTest < Phonie::TestCase
5
+
6
+ def test_local
7
+ parse_test('+1 251 123 4567', '1', '251', '1234567', 'United States')
8
+ end
9
+
10
+ def test_tollfree
11
+ parse_test('+1 800 555 3456', '1', '800', '5553456', 'United States')
12
+ end
13
+
14
+ def test_long_with_default_country_code
15
+ Phonie::Phone.default_country_code = '1'
16
+ parse_test('2069735100', '1', '206', '9735100', 'United States')
17
+ end
18
+
19
+ def test_short_with_default_country_code_and_area_code
20
+ Phonie::Phone.default_country_code = '1'
21
+ Phonie::Phone.default_area_code = '206'
22
+ parse_test('9735100', '1', '206', '9735100', 'United States')
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## Uruguay
4
+ # source: http://en.wikipedia.org/wiki/Telephone_numbers_in_Uruguay
5
+ class UYTest < Phonie::TestCase
6
+ # 02 Montevideo
7
+ def test_montevideo
8
+ parse_test('+598 2 1234567', '598', '2', '1234567')
9
+ end
10
+
11
+ # 042 Maldonado
12
+ def test_maldonado
13
+ parse_test('+598 42 123456', '598', '42', '123456', "Uruguay", false)
14
+ end
15
+
16
+ # 09 Mobile phones
17
+ def test_mobile_phones
18
+ parse_test('+598 99 570110', '598', '99', '570110', "Uruguay", true)
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## South Africa
4
+ class ZATest < Phonie::TestCase
5
+ def test_local
6
+ # Telkom
7
+ parse_test('+27 11 555 5555', '27', '11', '5555555', "South Africa", false)
8
+ end
9
+
10
+ def test_mobile
11
+ # Vodacom
12
+ parse_test('+27 82 555 5555', '27', '82', '5555555', "South Africa", true)
13
+ end
14
+
15
+ def test_tollfree
16
+ # Telkom
17
+ parse_test('+27 800 123 321', '27', '800', '123321', "South Africa", false)
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## Zimbabwe
4
+ class ZWTest < Phonie::TestCase
5
+ def test_local
6
+ parse_test('+263454168409', '263', '4', '54168409', "Zimbabwe", false)
7
+ end
8
+
9
+ def test_mobile
10
+ parse_test('263774168409', '263', '77', '4168409', "Zimbabwe", true)
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class CountryTest < Phonie::TestCase
4
+ def test_find_by_country_name
5
+ country = Phonie::Country.find_by_name('canada')
6
+ assert_equal country.name, "Canada"
7
+
8
+ country = Phonie::Country.find_by_name('Canada')
9
+ assert_equal country.name, "Canada"
10
+
11
+ assert_nil Phonie::Country.find_by_name(nil)
12
+ assert_nil Phonie::Country.find_by_country_code(nil)
13
+ assert_equal [], Phonie::Country.find_all_by_phone_code(nil)
14
+ end
15
+
16
+ def test_find_by_country_code
17
+ country = Phonie::Country.find_by_country_code('NO')
18
+ assert_equal country.name, "Norway"
19
+ end
20
+
21
+ def test_find_all_by_phone_code
22
+ countries = Phonie::Country.find_all_by_phone_code('47')
23
+ assert_equal countries.length, 1
24
+ assert_equal countries.first.name, "Norway"
25
+ end
26
+
27
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class ExtensionTest < Test::Unit::TestCase
4
+
5
+ def test_parse_usa_long_with_simple_extension
6
+ pn = Phonie::Phone.parse "+1 2069735100 x143"
7
+
8
+ assert_not_nil pn, %Q{parse should pass}
9
+ assert_equal '9735100', pn.number
10
+ assert_equal '206', pn.area_code
11
+ assert_equal '1', pn.country_code
12
+ assert_equal '143', pn.extension
13
+ end
14
+
15
+ def test_to_s_with_extension
16
+ pn = Phonie::Phone.new '5125486', '91', '385', '143'
17
+ assert_equal '+385915125486x143', pn.format(:default_with_extension)
18
+ end
19
+
20
+ def test_format_with_extension
21
+ pn = Phonie::Phone.new '5125486', '91', '385', '143'
22
+ assert_equal '(091)/512-5486 x 143', pn.format('(%A)/%f-%l x %x')
23
+ end
24
+
25
+ def test_validation_keeps_extension
26
+ number = "555-555-1212 ext 1234"
27
+ Phonie::Phone.valid?(number)
28
+ assert_equal "555-555-1212 ext 1234", number
29
+ end
30
+ end
@@ -0,0 +1,137 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class PhoneTest < Phonie::TestCase
4
+
5
+ def test_is_mobile
6
+ assert Phonie::Phone.is_mobile?("918124452900")
7
+ end
8
+
9
+ def test_number_without_country_code_initialize
10
+ Phonie::Phone.default_country_code = nil
11
+
12
+ assert_raise RuntimeError do
13
+ pn = Phonie::Phone.new '5125486', '91'
14
+ end
15
+ end
16
+
17
+ def test_number_without_country_and_area_code_initialize
18
+ Phonie::Phone.default_country_code = nil
19
+ Phonie::Phone.default_area_code = nil
20
+
21
+ assert_raise RuntimeError do
22
+ pn = Phonie::Phone.new '451588'
23
+ end
24
+ end
25
+
26
+ def test_number_with_default_area_code_initialize
27
+ Phonie::Phone.default_country_code = '385'
28
+ Phonie::Phone.default_area_code = '47'
29
+
30
+ pn = Phonie::Phone.new '451588'
31
+ assert pn.number == '451588'
32
+ assert pn.area_code == '47'
33
+ assert pn.country_code == '385'
34
+ end
35
+
36
+ def test_number_with_default_country_code_initialize
37
+ Phonie::Phone.default_country_code = '386'
38
+
39
+ pn = Phonie::Phone.new '5125486', '91'
40
+ assert pn.number == '5125486'
41
+ assert pn.area_code == '91'
42
+ assert pn.country_code == '386'
43
+ end
44
+
45
+ def test_number_with_country_code_initialize
46
+ Phonie::Phone.default_country_code = '387'
47
+
48
+ pn = Phonie::Phone.new '5125486', '91', '385'
49
+ assert pn.number == '5125486'
50
+ assert pn.area_code == '91'
51
+ assert pn.country_code == '385'
52
+ end
53
+
54
+ def test_parse_empty
55
+ assert_equal Phonie::Phone.parse(''), nil
56
+ assert_equal Phonie::Phone.parse(nil), nil
57
+ end
58
+
59
+ def test_parse_short_without_special_characters_without_country
60
+ Phonie::Phone.default_country_code = nil
61
+
62
+ assert_nil Phonie::Phone.parse "0915125486"
63
+
64
+ assert_raise RuntimeError do
65
+ Phonie::Phone.parse! "0915125486"
66
+ end
67
+ end
68
+
69
+ def test_parse_short_with_special_characters_without_country
70
+ Phonie::Phone.default_country_code = nil
71
+
72
+ assert_nil Phonie::Phone.parse "091/512-5486"
73
+
74
+ assert_raise RuntimeError do
75
+ Phonie::Phone.parse! "091/512-5486"
76
+ end
77
+ end
78
+
79
+ def test_to_s
80
+ Phonie::Phone.default_country_code = nil
81
+ pn = Phonie::Phone.new '5125486', '91', '385'
82
+ assert pn.to_s == '+385915125486'
83
+ end
84
+
85
+ def test_to_s_without_country_code
86
+ Phonie::Phone.default_country_code = '385'
87
+ pn = Phonie::Phone.new '5125486', '91'
88
+ assert pn.format("0%a%n") == '0915125486'
89
+ end
90
+
91
+ def test_format_special_with_country_code
92
+ Phonie::Phone.default_country_code = nil
93
+ pn = Phonie::Phone.new '5125486', '91', '385'
94
+ assert pn.format("+ %c (%a) %n") == '+ 385 (91) 5125486'
95
+ end
96
+
97
+ def test_format_special_without_country_code
98
+ Phonie::Phone.default_country_code = '385'
99
+ pn = Phonie::Phone.new '5125486', '91'
100
+ assert_equal pn.format("%A/%f-%l"), '091/512-5486'
101
+ end
102
+
103
+ def test_format_with_symbol_specifier
104
+ Phonie::Phone.default_country_code = nil
105
+ pn = Phonie::Phone.new '5125486', '91', '385'
106
+ assert_equal pn.format(:europe), '+385 (0) 91 512 5486'
107
+ end
108
+
109
+ def test_valid
110
+ assert_equal Phonie::Phone.valid?('915125486', :country_code => '385'), true
111
+ assert_equal Phonie::Phone.valid?('385915125486'), true
112
+ end
113
+
114
+ def test_doesnt_validate
115
+ assert_equal Phonie::Phone.valid?('asdas'), false
116
+ assert_equal Phonie::Phone.valid?('38591512548678'), false
117
+ end
118
+
119
+ def test_comparison_true
120
+ pn1 = Phonie::Phone.new '5125486', '91', '385'
121
+ pn2 = Phonie::Phone.new '5125486', '91', '385'
122
+ assert pn1 == pn2
123
+ end
124
+
125
+ def test_comparison_false
126
+ pn1 = Phonie::Phone.new '5125486', '91', '385'
127
+ pn2 = Phonie::Phone.new '1234567', '91', '385'
128
+ assert pn1 != pn2
129
+ end
130
+
131
+ def test_parse_number_without_international_code
132
+ assert_equal (Phonie::Phone.parse "90123456"), nil
133
+ assert_equal (Phonie::Phone.parse "90123456", :country_code => '47').format(:default), "+4790123456"
134
+ assert_equal (Phonie::Phone.parse "90123456", :country_code => '47', :area_code => '').format(:default), "+4790123456"
135
+ end
136
+
137
+ end
@@ -0,0 +1,37 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+
5
+ require 'test/unit'
6
+ require 'phonie'
7
+
8
+ def parse_test(raw, country_code, area_code, number, country_name = nil, is_mobile = nil)
9
+ pn = Phonie::Phone.parse(raw)
10
+
11
+ assert_not_nil pn, %Q{parse should pass}
12
+ assert_equal pn.country_code, country_code
13
+ assert_equal pn.area_code, area_code
14
+ assert_equal pn.number, number
15
+ if country_name
16
+ assert_equal pn.country.name, country_name
17
+ end
18
+
19
+ unless is_mobile.nil?
20
+ assert_equal is_mobile, pn.is_mobile?
21
+ end
22
+ end
23
+
24
+
25
+ class Phonie::TestCase < Test::Unit::TestCase
26
+
27
+ def setup
28
+ Phonie::Phone.default_country_code = nil
29
+ Phonie::Phone.default_area_code = nil
30
+ end
31
+
32
+ def default_test
33
+ klass = self.class.to_s
34
+ ancestors = (self.class.ancestors - [self.class]).collect { |ancestor| ancestor.to_s }
35
+ super unless klass =~ /TestCase/ or ancestors.first =~ /TestCase/
36
+ end
37
+ end
@@ -0,0 +1,99 @@
1
+ 601-867-5000 ext 75292
2
+ (334)821-2223 Ext. 107
3
+ (863) 983-6171 ext: 0055
4
+ 601-987-3995 ext 10
5
+ (512) 473-3200Ext2396
6
+ (727) 558-5010 ext: 0112
7
+ (904) 360-5611 ext: 5611
8
+ (937) 255-7204Ext337
9
+ 228-374-5022 ext 5386
10
+ (205)987-3500 Ext. 205-32
11
+ (607) 273-8588x402
12
+ (205)328-3330 Ext. 802-59
13
+ (407) 251-2452 ext: 0146
14
+ 502-564-8110 xt 312
15
+ (828) 698-3923x13
16
+ (608)836-7433 Ext. 5147
17
+ (1-808)935-3222 Ext. 2#
18
+ (770)420-3179 Ext. x 179
19
+ (415) 558-8669x218
20
+ (205)879-6330 Ext. 731-50
21
+ (205)987-5995 Ext. 802-58
22
+ (609) 924-2200Ext10
23
+ (312) 440-4373x12
24
+ (978) 369-9602x457
25
+ 1-602-264-1774 # 135
26
+ 502-564-8890 x4340
27
+ 502-564-5550 x4569
28
+ (480)437-2600 Ext. VM123
29
+ (850) 245-4131 ext: 3506
30
+ 601-354-6161 ext 103
31
+ (414)778-5400 Ext. 127
32
+ (262)338-4475 Ext. #16
33
+ (602) 264-1774 extention # 135
34
+ (314) 361-3221x107
35
+ (800) 255-5747x46
36
+ 410-514-7336 TDDTTY + Voice
37
+ (617) 695-2300x393
38
+ 1-334-386-8800 Ext. 107
39
+ (928)634-9536 Ext. 143
40
+ 502-573-1555 x266
41
+ (978) 927-8000x246
42
+ (920) 887-4600Ext340
43
+ 502-564-1404 x4536
44
+ (850) 245-6774 ext: 4774
45
+ (718) 852-3000 Ext 269
46
+ 228-374-5022 ext 5022
47
+ 502-564-0105 x10260
48
+ (850) 000-0000 ext: 00000000
49
+ (979) 233-1616x306
50
+ (207) 333-3267x224
51
+ (561) 625-5122 ext: 687
52
+ (386) 961-7414 ext: 7414
53
+ (262)549-2249 Ext. 104
54
+ (205)322-7500 Ext. BECKYS
55
+ (303)858-8100 Ext. n.a.
56
+ (937) 255-7204x337
57
+ 601-944-4830 ext 104
58
+ 228-432-1056 ext 104
59
+ (209) 586-1495Ext104
60
+ 502-564-8139 : 4645
61
+ (602) 264-1774 ext is 135
62
+ (602) 264-1774Ext135
63
+ 502-564-7822 x4212
64
+ (609) 921-8808Ext3
65
+ (703) 739-7900x222
66
+ (609) 758-2241x144
67
+ (970)223-6500 Ext. X6510
68
+ (734) 485-2000Ext255
69
+ (904) 620-4424 ext: 8614424
70
+ (904) 827-2520 ext: 2520
71
+ (978) 774-1050x251
72
+ (541) 343-0123x2269
73
+ (608)277-2752 Ext. 2752
74
+ (203) 869-6786x335
75
+ 502-564-5981 x272
76
+ 606-878-5908 x6085
77
+ 502-564-2257 x3464
78
+ (205)871-1911 Ext. (205)
79
+ 502-564-7770 x3965
80
+ (608)374-2130 Ext. 28
81
+ (850) 410-9660 ext: 9240
82
+ 601-987-6837 ext 102
83
+ (414)529-1101 Ext. 153
84
+ (209) 586-1495x104
85
+ (520)408-1885 Ext. # 227
86
+ 601-605-5388 ext 100
87
+ (352) 375-8484 ext: 0174
88
+ 15025645981x225
89
+ (602) 264-1774 X 135
90
+ (205)803-1499 Ext. 414-03
91
+ (651) 459-4121x203
92
+ +61 3 9811 2400
93
+ (262)681-2020 Ext. 6682
94
+ 601-944-4845 ext 118
95
+ 384-1100 VOICE MAIL:3004
96
+ (205)251-1267 Ext. 802-09
97
+ (410) 280-2038Ext11
98
+ 502-564-3170 x190
99
+ 15025646734x4420