phone 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Readme.rdoc +12 -2
- data/data/phone_countries.yml +1 -0
- data/lib/country.rb +11 -5
- data/lib/phone.rb +3 -3
- data/test/phone_test.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f34c77402ea12b6fafbda205f46bb897b0996c66
|
4
|
+
data.tar.gz: f29f3fd69c0f6415ba76d1d16da01279cdc76b8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c94ec8394ccd7a4485d56caa2e701d147f241745909622b59a08c05e4d91733f01b21088e7c81d53cdb1a55efb5d73926c46f3abe9bf3496ae8d4a8bc9fa1087
|
7
|
+
data.tar.gz: 23f8e3d274b53614a54964c87bd7cc09a7ef103d47eccf1d53db75a444a986e6e40d8cf80dc657ed0febb7a0a48ec2a631fab8197b0e7f7a5c9bf6016685235c
|
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
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,18 @@ module Phoner
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.find_by_country_code(code)
|
22
|
-
@@all[code]
|
22
|
+
@@all[code]
|
23
|
+
end
|
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
|
23
29
|
end
|
24
30
|
|
25
31
|
def country_code_regexp
|
26
|
-
Regexp.new("^[+]#{country_code}")
|
32
|
+
Regexp.new("^[+]#{country_code}")
|
27
33
|
end
|
28
34
|
end
|
29
35
|
|
30
|
-
end
|
36
|
+
end
|
data/lib/phone.rb
CHANGED
@@ -16,7 +16,7 @@ require File.join(File.dirname(__FILE__), 'errors')
|
|
16
16
|
module Phoner
|
17
17
|
class Phone
|
18
18
|
NUMBER = '([0-9]{1,8})$'
|
19
|
-
DEFAULT_AREA_CODE = '[
|
19
|
+
DEFAULT_AREA_CODE = '[0-9][0-9][0-9]' # any 3 digits
|
20
20
|
|
21
21
|
attr_accessor :country_code, :area_code, :number, :extension
|
22
22
|
|
@@ -159,7 +159,7 @@ module Phoner
|
|
159
159
|
|
160
160
|
# fix string so it's easier to parse, remove extra characters etc.
|
161
161
|
def self.normalize(string_with_number)
|
162
|
-
string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+')
|
162
|
+
string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+').gsub(/^\+00/, '+').gsub(/^\+0/, '+')
|
163
163
|
end
|
164
164
|
|
165
165
|
# pull off anything that look like an extension
|
@@ -257,4 +257,4 @@ module Phoner
|
|
257
257
|
return result
|
258
258
|
end
|
259
259
|
end
|
260
|
-
end
|
260
|
+
end
|
data/test/phone_test.rb
CHANGED
@@ -114,4 +114,12 @@ class PhoneTest < Test::Unit::TestCase
|
|
114
114
|
pn2 = Phoner::Phone.new '1234567', '91', '385'
|
115
115
|
assert pn1 != pn2
|
116
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
|
+
|
117
125
|
end
|