phone 0.9.9.3 → 1.0

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.
@@ -9,34 +9,40 @@ You can install the phone library as a gem
9
9
  Or as a Rails plugin
10
10
  script/plugin install git://github.com/carr/phone.git
11
11
 
12
+ == Updates in v 1.0
13
+
14
+ The biggest updating is the namespacing problem fixed that a lot of people were having. You now use phone by refering to
15
+
16
+ Phoner::Phone
17
+
12
18
  == Initializing
13
19
  You can initialize a new phone object with the number, area code, country code and extension number
14
20
 
15
- Phone.new('5125486', '91', '385')
21
+ Phoner::Phone.new('5125486', '91', '385')
16
22
  or
17
- Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143')
23
+ Phoner::Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143')
18
24
 
19
25
  == Parsing
20
- You can create a new phone object by parsing from a string. Phone does it's best to detect the country and area codes:
21
- Phone.parse '+385915125486'
22
- Phone.parse '00385915125486'
26
+ You can create a new phone object by parsing from a string. Phoner::Phone does it's best to detect the country and area codes:
27
+ Phoner::Phone.parse '+385915125486'
28
+ Phoner::Phone.parse '00385915125486'
23
29
 
24
30
  If the country or area code isn't given in the string, you must set it, otherwise it doesn't work:
25
- Phone.parse '091/512-5486', :country_code => '385'
26
- Phone.parse '(091) 512 5486', :country_code => '385'
31
+ Phoner::Phone.parse '091/512-5486', :country_code => '385'
32
+ Phoner::Phone.parse '(091) 512 5486', :country_code => '385'
27
33
 
28
34
  If you feel that it's tedious, set the default country code once (in your config/environment.rb):
29
- Phone.default_country_code = '385'
30
- Phone.parse '091/512-5486'
31
- Phone.parse '(091) 512 5486'
35
+ Phoner::Phone.default_country_code = '385'
36
+ Phoner::Phone.parse '091/512-5486'
37
+ Phoner::Phone.parse '(091) 512 5486'
32
38
 
33
39
  Same goes for the area code:
34
- Phone.parse '451-588', :country_code => '385', :area_code => '47'
40
+ Phoner::Phone.parse '451-588', :country_code => '385', :area_code => '47'
35
41
  or
36
- Phone.default_country_code = '385'
37
- Phone.default_area_code = '47'
42
+ Phoner::Phone.default_country_code = '385'
43
+ Phoner::Phone.default_area_code = '47'
38
44
 
39
- Phone.parse '451-588'
45
+ Phoner::Phone.parse '451-588'
40
46
 
41
47
  === Automatic country and area code detection
42
48
  Like it's stated above, Phone does it's best to automatically detect the country and area code while parsing. Do do this,
@@ -45,12 +51,12 @@ phone uses data stored in <tt>data/countries.yml</tt>.
45
51
  Each country code can have a regular expression named <tt>area_code</tt> that describes how the area code for that
46
52
  particular country looks like.
47
53
 
48
- If an <tt>area_code</tt> regular expression isn't specified, the default, <tt>Phone::DEFAULT_AREA_CODE</tt> (correct for
54
+ If an <tt>area_code</tt> regular expression isn't specified, the default, <tt>Phoner::Phone::DEFAULT_AREA_CODE</tt> (correct for
49
55
  the US) is used.
50
56
 
51
57
  == Validating
52
58
  Validating is very relaxed, basically it strips out everything that's not a number or '+' character:
53
- Phone.valid? 'blabla 091/512-5486 blabla'
59
+ Phoner::Phone.valid? 'blabla 091/512-5486 blabla'
54
60
 
55
61
  == Formatting
56
62
  Formating is done via the <tt>format</tt> method. The method accepts a <tt>Symbol</tt> or a <tt>String</tt>.
@@ -61,22 +67,22 @@ When given a string, it interpolates the string with the following fields:
61
67
  * %a - area_code (91)
62
68
  * %A - area_code with leading zero (091)
63
69
  * %n - number (5125486)
64
- * %f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512)
70
+ * %f - first @@n1_length characters of number (configured through Phoner::Phone.n1_length), default is 3 (512)
65
71
  * %l - last characters of number (5486)
66
72
  * %x - the extension number
67
73
 
68
- pn = Phone.parse('+385915125486')
74
+ pn = Phoner::Phone.parse('+385915125486')
69
75
  pn.to_s # => "+385915125486"
70
76
  pn.format("%A/%f-%l") # => "091/512-5486"
71
77
  pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486"
72
78
 
73
- When given a symbol it is used as a lookup for the format in the <tt>Phone.named_formats</tt> hash.
79
+ When given a symbol it is used as a lookup for the format in the <tt>Phoner::Phone.named_formats</tt> hash.
74
80
  pn.format(:europe) # => "+385 (0) 91 512 5486"
75
81
  pn.format(:us) # => "(234) 123 4567"
76
82
  pn.format(:default_with_extension) # => "+3851234567x143"
77
83
 
78
84
  You can add your own custom named formats like so:
79
- Phone.named_formats[:short] = '%A/%n1-%n2'
85
+ Phoner::Phone.named_formats[:short] = '%A/%n1-%n2'
80
86
  pn.format(:short) # => 091/512-5486
81
87
 
82
88
  = TODO
@@ -99,8 +105,11 @@ Currently tested on:
99
105
  [US] United States
100
106
  [ZA] South Africa
101
107
 
108
+ = Known issues
109
+ There's an issue with Germany and area codes.
110
+
102
111
  = Author
103
- Copyright © 2010 Tomislav Car, Infinum
112
+ Copyright © 2010 Tomislav Car, {Infinum}[http://www.infinumdigital.com]
104
113
 
105
114
  = Contributors
106
115
  Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall
@@ -528,7 +528,7 @@
528
528
  :char_3_code: FR
529
529
  :name: France
530
530
  :international_dialing_prefix: "0"
531
- :area_code: "[1-6,8-9]"
531
+ :area_code: "[1-7,8-9]"
532
532
  "995":
533
533
  :country_code: "995"
534
534
  :national_dialing_prefix: 8*
@@ -957,6 +957,7 @@
957
957
  :char_3_code: PT
958
958
  :name: Portugal
959
959
  :international_dialing_prefix: "0"
960
+ :area_code: "21|22|2[3-9][1-9]|70[78]|80[089]|9[136]|92[1-9]"
960
961
  "973":
961
962
  :country_code: "973"
962
963
  :national_dialing_prefix: None
@@ -0,0 +1,30 @@
1
+ module Phoner
2
+ class Country < Struct.new(:name, :country_code, :char_2_code, :area_code)
3
+ cattr_accessor :all
4
+
5
+ def self.load
6
+ return @@all if @@all.present?
7
+
8
+ data_file = File.join(File.dirname(__FILE__), '..', 'data', 'phone_countries.yml')
9
+
10
+ @@all = {}
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])
13
+ end
14
+ @@all
15
+ end
16
+
17
+ def to_s
18
+ name
19
+ end
20
+
21
+ def self.find_by_country_code(code)
22
+ @@all[code]
23
+ end
24
+
25
+ def country_code_regexp
26
+ Regexp.new("^[+]#{country_code}")
27
+ end
28
+ end
29
+
30
+ end
@@ -10,240 +10,243 @@
10
10
  # Phone.default_area_code
11
11
  #
12
12
  require File.join(File.dirname(__FILE__), 'support') unless defined? ActiveSupport
13
- require File.join(File.dirname(__FILE__), 'phone_country')
14
- class Phone
15
- NUMBER = '([0-9]{1,8})$'
16
- DEFAULT_AREA_CODE = '[2-9][0-8][0-9]' # USA
17
-
18
- attr_accessor :country_code, :area_code, :number, :extension
19
-
20
- cattr_accessor :default_country_code
21
- cattr_accessor :default_area_code
22
- cattr_accessor :named_formats
23
-
24
- # length of first number part (using multi number format)
25
- cattr_accessor :n1_length
26
- # default length of first number part
27
- @@n1_length = 3
28
-
29
- @@named_formats = {
30
- :default => "+%c%a%n",
31
- :default_with_extension => "+%c%a%nx%x",
32
- :europe => '+%c (0) %a %f %l',
33
- :us => "(%a) %f-%l"
34
- }
35
-
36
- def initialize(*hash_or_args)
37
- if hash_or_args.first.is_a?(Hash)
38
- hash_or_args = hash_or_args.first
39
- keys = {:number => :number, :area_code => :area_code, :country_code => :country_code, :extension => :extension}
40
- else
41
- keys = {:number => 0, :area_code => 1, :country_code => 2, :extension => 3}
42
- end
43
-
44
- self.number = hash_or_args[ keys[:number] ]
45
- self.area_code = hash_or_args[ keys[:area_code] ] || self.default_area_code
46
- self.country_code = hash_or_args[ keys[:country_code] ] || self.default_country_code
47
- self.extension = hash_or_args[ keys[:extension] ]
48
-
49
- raise "Must enter number" if self.number.blank?
50
- raise "Must enter area code or set default area code" if self.area_code.blank?
51
- raise "Must enter country code or set default country code" if self.country_code.blank?
52
- end
53
-
54
- # create a new phone number by parsing a string
55
- # the format of the string is detect automatically (from FORMATS)
56
- def self.parse(string, options={})
57
- if string.present?
58
- PhoneCountry.load
59
- extension = extract_extension(string)
60
- string = normalize(string)
61
-
62
- options[:country_code] ||= self.default_country_code
63
- options[:area_code] ||= self.default_area_code
64
-
65
- parts = split_to_parts(string, options)
66
-
67
- pn = Phone.new(parts) if parts
68
- if pn.present? and extension.present?
69
- pn.extension = extension
13
+ require File.join(File.dirname(__FILE__), 'country')
14
+
15
+ module Phoner
16
+ class Phone
17
+ NUMBER = '([0-9]{1,8})$'
18
+ DEFAULT_AREA_CODE = '[2-9][0-8][0-9]' # USA
19
+
20
+ attr_accessor :country_code, :area_code, :number, :extension
21
+
22
+ cattr_accessor :default_country_code
23
+ cattr_accessor :default_area_code
24
+ cattr_accessor :named_formats
25
+
26
+ # length of first number part (using multi number format)
27
+ cattr_accessor :n1_length
28
+ # default length of first number part
29
+ @@n1_length = 3
30
+
31
+ @@named_formats = {
32
+ :default => "+%c%a%n",
33
+ :default_with_extension => "+%c%a%nx%x",
34
+ :europe => '+%c (0) %a %f %l',
35
+ :us => "(%a) %f-%l"
36
+ }
37
+
38
+ def initialize(*hash_or_args)
39
+ if hash_or_args.first.is_a?(Hash)
40
+ hash_or_args = hash_or_args.first
41
+ keys = {:number => :number, :area_code => :area_code, :country_code => :country_code, :extension => :extension}
42
+ else
43
+ keys = {:number => 0, :area_code => 1, :country_code => 2, :extension => 3}
70
44
  end
71
- return pn
72
- end
73
- end
74
-
75
- # is this string a valid phone number?
76
- def self.valid?(string)
77
- begin
78
- parse(string).present?
79
- rescue RuntimeError # if we encountered exceptions (missing country code, missing area code etc)
80
- return false
81
- end
82
- end
83
-
84
- # split string into hash with keys :country_code, :area_code and :number
85
- def self.split_to_parts(string, options = {})
86
- country = detect_country(string)
87
-
88
- if country
89
- options[:country_code] = country.country_code
90
- string = string.gsub(country.country_code_regexp, '0')
91
- else
92
- if options[:country_code]
93
- country = PhoneCountry.find_by_country_code options[:country_code]
45
+
46
+ self.number = hash_or_args[ keys[:number] ]
47
+ self.area_code = hash_or_args[ keys[:area_code] ] || self.default_area_code
48
+ self.country_code = hash_or_args[ keys[:country_code] ] || self.default_country_code
49
+ self.extension = hash_or_args[ keys[:extension] ]
50
+
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?
54
+ end
55
+
56
+ # create a new phone number by parsing a string
57
+ # the format of the string is detect automatically (from FORMATS)
58
+ def self.parse(string, options={})
59
+ if string.present?
60
+ Country.load
61
+ extension = extract_extension(string)
62
+ string = normalize(string)
63
+
64
+ options[:country_code] ||= self.default_country_code
65
+ options[:area_code] ||= self.default_area_code
66
+
67
+ parts = split_to_parts(string, options)
68
+
69
+ pn = Phone.new(parts) if parts
70
+ if pn.present? and extension.present?
71
+ pn.extension = extension
72
+ end
73
+ return pn
94
74
  end
95
75
  end
96
-
97
- if country.nil?
98
- if options[:country_code].nil?
99
- raise "Must enter country code or set default country code"
76
+
77
+ # is this string a valid phone number?
78
+ def self.valid?(string)
79
+ begin
80
+ parse(string).present?
81
+ rescue RuntimeError # if we encountered exceptions (missing country code, missing area code etc)
82
+ return false
83
+ end
84
+ end
85
+
86
+ # split string into hash with keys :country_code, :area_code and :number
87
+ def self.split_to_parts(string, options = {})
88
+ country = detect_country(string)
89
+
90
+ if country
91
+ options[:country_code] = country.country_code
92
+ string = string.gsub(country.country_code_regexp, '0')
100
93
  else
101
- raise "Could not find country with country code #{options[:country_code]}"
94
+ if options[:country_code]
95
+ country = Country.find_by_country_code options[:country_code]
96
+ end
102
97
  end
98
+
99
+ if country.nil?
100
+ if options[:country_code].nil?
101
+ raise "Must enter country code or set default country code"
102
+ else
103
+ raise "Could not find country with country code #{options[:country_code]}"
104
+ end
105
+ end
106
+
107
+ format = detect_format(string, country)
108
+
109
+ return nil if format.nil?
110
+
111
+ parts = string.match formats(country)[format]
112
+
113
+ case format
114
+ when :short
115
+ {:number => parts[2], :area_code => parts[1], :country_code => options[:country_code]}
116
+ when :really_short
117
+ {:number => parts[1], :area_code => options[:area_code], :country_code => options[:country_code]}
118
+ end
103
119
  end
104
-
105
- format = detect_format(string, country)
106
-
107
- return nil if format.nil?
108
-
109
- parts = string.match formats(country)[format]
110
-
111
- case format
112
- when :short
113
- {:number => parts[2], :area_code => parts[1], :country_code => options[:country_code]}
114
- when :really_short
115
- {:number => parts[1], :area_code => options[:area_code], :country_code => options[:country_code]}
116
- end
117
- end
118
-
119
- # detect country from the string entered
120
- def self.detect_country(string)
121
- detected_country = nil
122
- # find if the number has a country code
123
- PhoneCountry.all.each_pair do |country_code, country|
124
- if string =~ country.country_code_regexp
125
- detected_country = country
120
+
121
+ # detect country from the string entered
122
+ def self.detect_country(string)
123
+ detected_country = nil
124
+ # find if the number has a country code
125
+ Country.all.each_pair do |country_code, country|
126
+ if string =~ country.country_code_regexp
127
+ detected_country = country
128
+ end
126
129
  end
130
+ detected_country
127
131
  end
128
- detected_country
129
- end
130
-
131
- def self.formats(country)
132
- area_code_regexp = country.area_code || DEFAULT_AREA_CODE
133
- {
134
- # 047451588, 013668734
135
- :short => Regexp.new('^0?(' + area_code_regexp + ')' + NUMBER),
136
- # 451588
137
- :really_short => Regexp.new('^' + NUMBER)
138
- }
139
- end
140
-
141
- # detect format (from FORMATS) of input string
142
- def self.detect_format(string_with_number, country)
143
- arr = []
144
- formats(country).each_pair do |format, regexp|
145
- arr << format if string_with_number =~ regexp
146
- end
147
-
148
- # raise "Detected more than 1 format for #{string_with_number}" if arr.size > 1
149
- if arr.length > 1
150
- # puts %Q{detect_format: more than one format found - #{arr.inspect}}
151
- return :really_short
152
- end
153
- arr.first
154
- end
155
-
156
- # fix string so it's easier to parse, remove extra characters etc.
157
- def self.normalize(string_with_number)
158
- string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+')
159
- end
160
-
161
- # pull off anything that look like an extension
162
- #TODO: refactor things so this doesn't change string as a side effect
163
- #
164
- def self.extract_extension(string)
165
- return nil if string.nil?
166
- if string.sub! /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(*([-0-9]{1,})\)*#?$/i, ''
167
- extension = $2
168
- return extension
132
+
133
+ def self.formats(country)
134
+ area_code_regexp = country.area_code || DEFAULT_AREA_CODE
135
+ {
136
+ # 047451588, 013668734
137
+ :short => Regexp.new('^0?(' + area_code_regexp + ')' + NUMBER),
138
+ # 451588
139
+ :really_short => Regexp.new('^' + NUMBER)
140
+ }
169
141
  end
142
+
143
+ # detect format (from FORMATS) of input string
144
+ def self.detect_format(string_with_number, country)
145
+ arr = []
146
+ formats(country).each_pair do |format, regexp|
147
+ arr << format if string_with_number =~ regexp
148
+ end
149
+
150
+ # raise "Detected more than 1 format for #{string_with_number}" if arr.size > 1
151
+ if arr.length > 1
152
+ # puts %Q{detect_format: more than one format found - #{arr.inspect}}
153
+ return :really_short
154
+ end
155
+ arr.first
156
+ end
157
+
158
+ # fix string so it's easier to parse, remove extra characters etc.
159
+ def self.normalize(string_with_number)
160
+ string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+')
161
+ end
162
+
163
+ # pull off anything that look like an extension
164
+ #TODO: refactor things so this doesn't change string as a side effect
165
+ #
166
+ def self.extract_extension(string)
167
+ return nil if string.nil?
168
+ if string.sub! /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(*([-0-9]{1,})\)*#?$/i, ''
169
+ extension = $2
170
+ return extension
171
+ end
172
+ #
173
+ # We already returned any recognizable extension.
174
+ # However, we might still have extra junk to the right
175
+ # of the phone number proper, so just chop it off.
176
+ #
177
+ idx = string.rindex(/[0-9]/)
178
+ return nil if idx.nil?
179
+ return nil if idx == (string.length - 1) # at the end
180
+ string.slice!((idx+1)..-1) # chop it
181
+ return nil
182
+ end
183
+
184
+ # format area_code with trailing zero (e.g. 91 as 091)
185
+ # format area_code with trailing zero (e.g. 91 as 091)
186
+ def area_code_long
187
+ "0" + area_code if area_code
188
+ end
189
+
190
+ # first n characters of :number
191
+ def number1
192
+ number[0...self.class.n1_length]
193
+ end
194
+
195
+ # everything left from number after the first n characters (see number1)
196
+ def number2
197
+ n2_length = number.size - self.class.n1_length
198
+ number[-n2_length, n2_length]
199
+ end
200
+
201
+ # Formats the phone number.
202
+ #
203
+ # if the method argument is a String, it is used as a format string, with the following fields being interpolated:
170
204
  #
171
- # We already returned any recognizable extension.
172
- # However, we might still have extra junk to the right
173
- # of the phone number proper, so just chop it off.
205
+ # * %c - country_code (385)
206
+ # * %a - area_code (91)
207
+ # * %A - area_code with leading zero (091)
208
+ # * %n - number (5125486)
209
+ # * %f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512)
210
+ # * %l - last characters of number (5486)
211
+ # * %x - entire extension
174
212
  #
175
- idx = string.rindex(/[0-9]/)
176
- return nil if idx.nil?
177
- return nil if idx == (string.length - 1) # at the end
178
- string.slice!((idx+1)..-1) # chop it
179
- return nil
180
- end
181
-
182
- # format area_code with trailing zero (e.g. 91 as 091)
183
- # format area_code with trailing zero (e.g. 91 as 091)
184
- def area_code_long
185
- "0" + area_code if area_code
186
- end
187
-
188
- # first n characters of :number
189
- def number1
190
- number[0...self.class.n1_length]
191
- end
192
-
193
- # everything left from number after the first n characters (see number1)
194
- def number2
195
- n2_length = number.size - self.class.n1_length
196
- number[-n2_length, n2_length]
197
- end
198
-
199
- # Formats the phone number.
200
- #
201
- # if the method argument is a String, it is used as a format string, with the following fields being interpolated:
202
- #
203
- # * %c - country_code (385)
204
- # * %a - area_code (91)
205
- # * %A - area_code with leading zero (091)
206
- # * %n - number (5125486)
207
- # * %f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512)
208
- # * %l - last characters of number (5486)
209
- # * %x - entire extension
210
- #
211
- # if the method argument is a Symbol, it is used as a lookup key for a format String in Phone.named_formats
212
- # pn.format(:europe)
213
- def format(fmt)
214
- if fmt.is_a?(Symbol)
215
- raise "The format #{fmt} doesn't exist'" unless named_formats.has_key?(fmt)
216
- format_number named_formats[fmt]
217
- else
218
- format_number(fmt)
219
- end
220
- end
221
-
222
- # the default format is "+%c%a%n"
223
- def to_s
224
- format(:default)
225
- end
226
-
227
- # does this number belong to the default country code?
228
- def has_default_country_code?
229
- country_code == self.class.default_country_code
230
- end
231
-
232
- # does this number belong to the default area code?
233
- def has_default_area_code?
234
- area_code == self.class.default_area_code
235
- end
236
-
237
- private
238
-
239
- def format_number(fmt)
240
- result = fmt.gsub("%c", country_code || "").
241
- gsub("%a", area_code || "").
242
- gsub("%A", area_code_long || "").
243
- gsub("%n", number || "").
244
- gsub("%f", number1 || "").
245
- gsub("%l", number2 || "").
246
- gsub("%x", extension || "")
247
- return result
248
- end
249
- end
213
+ # if the method argument is a Symbol, it is used as a lookup key for a format String in Phone.named_formats
214
+ # pn.format(:europe)
215
+ def format(fmt)
216
+ if fmt.is_a?(Symbol)
217
+ raise "The format #{fmt} doesn't exist'" unless named_formats.has_key?(fmt)
218
+ format_number named_formats[fmt]
219
+ else
220
+ format_number(fmt)
221
+ end
222
+ end
223
+
224
+ # the default format is "+%c%a%n"
225
+ def to_s
226
+ format(:default)
227
+ end
228
+
229
+ # does this number belong to the default country code?
230
+ def has_default_country_code?
231
+ country_code == self.class.default_country_code
232
+ end
233
+
234
+ # does this number belong to the default area code?
235
+ def has_default_area_code?
236
+ area_code == self.class.default_area_code
237
+ end
238
+
239
+ private
240
+
241
+ def format_number(fmt)
242
+ result = fmt.gsub("%c", country_code || "").
243
+ gsub("%a", area_code || "").
244
+ gsub("%A", area_code_long || "").
245
+ gsub("%n", number || "").
246
+ gsub("%f", number1 || "").
247
+ gsub("%l", number2 || "").
248
+ gsub("%x", extension || "")
249
+ return result
250
+ end
251
+ end
252
+ end
@@ -11,4 +11,9 @@ class DETest < Test::Unit::TestCase
11
11
  parse_test('+49 162 3499558', '49', '162', '3499558')
12
12
  end
13
13
 
14
+ # TODO: germany has 2-5 length area codes, that's why this test doesn't go through
15
+ #def test_country_side
16
+ # parse_test('+49 (0)6120 59511-23', '49', '6120', '5951123')
17
+ #end
18
+
14
19
  end
@@ -10,6 +10,10 @@ class FRTest < Test::Unit::TestCase
10
10
  def test_mobile
11
11
  parse_test('+33 6 11 22 33 44', '33', '6', '11223344')
12
12
  end
13
+
14
+ def test_mobile_07
15
+ parse_test('+33 7 11 22 33 44', '33', '7', '11223344')
16
+ end
13
17
 
14
18
  def test_voip
15
19
  parse_test('+33 9 11 22 33 44', '33', '9', '11223344')
@@ -28,49 +28,49 @@ class HRTest < Test::Unit::TestCase
28
28
  end
29
29
 
30
30
  def test_short_without_special_characters_with_country
31
- Phone.default_country_code = '385'
31
+ Phoner::Phone.default_country_code = '385'
32
32
  parse_test('044885047', '385', '44', '885047')
33
33
  end
34
34
 
35
35
  def test_zagreb_short_without_special_characters_with_country
36
- Phone.default_country_code = '385'
36
+ Phoner::Phone.default_country_code = '385'
37
37
  parse_test('013668734', '385', '1', '3668734')
38
38
  end
39
39
 
40
40
  def test_long_with_zero_in_brackets
41
- Phone.default_country_code = nil
41
+ Phoner::Phone.default_country_code = nil
42
42
  parse_test('+385 (0)1 366 8111', '385', '1', '3668111')
43
43
  end
44
44
 
45
45
  def test_has_default_country_code
46
- Phone.default_country_code = '385'
46
+ Phoner::Phone.default_country_code = '385'
47
47
 
48
- assert_equal Phone.parse('+38547451588').has_default_country_code?, true
49
- assert_equal Phone.parse('+38647451588').has_default_country_code?, false
48
+ assert_equal Phoner::Phone.parse('+38547451588').has_default_country_code?, true
49
+ assert_equal Phoner::Phone.parse('+38647451588').has_default_country_code?, false
50
50
  end
51
51
 
52
52
  def test_has_default_area_code
53
- Phone.default_country_code = '385'
54
- Phone.default_area_code = '47'
53
+ Phoner::Phone.default_country_code = '385'
54
+ Phoner::Phone.default_area_code = '47'
55
55
 
56
- assert_equal Phone.parse('047/451-588').has_default_area_code?, true
57
- assert_equal Phone.parse('032/336-1456').has_default_area_code?, false
56
+ assert_equal Phoner::Phone.parse('047/451-588').has_default_area_code?, true
57
+ assert_equal Phoner::Phone.parse('032/336-1456').has_default_area_code?, false
58
58
  end
59
59
 
60
60
  def test_validates
61
- Phone.default_country_code = nil
62
- assert_equal Phone.valid?('00385915125486'), true
63
- assert_equal Phone.valid?('+385915125486'), true
64
- assert_equal Phone.valid?('+385 (91) 512 5486'), true
65
- assert_equal Phone.valid?('+38547451588'), true
61
+ Phoner::Phone.default_country_code = nil
62
+ assert_equal Phoner::Phone.valid?('00385915125486'), true
63
+ assert_equal Phoner::Phone.valid?('+385915125486'), true
64
+ assert_equal Phoner::Phone.valid?('+385 (91) 512 5486'), true
65
+ assert_equal Phoner::Phone.valid?('+38547451588'), true
66
66
 
67
- Phone.default_country_code = '385'
68
- assert_equal Phone.valid?('0915125486'), true
69
- assert_equal Phone.valid?('091/512-5486'), true
70
- assert_equal Phone.valid?('091/512-5486'), true
71
- assert_equal Phone.valid?('091 512 54 86'), true
72
- assert_equal Phone.valid?('091-512-54-86'), true
73
- assert_equal Phone.valid?('047/451-588'), true
67
+ Phoner::Phone.default_country_code = '385'
68
+ assert_equal Phoner::Phone.valid?('0915125486'), true
69
+ assert_equal Phoner::Phone.valid?('091/512-5486'), true
70
+ assert_equal Phoner::Phone.valid?('091/512-5486'), true
71
+ assert_equal Phoner::Phone.valid?('091 512 54 86'), true
72
+ assert_equal Phoner::Phone.valid?('091-512-54-86'), true
73
+ assert_equal Phoner::Phone.valid?('047/451-588'), true
74
74
  end
75
75
 
76
76
  end
@@ -0,0 +1,129 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ ## Portugal
4
+ # source 1: http://pt.wikipedia.org/wiki/N%C3%BAmeros_de_telefone_em_Portugal
5
+ # source 2: http://www.anacom.pt/render.jsp?categoryId=279035
6
+
7
+ class PTTest < Test::Unit::TestCase
8
+
9
+ ### Prefixes followed by corresponding tests
10
+
11
+ ## 01-09: reserved for future use
12
+ ## 1: Short Numbers
13
+
14
+ ## 2: Landline / fixed numbers
15
+
16
+ ## two digits landlines
17
+ # 21: Lisbon (Lisboa)
18
+ def test_lisbon
19
+ parse_test('+351 21 123 4567', '351', '21', '1234567')
20
+ end
21
+ # 22: Oporto (Porto)
22
+ def test_oporto
23
+ parse_test('+351 22 123 4567', '351', '22', '1234567')
24
+ end
25
+
26
+ ## three digits landlines
27
+ # 231: Mealhada
28
+ def test_mealhada
29
+ parse_test('+351 231 123456', '351', '231', '123456')
30
+ end
31
+
32
+ # 232-295 (Viseu - Angra do Heroismo)
33
+ def test_viseu
34
+ parse_test('+351 232 123456', '351', '232', '123456')
35
+ end
36
+ def test_angra
37
+ parse_test('+351 295 123456', '351', '295', '123456')
38
+ end
39
+
40
+ # 296: Ponta Delgada
41
+ def test_pontadelgada
42
+ parse_test('+351 296 123456', '351', '296', '123456')
43
+ end
44
+
45
+ ## 3: Nomad services
46
+ ## 4: Not used
47
+ ## 5: Free
48
+ ## 6: Audiotext service, data network access...
49
+
50
+ ## 7: Premium numbers ("Universal access numbers")
51
+
52
+ # 707-708: Premium Numbers
53
+ def test_707
54
+ Phoner::Phone.default_country_code = '351'
55
+ parse_test('707 123 456', '351', '707', '123456')
56
+ end
57
+
58
+ ## 8: Free toll numbers
59
+
60
+ # 800: Numero verde ("Green Number")
61
+ def test_800
62
+ Phoner::Phone.default_country_code = '351'
63
+ parse_test('800 123 456', '351', '800', '123456')
64
+ end
65
+ # 808: Numero azul ("Blue Number")
66
+ def test_808
67
+ Phoner::Phone.default_country_code = '351'
68
+ parse_test('808 123 456', '351', '808', '123456')
69
+ end
70
+ # 809: Custo partilhado ("Shared cost")
71
+ def test_809
72
+ Phoner::Phone.default_country_code = '351'
73
+ parse_test('809 123 456', '351', '809', '123456')
74
+ end
75
+
76
+ ## 9: Mobile networks
77
+
78
+ ## two-digits mobile networks
79
+ # 91: Vodafone
80
+ def test_vodafone
81
+ parse_test('+351 91 1234567', '351', '91', '1234567')
82
+ end
83
+ # 93: Optimus
84
+ def test_optimus
85
+ parse_test('+351 93 1234567', '351', '93', '1234567')
86
+ end
87
+ # 96: TMN two-digits
88
+ def test_tmn
89
+ parse_test('+351 96 1234567', '351', '96', '1234567')
90
+ end
91
+
92
+ ## three-digits mobile networks
93
+ # 921: TMN three-digits
94
+ def test_tmn921
95
+ parse_test('+351 921 123456', '351', '921', '123456')
96
+ end
97
+ # 923-926: TMN three-digits (corporate networks)
98
+ def test_tmn923
99
+ parse_test('+351 925 123456', '351', '925', '123456')
100
+ end
101
+ # 922: Phone-ix
102
+ def test_phoneix
103
+ parse_test('+351 922 123456', '351', '922', '123456')
104
+ end
105
+ # 929: Zon Mobile
106
+ def test_zonmobile
107
+ parse_test('+351 929 123456', '351', '929', '123456')
108
+ end
109
+
110
+ def test_validates
111
+ Phoner::Phone.default_country_code = nil
112
+ assert_equal Phoner::Phone.valid?('00351211234567'), true
113
+ assert_equal Phoner::Phone.valid?('00351911234567'), true
114
+ assert_equal Phoner::Phone.valid?('+351931234567'), true
115
+ assert_equal Phoner::Phone.valid?('+351 (911) 123 456'), true
116
+ assert_equal Phoner::Phone.valid?('+351921123456'), true
117
+
118
+ Phoner::Phone.default_country_code = '351'
119
+ assert_equal Phoner::Phone.valid?('(931) 234-567'), true
120
+ assert_equal Phoner::Phone.valid?('(211) 234 567'), true
121
+ assert_equal Phoner::Phone.valid?('232-123-456'), true
122
+ assert_equal Phoner::Phone.valid?('232123456'), true
123
+ assert_equal Phoner::Phone.valid?('92 212 34 56'), true
124
+ assert_equal Phoner::Phone.valid?('221234567'), true
125
+ assert_equal Phoner::Phone.valid?('708123456'), true
126
+ assert_equal Phoner::Phone.valid?('800 123 456'), true
127
+ end
128
+
129
+ end
@@ -12,13 +12,13 @@ class USTest < Test::Unit::TestCase
12
12
  end
13
13
 
14
14
  def test_long_with_default_country_code
15
- Phone.default_country_code = '1'
15
+ Phoner::Phone.default_country_code = '1'
16
16
  parse_test('2069735100', '1', '206', '9735100')
17
17
  end
18
18
 
19
19
  def test_short_with_default_country_code_and_area_code
20
- Phone.default_country_code = '1'
21
- Phone.default_area_code = '206'
20
+ Phoner::Phone.default_country_code = '1'
21
+ Phoner::Phone.default_area_code = '206'
22
22
  parse_test('9735100', '1', '206', '9735100')
23
23
  end
24
24
  end
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
3
3
  class ExtensionTest < Test::Unit::TestCase
4
4
 
5
5
  def test_parse_usa_long_with_simple_extension
6
- pn = Phone.parse "+1 2069735100 x143"
6
+ pn = Phoner::Phone.parse "+1 2069735100 x143"
7
7
 
8
8
  assert_not_nil pn, %Q{parse should pass}
9
9
  assert_equal '9735100', pn.number
@@ -13,12 +13,12 @@ class ExtensionTest < Test::Unit::TestCase
13
13
  end
14
14
 
15
15
  def test_to_s_with_extension
16
- pn = Phone.new '5125486', '91', '385', '143'
16
+ pn = Phoner::Phone.new '5125486', '91', '385', '143'
17
17
  assert_equal '+385915125486x143', pn.format(:default_with_extension)
18
18
  end
19
19
 
20
20
  def test_format_with_extension
21
- pn = Phone.new '5125486', '91', '385', '143'
21
+ pn = Phoner::Phone.new '5125486', '91', '385', '143'
22
22
  assert_equal '(091)/512-5486 x 143', pn.format('(%A)/%f-%l x %x')
23
23
  end
24
24
 
@@ -3,104 +3,104 @@ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
3
3
  class PhoneTest < Test::Unit::TestCase
4
4
 
5
5
  def test_number_without_country_code_initialize
6
- Phone.default_country_code = nil
6
+ Phoner::Phone.default_country_code = nil
7
7
 
8
8
  assert_raise RuntimeError do
9
- pn = Phone.new '5125486', '91'
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
- Phone.default_country_code = nil
15
- Phone.default_area_code = nil
14
+ Phoner::Phone.default_country_code = nil
15
+ Phoner::Phone.default_area_code = nil
16
16
 
17
17
  assert_raise RuntimeError do
18
- pn = Phone.new '451588'
18
+ pn = Phoner::Phone.new '451588'
19
19
  end
20
20
  end
21
21
 
22
22
  def test_number_with_default_area_code_initialize
23
- Phone.default_country_code = '385'
24
- Phone.default_area_code = '47'
23
+ Phoner::Phone.default_country_code = '385'
24
+ Phoner::Phone.default_area_code = '47'
25
25
 
26
- pn = Phone.new '451588'
26
+ pn = Phoner::Phone.new '451588'
27
27
  assert pn.number == '451588'
28
28
  assert pn.area_code == '47'
29
29
  assert pn.country_code == '385'
30
30
  end
31
31
 
32
32
  def test_number_with_default_country_code_initialize
33
- Phone.default_country_code = '386'
33
+ Phoner::Phone.default_country_code = '386'
34
34
 
35
- pn = Phone.new '5125486', '91'
35
+ pn = Phoner::Phone.new '5125486', '91'
36
36
  assert pn.number == '5125486'
37
37
  assert pn.area_code == '91'
38
38
  assert pn.country_code == '386'
39
39
  end
40
40
 
41
41
  def test_number_with_country_code_initialize
42
- Phone.default_country_code = '387'
42
+ Phoner::Phone.default_country_code = '387'
43
43
 
44
- pn = Phone.new '5125486', '91', '385'
44
+ pn = Phoner::Phone.new '5125486', '91', '385'
45
45
  assert pn.number == '5125486'
46
46
  assert pn.area_code == '91'
47
47
  assert pn.country_code == '385'
48
48
  end
49
49
 
50
50
  def test_parse_empty
51
- assert_equal Phone.parse(''), nil
52
- assert_equal Phone.parse(nil), nil
51
+ assert_equal Phoner::Phone.parse(''), nil
52
+ assert_equal Phoner::Phone.parse(nil), nil
53
53
  end
54
54
 
55
55
  def test_parse_short_without_special_characters_without_country
56
- Phone.default_country_code = nil
56
+ Phoner::Phone.default_country_code = nil
57
57
 
58
58
  assert_raise RuntimeError do
59
- pn = Phone.parse "0915125486"
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
- Phone.default_country_code = nil
64
+ Phoner::Phone.default_country_code = nil
65
65
 
66
66
  assert_raise RuntimeError do
67
- pn = Phone.parse "091/512-5486"
67
+ pn = Phoner::Phone.parse "091/512-5486"
68
68
  end
69
69
  end
70
70
 
71
71
  def test_to_s
72
- Phone.default_country_code = nil
73
- pn = Phone.new '5125486', '91', '385'
72
+ Phoner::Phone.default_country_code = nil
73
+ pn = Phoner::Phone.new '5125486', '91', '385'
74
74
  assert pn.to_s == '+385915125486'
75
75
  end
76
76
 
77
77
  def test_to_s_without_country_code
78
- Phone.default_country_code = '385'
79
- pn = Phone.new '5125486', '91'
78
+ Phoner::Phone.default_country_code = '385'
79
+ pn = Phoner::Phone.new '5125486', '91'
80
80
  assert pn.format("0%a%n") == '0915125486'
81
81
  end
82
82
 
83
83
  def test_format_special_with_country_code
84
- Phone.default_country_code = nil
85
- pn = Phone.new '5125486', '91', '385'
84
+ Phoner::Phone.default_country_code = nil
85
+ pn = Phoner::Phone.new '5125486', '91', '385'
86
86
  assert pn.format("+ %c (%a) %n") == '+ 385 (91) 5125486'
87
87
  end
88
88
 
89
89
  def test_format_special_without_country_code
90
- Phone.default_country_code = '385'
91
- pn = Phone.new '5125486', '91'
90
+ Phoner::Phone.default_country_code = '385'
91
+ pn = Phoner::Phone.new '5125486', '91'
92
92
  assert_equal pn.format("%A/%f-%l"), '091/512-5486'
93
93
  end
94
94
 
95
95
  def test_format_with_symbol_specifier
96
- Phone.default_country_code = nil
97
- pn = Phone.new '5125486', '91', '385'
96
+ Phoner::Phone.default_country_code = nil
97
+ pn = Phoner::Phone.new '5125486', '91', '385'
98
98
  assert_equal pn.format(:europe), '+385 (0) 91 512 5486'
99
99
  end
100
100
 
101
101
  def test_doesnt_validate
102
- assert_equal Phone.valid?('asdas'), false
103
- assert_equal Phone.valid?('385915125486'), false
102
+ assert_equal Phoner::Phone.valid?('asdas'), false
103
+ assert_equal Phoner::Phone.valid?('385915125486'), false
104
104
  end
105
105
 
106
106
  end
@@ -6,7 +6,7 @@ require 'test/unit'
6
6
  require 'phone'
7
7
 
8
8
  def parse_test(raw, country_code, area_code, number)
9
- pn = Phone.parse(raw)
9
+ pn = Phoner::Phone.parse(raw)
10
10
 
11
11
  assert_not_nil pn, %Q{parse should pass}
12
12
  assert_equal pn.country_code, country_code
metadata CHANGED
@@ -1,14 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phone
3
3
  version: !ruby/object:Gem::Version
4
- hash: 37
5
4
  prerelease: false
6
5
  segments:
6
+ - 1
7
7
  - 0
8
- - 9
9
- - 9
10
- - 3
11
- version: 0.9.9.3
8
+ version: "1.0"
12
9
  platform: ruby
13
10
  authors:
14
11
  - Tomislav Car
@@ -18,7 +15,7 @@ autorequire:
18
15
  bindir: bin
19
16
  cert_chain: []
20
17
 
21
- date: 2010-07-30 00:00:00 +02:00
18
+ date: 2011-04-05 00:00:00 +02:00
22
19
  default_executable:
23
20
  dependencies: []
24
21
 
@@ -39,7 +36,7 @@ files:
39
36
  - LICENSE
40
37
  - data/phone_countries.yml
41
38
  - lib/phone.rb
42
- - lib/phone_country.rb
39
+ - lib/country.rb
43
40
  - lib/support.rb
44
41
  - test/extension_test.rb
45
42
  - test/phone_test.rb
@@ -53,6 +50,7 @@ files:
53
50
  - test/countries/hr_test.rb
54
51
  - test/countries/hu_test.rb
55
52
  - test/countries/nl_test.rb
53
+ - test/countries/pt_test.rb
56
54
  - test/countries/rs_test.rb
57
55
  - test/countries/se_test.rb
58
56
  - test/countries/si_test.rb
@@ -76,7 +74,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
74
  requirements:
77
75
  - - ">="
78
76
  - !ruby/object:Gem::Version
79
- hash: 3
80
77
  segments:
81
78
  - 0
82
79
  version: "0"
@@ -85,7 +82,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
82
  requirements:
86
83
  - - ">="
87
84
  - !ruby/object:Gem::Version
88
- hash: 3
89
85
  segments:
90
86
  - 0
91
87
  version: "0"
@@ -109,6 +105,7 @@ test_files:
109
105
  - test/countries/hr_test.rb
110
106
  - test/countries/hu_test.rb
111
107
  - test/countries/nl_test.rb
108
+ - test/countries/pt_test.rb
112
109
  - test/countries/rs_test.rb
113
110
  - test/countries/se_test.rb
114
111
  - test/countries/si_test.rb
@@ -1,27 +0,0 @@
1
- class PhoneCountry < Struct.new(:name, :country_code, :char_2_code, :area_code)
2
- cattr_accessor :all
3
-
4
- def self.load
5
- return @@all if @@all.present?
6
-
7
- data_file = File.join(File.dirname(__FILE__), '..', 'data', 'phone_countries.yml')
8
-
9
- @@all = {}
10
- YAML.load(File.read(data_file)).each_pair do |key, c|
11
- @@all[key] = PhoneCountry.new(c[:name], c[:country_code], c[:char_2_code], c[:area_code])
12
- end
13
- @@all
14
- end
15
-
16
- def to_s
17
- name
18
- end
19
-
20
- def self.find_by_country_code(code)
21
- @@all[code]
22
- end
23
-
24
- def country_code_regexp
25
- Regexp.new("^[+]#{country_code}")
26
- end
27
- end