phone 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.rdoc +12 -2
- data/data/phone_countries.yml +2 -0
- data/lib/country.rb +12 -7
- data/lib/phone.rb +9 -3
- data/test/countries/es_test.rb +33 -0
- data/test/countries/uy_test.rb +22 -0
- data/test/phone_test.rb +19 -0
- metadata +7 -3
data/Readme.rdoc
CHANGED
@@ -85,6 +85,15 @@ You can add your own custom named formats like so:
|
|
85
85
|
Phoner::Phone.named_formats[:short] = '%A/%n1-%n2'
|
86
86
|
pn.format(:short) # => 091/512-5486
|
87
87
|
|
88
|
+
== Finding countries by their isocode
|
89
|
+
If you don't have the country code, but you know from other sources what country a phone is from, you can retrieve the country using the country isocode (such as 'de', 'es', 'us', ...).
|
90
|
+
|
91
|
+
if country = Phoner::Country.find_by_country_isocode(user_country_isocode)
|
92
|
+
phone_number = Phoner::Phone.parse(user_input, :country_code => country.country_code)
|
93
|
+
end
|
94
|
+
|
95
|
+
You have to remember to run Phoner::Country.load method before though.
|
96
|
+
|
88
97
|
= TODO
|
89
98
|
Parse testing for different countries.
|
90
99
|
|
@@ -93,6 +102,7 @@ Currently tested on:
|
|
93
102
|
[BA] Bosnia and Herzegovina
|
94
103
|
[BE] Belgium
|
95
104
|
[DE] Germany
|
105
|
+
[ES] Spain
|
96
106
|
[FR] France
|
97
107
|
[GB] United Kingdom
|
98
108
|
[HR] Croatia
|
@@ -106,11 +116,11 @@ Currently tested on:
|
|
106
116
|
[ZA] South Africa
|
107
117
|
|
108
118
|
= Known issues
|
109
|
-
There's an issue with Germany and area codes.
|
119
|
+
There's an issue with Germany and Spanish area codes.
|
110
120
|
|
111
121
|
= Author
|
112
122
|
Copyright © 2010 Tomislav Car, {Infinum}[http://www.infinumdigital.com]
|
113
123
|
|
114
124
|
= Contributors
|
115
|
-
Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall
|
125
|
+
Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall, Jakob Hilden, Tieg Zaharia
|
116
126
|
|
data/data/phone_countries.yml
CHANGED
@@ -357,6 +357,7 @@
|
|
357
357
|
:char_3_code: UY
|
358
358
|
:name: Uruguay
|
359
359
|
:international_dialing_prefix: "0"
|
360
|
+
:area_code: "2|42|43[34567]|4364|44[3457]|4452|4542|4544|456[7]?|4586|46[234]|4675|47[237]|4779|9[13456789]"
|
360
361
|
"992":
|
361
362
|
:country_code: "992"
|
362
363
|
:national_dialing_prefix: "8"
|
@@ -571,6 +572,7 @@
|
|
571
572
|
:char_3_code: ES
|
572
573
|
:name: Spain
|
573
574
|
:international_dialing_prefix: "0"
|
575
|
+
:area_code: "6[0-9][0-9]|7[1-9][0-9]|9[0-9][0-9]"
|
574
576
|
"232":
|
575
577
|
:country_code: "232"
|
576
578
|
:national_dialing_prefix: "0"
|
data/lib/country.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Phoner
|
2
|
-
class Country < Struct.new(:name, :country_code, :char_2_code, :area_code)
|
2
|
+
class Country < Struct.new(:name, :country_code, :char_2_code, :char_3_code, :area_code)
|
3
3
|
cattr_accessor :all
|
4
4
|
|
5
5
|
def self.load
|
@@ -9,7 +9,7 @@ module Phoner
|
|
9
9
|
|
10
10
|
@@all = {}
|
11
11
|
YAML.load(File.read(data_file)).each_pair do |key, c|
|
12
|
-
@@all[key] = Country.new(c[:name], c[:country_code], c[:char_2_code], c[:area_code])
|
12
|
+
@@all[key] = Country.new(c[:name], c[:country_code], c[:char_2_code], c[:char_3_code], c[:area_code])
|
13
13
|
end
|
14
14
|
@@all
|
15
15
|
end
|
@@ -19,12 +19,17 @@ module Phoner
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.find_by_country_code(code)
|
22
|
-
@@all[code]
|
22
|
+
@@all[code]
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
|
+
def self.find_by_country_isocode(isocode)
|
26
|
+
if country = @@all.detect{|c|c[1].char_3_code.downcase == isocode}
|
27
|
+
country[1]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
25
31
|
def country_code_regexp
|
26
|
-
Regexp.new("^[+]#{country_code}")
|
32
|
+
Regexp.new("^[+]#{country_code}")
|
27
33
|
end
|
28
34
|
end
|
29
|
-
|
30
|
-
end
|
35
|
+
end
|
data/lib/phone.rb
CHANGED
@@ -15,7 +15,7 @@ require File.join(File.dirname(__FILE__), 'country')
|
|
15
15
|
module Phoner
|
16
16
|
class Phone
|
17
17
|
NUMBER = '([0-9]{1,8})$'
|
18
|
-
DEFAULT_AREA_CODE = '[
|
18
|
+
DEFAULT_AREA_CODE = '[0-9][0-9][0-9]' # any 3 digits
|
19
19
|
|
20
20
|
attr_accessor :country_code, :area_code, :number, :extension
|
21
21
|
|
@@ -157,7 +157,7 @@ module Phoner
|
|
157
157
|
|
158
158
|
# fix string so it's easier to parse, remove extra characters etc.
|
159
159
|
def self.normalize(string_with_number)
|
160
|
-
string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+')
|
160
|
+
string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+').gsub(/^\+00/, '+').gsub(/^\+0/, '+')
|
161
161
|
end
|
162
162
|
|
163
163
|
# pull off anything that look like an extension
|
@@ -235,6 +235,12 @@ module Phoner
|
|
235
235
|
def has_default_area_code?
|
236
236
|
area_code == self.class.default_area_code
|
237
237
|
end
|
238
|
+
|
239
|
+
# comparison of 2 phone objects
|
240
|
+
def ==(other)
|
241
|
+
methods = [:country_code, :area_code, :number, :extension]
|
242
|
+
methods.all? { |method| other.respond_to?(method) && send(method) == other.send(method) }
|
243
|
+
end
|
238
244
|
|
239
245
|
private
|
240
246
|
|
@@ -249,4 +255,4 @@ module Phoner
|
|
249
255
|
return result
|
250
256
|
end
|
251
257
|
end
|
252
|
-
end
|
258
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
## Spain
|
4
|
+
# http://en.wikipedia.org/wiki/Telephone_numbers_in_Spain
|
5
|
+
# http://gospain.about.com/od/practicaltraveltips/f/area_codes.htm
|
6
|
+
|
7
|
+
# A problem is that landline area codes can be 2 (92) as well as 3 digits (923)
|
8
|
+
# right now the area_code_regexp is only finding 3 digit area_codes
|
9
|
+
|
10
|
+
class ESTest < Test::Unit::TestCase
|
11
|
+
|
12
|
+
def test_validates
|
13
|
+
Phoner::Phone.default_country_code = nil
|
14
|
+
assert_equal Phoner::Phone.valid?("+34-695-097-612"), true
|
15
|
+
assert_equal Phoner::Phone.valid?("0034 91-3597426"), true
|
16
|
+
assert_equal Phoner::Phone.valid?("+0034 91-3597426"), true
|
17
|
+
assert_equal Phoner::Phone.valid?("+34 92-6563629"), true
|
18
|
+
assert_equal Phoner::Phone.valid?("(+34) 606 275 213"), true
|
19
|
+
assert_equal Phoner::Phone.valid?("+034 937 299 016"), true
|
20
|
+
assert_equal Phoner::Phone.valid?("+34 93 487 32 93"), true
|
21
|
+
|
22
|
+
Phoner::Phone.default_country_code = '34'
|
23
|
+
assert_equal Phoner::Phone.valid?(" 607 65 05 01 "), true
|
24
|
+
assert_equal Phoner::Phone.valid?(" 971 15 66 33"), true
|
25
|
+
assert_equal Phoner::Phone.valid?(" 93-4559934"), true
|
26
|
+
assert_equal Phoner::Phone.valid?("628 274 908"), true
|
27
|
+
assert_equal Phoner::Phone.valid?("744 486 62 78"), true
|
28
|
+
assert_equal Phoner::Phone.valid?("916754559"), true
|
29
|
+
assert_equal Phoner::Phone.valid?("93.4327119"), true
|
30
|
+
assert_equal Phoner::Phone.valid?("986300257"), true
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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 < Test::Unit::TestCase
|
6
|
+
|
7
|
+
# 02 Montevideo
|
8
|
+
def test_montevideo
|
9
|
+
parse_test('+598 2 1234567', '598', '2', '1234567')
|
10
|
+
end
|
11
|
+
|
12
|
+
# 042 Maldonado
|
13
|
+
def test_maldonado
|
14
|
+
parse_test('+598 42 123456', '598', '42', '123456')
|
15
|
+
end
|
16
|
+
|
17
|
+
# 09 Mobile phones
|
18
|
+
def test_mobile_phones
|
19
|
+
parse_test('+598 99 570110', '598', '99', '570110')
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/test/phone_test.rb
CHANGED
@@ -103,4 +103,23 @@ class PhoneTest < Test::Unit::TestCase
|
|
103
103
|
assert_equal Phoner::Phone.valid?('385915125486'), false
|
104
104
|
end
|
105
105
|
|
106
|
+
def test_comparison_true
|
107
|
+
pn1 = Phoner::Phone.new '5125486', '91', '385'
|
108
|
+
pn2 = Phoner::Phone.new '5125486', '91', '385'
|
109
|
+
assert pn1 == pn2
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_comparison_false
|
113
|
+
pn1 = Phoner::Phone.new '5125486', '91', '385'
|
114
|
+
pn2 = Phoner::Phone.new '1234567', '91', '385'
|
115
|
+
assert pn1 != pn2
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_find_by_country_isocode
|
119
|
+
Phoner::Country.load
|
120
|
+
assert_equal Phoner::Country.find_by_country_isocode('de').country_code, "49"
|
121
|
+
assert_equal Phoner::Country.find_by_country_isocode('xx'), nil
|
122
|
+
assert_equal Phoner::Country.find_by_country_isocode('bla'), nil
|
123
|
+
end
|
124
|
+
|
106
125
|
end
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
version: "1.
|
7
|
+
- 1
|
8
|
+
version: "1.1"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Tomislav Car
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-24 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- test/countries/ba_test.rb
|
46
46
|
- test/countries/be_test.rb
|
47
47
|
- test/countries/de_test.rb
|
48
|
+
- test/countries/es_test.rb
|
48
49
|
- test/countries/fr_test.rb
|
49
50
|
- test/countries/gb_test.rb
|
50
51
|
- test/countries/hr_test.rb
|
@@ -56,6 +57,7 @@ files:
|
|
56
57
|
- test/countries/si_test.rb
|
57
58
|
- test/countries/ua_test.rb
|
58
59
|
- test/countries/us_test.rb
|
60
|
+
- test/countries/uy_test.rb
|
59
61
|
- test/countries/za_test.rb
|
60
62
|
has_rdoc: true
|
61
63
|
homepage: http://github.com/carr/phone
|
@@ -100,6 +102,7 @@ test_files:
|
|
100
102
|
- test/countries/ba_test.rb
|
101
103
|
- test/countries/be_test.rb
|
102
104
|
- test/countries/de_test.rb
|
105
|
+
- test/countries/es_test.rb
|
103
106
|
- test/countries/fr_test.rb
|
104
107
|
- test/countries/gb_test.rb
|
105
108
|
- test/countries/hr_test.rb
|
@@ -111,4 +114,5 @@ test_files:
|
|
111
114
|
- test/countries/si_test.rb
|
112
115
|
- test/countries/ua_test.rb
|
113
116
|
- test/countries/us_test.rb
|
117
|
+
- test/countries/uy_test.rb
|
114
118
|
- test/countries/za_test.rb
|