phone 1.1 → 1.2.1
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 +7 -0
- data/Readme.rdoc +2 -12
- data/data/phone_countries.yml +0 -1
- data/lib/country.rb +7 -12
- data/lib/errors.rb +6 -0
- data/lib/phone.rb +11 -9
- data/test/phone_test.rb +9 -17
- metadata +26 -46
- data/test/countries/es_test.rb +0 -33
- data/test/countries/uy_test.rb +0 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9927575529414f0e507e81c228a7c6106670a6ab
|
4
|
+
data.tar.gz: f8ac795cbf0043375ecdb6d100210cdfe6dc2198
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d21a6eb4e3e3e10e102b20dc63a9e477b85ba5a72761d996d36b5a61ac7880050d38b16458cf7b5897fe60604ae6f8abefeaa050495216eb1889f3a65b64f57c
|
7
|
+
data.tar.gz: a76493f7a957f556b3074cc3554da658793786de605e33b0b3c021abb0ba1c025e2b8f959e1f078760fd1c05763dc68f308a14a82adb613d10d54cad13ebd983
|
data/Readme.rdoc
CHANGED
@@ -85,15 +85,6 @@ 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
|
-
|
97
88
|
= TODO
|
98
89
|
Parse testing for different countries.
|
99
90
|
|
@@ -102,7 +93,6 @@ Currently tested on:
|
|
102
93
|
[BA] Bosnia and Herzegovina
|
103
94
|
[BE] Belgium
|
104
95
|
[DE] Germany
|
105
|
-
[ES] Spain
|
106
96
|
[FR] France
|
107
97
|
[GB] United Kingdom
|
108
98
|
[HR] Croatia
|
@@ -116,11 +106,11 @@ Currently tested on:
|
|
116
106
|
[ZA] South Africa
|
117
107
|
|
118
108
|
= Known issues
|
119
|
-
There's an issue with Germany and
|
109
|
+
There's an issue with Germany and area codes.
|
120
110
|
|
121
111
|
= Author
|
122
112
|
Copyright © 2010 Tomislav Car, {Infinum}[http://www.infinumdigital.com]
|
123
113
|
|
124
114
|
= Contributors
|
125
|
-
Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall
|
115
|
+
Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall
|
126
116
|
|
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, :
|
2
|
+
class Country < Struct.new(:name, :country_code, :char_2_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[:
|
12
|
+
@@all[key] = Country.new(c[:name], c[:country_code], c[:char_2_code], c[:area_code])
|
13
13
|
end
|
14
14
|
@@all
|
15
15
|
end
|
@@ -19,17 +19,12 @@ 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
|
-
|
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
|
-
|
24
|
+
|
31
25
|
def country_code_regexp
|
32
|
-
Regexp.new("^[+]#{country_code}")
|
26
|
+
Regexp.new("^[+]#{country_code}")
|
33
27
|
end
|
34
28
|
end
|
35
|
-
|
29
|
+
|
30
|
+
end
|
data/lib/errors.rb
ADDED
data/lib/phone.rb
CHANGED
@@ -11,11 +11,12 @@
|
|
11
11
|
#
|
12
12
|
require File.join(File.dirname(__FILE__), 'support') unless defined? ActiveSupport
|
13
13
|
require File.join(File.dirname(__FILE__), 'country')
|
14
|
+
require File.join(File.dirname(__FILE__), 'errors')
|
14
15
|
|
15
16
|
module Phoner
|
16
17
|
class Phone
|
17
18
|
NUMBER = '([0-9]{1,8})$'
|
18
|
-
DEFAULT_AREA_CODE = '[
|
19
|
+
DEFAULT_AREA_CODE = '[2-9][0-8][0-9]' # USA
|
19
20
|
|
20
21
|
attr_accessor :country_code, :area_code, :number, :extension
|
21
22
|
|
@@ -48,9 +49,9 @@ module Phoner
|
|
48
49
|
self.country_code = hash_or_args[ keys[:country_code] ] || self.default_country_code
|
49
50
|
self.extension = hash_or_args[ keys[:extension] ]
|
50
51
|
|
51
|
-
raise "Must enter number" if self.number.blank?
|
52
|
-
raise "Must enter area code or set default area code" if self.area_code.blank?
|
53
|
-
raise "Must enter country code or set default country code" if self.country_code.blank?
|
52
|
+
raise NumberError, "Must enter number" if self.number.blank?
|
53
|
+
raise AreaCodeError, "Must enter area code or set default area code" if self.area_code.blank?
|
54
|
+
raise CountryCodeError, "Must enter country code or set default country code" if self.country_code.blank?
|
54
55
|
end
|
55
56
|
|
56
57
|
# create a new phone number by parsing a string
|
@@ -78,7 +79,8 @@ module Phoner
|
|
78
79
|
def self.valid?(string)
|
79
80
|
begin
|
80
81
|
parse(string).present?
|
81
|
-
|
82
|
+
# if we encountered exceptions (missing country code, missing area code etc)
|
83
|
+
rescue PhoneError
|
82
84
|
return false
|
83
85
|
end
|
84
86
|
end
|
@@ -98,9 +100,9 @@ module Phoner
|
|
98
100
|
|
99
101
|
if country.nil?
|
100
102
|
if options[:country_code].nil?
|
101
|
-
raise "Must enter country code or set default country code"
|
103
|
+
raise CountryCodeError, "Must enter country code or set default country code"
|
102
104
|
else
|
103
|
-
raise "Could not find country with country code #{options[:country_code]}"
|
105
|
+
raise CountryCodeError, "Could not find country with country code #{options[:country_code]}"
|
104
106
|
end
|
105
107
|
end
|
106
108
|
|
@@ -157,7 +159,7 @@ module Phoner
|
|
157
159
|
|
158
160
|
# fix string so it's easier to parse, remove extra characters etc.
|
159
161
|
def self.normalize(string_with_number)
|
160
|
-
string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+')
|
162
|
+
string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+')
|
161
163
|
end
|
162
164
|
|
163
165
|
# pull off anything that look like an extension
|
@@ -255,4 +257,4 @@ module Phoner
|
|
255
257
|
return result
|
256
258
|
end
|
257
259
|
end
|
258
|
-
end
|
260
|
+
end
|
data/test/phone_test.rb
CHANGED
@@ -4,17 +4,17 @@ class PhoneTest < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
def test_number_without_country_code_initialize
|
6
6
|
Phoner::Phone.default_country_code = nil
|
7
|
-
|
8
|
-
assert_raise
|
7
|
+
|
8
|
+
assert_raise Phoner::CountryCodeError do
|
9
9
|
pn = Phoner::Phone.new '5125486', '91'
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_number_without_country_and_area_code_initialize
|
14
14
|
Phoner::Phone.default_country_code = nil
|
15
|
-
Phoner::Phone.default_area_code = nil
|
16
|
-
|
17
|
-
assert_raise
|
15
|
+
Phoner::Phone.default_area_code = nil
|
16
|
+
|
17
|
+
assert_raise Phoner::AreaCodeError do
|
18
18
|
pn = Phoner::Phone.new '451588'
|
19
19
|
end
|
20
20
|
end
|
@@ -54,16 +54,16 @@ class PhoneTest < Test::Unit::TestCase
|
|
54
54
|
|
55
55
|
def test_parse_short_without_special_characters_without_country
|
56
56
|
Phoner::Phone.default_country_code = nil
|
57
|
-
|
58
|
-
assert_raise
|
57
|
+
|
58
|
+
assert_raise Phoner::CountryCodeError do
|
59
59
|
pn = Phoner::Phone.parse "0915125486"
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
def test_parse_short_with_special_characters_without_country
|
64
64
|
Phoner::Phone.default_country_code = nil
|
65
|
-
|
66
|
-
assert_raise
|
65
|
+
|
66
|
+
assert_raise Phoner::CountryCodeError do
|
67
67
|
pn = Phoner::Phone.parse "091/512-5486"
|
68
68
|
end
|
69
69
|
end
|
@@ -114,12 +114,4 @@ 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
|
-
|
125
117
|
end
|
metadata
CHANGED
@@ -1,42 +1,34 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: phone
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 1
|
7
|
-
- 1
|
8
|
-
version: "1.1"
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1
|
9
5
|
platform: ruby
|
10
|
-
authors:
|
6
|
+
authors:
|
11
7
|
- Tomislav Car
|
12
8
|
- Todd Eichel
|
13
9
|
- Don Morrison
|
14
10
|
autorequire:
|
15
11
|
bindir: bin
|
16
12
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-06-24 00:00:00 +02:00
|
19
|
-
default_executable:
|
13
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
20
14
|
dependencies: []
|
21
|
-
|
22
15
|
description: Phone number parsing, validation and formatting.
|
23
|
-
email:
|
16
|
+
email:
|
24
17
|
- tomislav@infinum.hr
|
25
18
|
- todd@toddeichel.com
|
26
19
|
- elskwid@gmail.com
|
27
20
|
executables: []
|
28
|
-
|
29
21
|
extensions: []
|
30
|
-
|
31
|
-
extra_rdoc_files:
|
22
|
+
extra_rdoc_files:
|
32
23
|
- Readme.rdoc
|
33
24
|
- LICENSE
|
34
|
-
files:
|
25
|
+
files:
|
35
26
|
- Readme.rdoc
|
36
27
|
- LICENSE
|
37
28
|
- data/phone_countries.yml
|
38
29
|
- lib/phone.rb
|
39
30
|
- lib/country.rb
|
31
|
+
- lib/errors.rb
|
40
32
|
- lib/support.rb
|
41
33
|
- test/extension_test.rb
|
42
34
|
- test/phone_test.rb
|
@@ -45,7 +37,6 @@ files:
|
|
45
37
|
- test/countries/ba_test.rb
|
46
38
|
- test/countries/be_test.rb
|
47
39
|
- test/countries/de_test.rb
|
48
|
-
- test/countries/es_test.rb
|
49
40
|
- test/countries/fr_test.rb
|
50
41
|
- test/countries/gb_test.rb
|
51
42
|
- test/countries/hr_test.rb
|
@@ -57,44 +48,35 @@ files:
|
|
57
48
|
- test/countries/si_test.rb
|
58
49
|
- test/countries/ua_test.rb
|
59
50
|
- test/countries/us_test.rb
|
60
|
-
- test/countries/uy_test.rb
|
61
51
|
- test/countries/za_test.rb
|
62
|
-
has_rdoc: true
|
63
52
|
homepage: http://github.com/carr/phone
|
64
53
|
licenses: []
|
65
|
-
|
54
|
+
metadata: {}
|
66
55
|
post_install_message:
|
67
|
-
rdoc_options:
|
68
|
-
- --main
|
56
|
+
rdoc_options:
|
57
|
+
- "--main"
|
69
58
|
- Readme.rdoc
|
70
|
-
- --inline-source
|
71
|
-
- --charset=UTF-8
|
72
|
-
require_paths:
|
59
|
+
- "--inline-source"
|
60
|
+
- "--charset=UTF-8"
|
61
|
+
require_paths:
|
73
62
|
- lib
|
74
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
-
|
76
|
-
requirements:
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
77
65
|
- - ">="
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
84
|
-
requirements:
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
85
70
|
- - ">="
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
|
88
|
-
- 0
|
89
|
-
version: "0"
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
90
73
|
requirements: []
|
91
|
-
|
92
74
|
rubyforge_project:
|
93
|
-
rubygems_version:
|
75
|
+
rubygems_version: 2.0.3
|
94
76
|
signing_key:
|
95
|
-
specification_version:
|
77
|
+
specification_version: 4
|
96
78
|
summary: Phone number parsing, validation and formatting
|
97
|
-
test_files:
|
79
|
+
test_files:
|
98
80
|
- test/extension_test.rb
|
99
81
|
- test/phone_test.rb
|
100
82
|
- test/test_helper.rb
|
@@ -102,7 +84,6 @@ test_files:
|
|
102
84
|
- test/countries/ba_test.rb
|
103
85
|
- test/countries/be_test.rb
|
104
86
|
- test/countries/de_test.rb
|
105
|
-
- test/countries/es_test.rb
|
106
87
|
- test/countries/fr_test.rb
|
107
88
|
- test/countries/gb_test.rb
|
108
89
|
- test/countries/hr_test.rb
|
@@ -114,5 +95,4 @@ test_files:
|
|
114
95
|
- test/countries/si_test.rb
|
115
96
|
- test/countries/ua_test.rb
|
116
97
|
- test/countries/us_test.rb
|
117
|
-
- test/countries/uy_test.rb
|
118
98
|
- test/countries/za_test.rb
|
data/test/countries/es_test.rb
DELETED
@@ -1,33 +0,0 @@
|
|
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
|
data/test/countries/uy_test.rb
DELETED
@@ -1,22 +0,0 @@
|
|
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
|