phoney 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -78,7 +78,6 @@ When given a symbol you can let the parser guess the best format and pass in one
78
78
  == TODOs
79
79
 
80
80
  - More test specs for different countries
81
- - Correctly handle numbers that have some digits BEFORE the area_code!
82
81
 
83
82
  == Licence
84
83
 
@@ -1,5 +1,5 @@
1
1
  class PhoneNumber
2
- attr_accessor :country_code, :area_code, :number
2
+ attr_accessor :country_code, :prefix_code, :area_code, :number
3
3
 
4
4
  class << self
5
5
  def default_region
@@ -44,10 +44,12 @@ class PhoneNumber
44
44
  end
45
45
 
46
46
  self.number = params[:number].to_s
47
+ # The rare case when some digits are in front of the area code
48
+ self.prefix_code = params[:prefix_code].to_s
47
49
  # Can be empty, because some special numbers just don't have an area code (e.g. 911)
48
50
  self.area_code = params[:area_code].to_s || self.class.default_area_code
49
51
  self.country_code = params[:country_code].to_s || country_code || self.class.default_country_code
50
-
52
+
51
53
  raise "Must enter number" if(self.number.nil? || self.number.empty?)
52
54
  raise "Must enter country code or set default country code" if(self.country_code.nil? || self.country_code.empty?)
53
55
  end
@@ -74,7 +76,7 @@ class PhoneNumber
74
76
  if fmt.is_a?(Symbol)
75
77
  case fmt
76
78
  when :default
77
- Parser::parse("+#{country_code} #{area_code} #{number}", country_code)
79
+ Parser::parse("+#{country_code} #{prefix_code}#{area_code} #{number}", country_code)
78
80
  when :national
79
81
  Parser::parse("#{area_code} #{number}", country_code)
80
82
  when :local
@@ -20,6 +20,7 @@ class PhoneNumber
20
20
 
21
21
  region = Region.find(region_code) || PhoneNumber.default_region
22
22
  country_code = region.country_code.to_s
23
+ prefix_code = nil
23
24
  area_code = nil
24
25
 
25
26
  dialout_prefix = get_dialout_prefix(phone_number, region)
@@ -69,6 +70,9 @@ class PhoneNumber
69
70
 
70
71
  # now that know how to format the number, do the formatting work...
71
72
  if(matching_rule)
73
+ if(matching_rule[:areacode_offset] > 0)
74
+ prefix_code = phone_number[0, matching_rule[:areacode_offset]]
75
+ end
72
76
  area_code = phone_number[matching_rule[:areacode_offset], matching_rule[:areacode_length]]
73
77
  number = phone_number[matching_rule[:areacode_offset]+matching_rule[:areacode_length]..-1]
74
78
  phone_number = format(phone_number, matching_rule[:format].to_s)
@@ -115,10 +119,16 @@ class PhoneNumber
115
119
  number.gsub!(/[^0-9]/,'') if number
116
120
 
117
121
  # Finally...we can output our parts as a hash
118
- { :formatted_number => phone_number, :area_code => area_code, :country_code => country_code, :number => number }
122
+ { :formatted_number => phone_number,
123
+ :prefix_code => prefix_code,
124
+ :area_code => area_code,
125
+ :country_code => country_code,
126
+ :number => number }
119
127
  end
120
128
 
121
- # private
129
+ private
130
+ # Returns the rule sets that we need to check for a given number and region.
131
+ # The rule_sets are filtered by the length of the number!
122
132
  def get_rule_sets_for_region(string, region)
123
133
  rule_sets = []
124
134
 
@@ -131,6 +141,8 @@ class PhoneNumber
131
141
  rule_sets
132
142
  end
133
143
 
144
+ # Given any number, find the rule in rule_sets that applies.
145
+ # Returns nil if no matching rule was found!
134
146
  def find_matching_rule(number, rule_sets)
135
147
  return nil if !number.match(/\A[0-9]+\Z/)
136
148
  match = nil
@@ -154,11 +166,14 @@ class PhoneNumber
154
166
  match
155
167
  end
156
168
 
169
+ # According to the region, is this number input trying to dial out?
157
170
  def dialing_out?(string, region=nil)
158
171
  region ||= PhoneNumber.default_region
159
172
  !get_dialout_prefix(string, region).empty?
160
173
  end
161
-
174
+
175
+ # Get the dialout prefix from the given string.
176
+ # Returns an empty string if no dialout prefix was found.
162
177
  def get_dialout_prefix(string, region=nil)
163
178
  region ||= PhoneNumber.default_region
164
179
  prefixes = region.dialout_prefixes
@@ -189,6 +204,8 @@ class PhoneNumber
189
204
  dialout_prefix
190
205
  end
191
206
 
207
+ # Get the national prefix from the given string.
208
+ # Returns an empty string if no national prefix was found.
192
209
  def get_national_prefix(string, region=nil)
193
210
  region ||= PhoneNumber.default_region
194
211
  prefix = region.national_prefix
@@ -202,6 +219,9 @@ class PhoneNumber
202
219
  national_prefix
203
220
  end
204
221
 
222
+ # Get the dialout region by looking at the string.
223
+ # Returns a Region object if we're dialing outside a region that is supported.
224
+ # Otherwise returns nil.
205
225
  def get_dialout_region(string, region)
206
226
  region ||= PhoneNumber.default_region
207
227
  dialout_prefix = get_dialout_prefix(string, region)
@@ -44,7 +44,7 @@ class PhoneNumber
44
44
  # Strips all non-numberpad characters from a string
45
45
  # => For example: "+45 (123) 023 1.1.1" -> "+45123023111"
46
46
  def normalize(string_with_number)
47
- string_with_number.scan(/[0-9+*#]/).to_s unless string_with_number.nil?
47
+ string_with_number.gsub(/[^0-9+*#]/,'') unless string_with_number.nil?
48
48
  end
49
49
 
50
50
  end
@@ -2,8 +2,8 @@ class PhoneNumber
2
2
 
3
3
  module VERSION #:nodoc:
4
4
  MAJOR = 0
5
- MINOR = 0
6
- TINY = 4
5
+ MINOR = 1
6
+ TINY = 0
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{phoney}
8
- s.version = "0.0.4"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jan Habermann"]
@@ -12,210 +12,29 @@ describe PhoneNumber do
12
12
  pn.to_s.should == "+1 (704) 123-4567"
13
13
  end
14
14
 
15
+ it "should extract the country, area, and number correctly" do
16
+ pn = PhoneNumber.new '7041234567'
17
+
18
+ pn.country_code.should == "1"
19
+ pn.area_code.should == "704"
20
+ pn.number.should == "1234567"
21
+ end
15
22
  end
16
23
 
17
- end
18
-
24
+ describe "using a number that has a prefix_code before the area code" do
25
+ before(:each) do
26
+ @pn = PhoneNumber.new("+55 12 34 5678-9012")
27
+ end
28
+
29
+ it "should have the correct prefix/area code assigned" do
30
+ @pn.prefix_code.should == "12"
31
+ @pn.area_code.should == "34"
32
+ @pn.number == "56789012"
33
+ end
34
+
35
+ it "should output the correct canonical format" do
36
+ @pn.to_s.should == "+55 (12 34) 5678-9012"
37
+ end
38
+ end
19
39
 
20
- # def test_number_without_country_code_initialize
21
- # PhoneNumber.default_country_code = nil
22
- #
23
- # assert_raise RuntimeError do
24
- # pn = PhoneNumber.new '5125486', '91'
25
- # end
26
- # end
27
- #
28
- # def test_number_without_country_and_area_code_initialize
29
- # PhoneNumber.default_country_code = nil
30
- # PhoneNumber.default_area_code = nil
31
- #
32
- # assert_raise RuntimeError do
33
- # pn = PhoneNumber.new '451588'
34
- # end
35
- # end
36
- #
37
- # def test_number_with_default_area_code_initialize
38
- # PhoneNumber.default_country_code = '385'
39
- # PhoneNumber.default_area_code = '47'
40
- #
41
- # pn = PhoneNumber.new '451588'
42
- # assert pn.number == '451588'
43
- # assert pn.area_code == '47'
44
- # assert pn.country_code == '385'
45
- # end
46
- #
47
- # def test_number_with_default_country_code_initialize
48
- # PhoneNumber.default_country_code = '386'
49
- #
50
- # pn = PhoneNumber.new '5125486', '91'
51
- # assert pn.number == '5125486'
52
- # assert pn.area_code == '91'
53
- # assert pn.country_code == '386'
54
- # end
55
- #
56
- # def test_number_with_country_code_initialize
57
- # PhoneNumber.default_country_code = '387'
58
- #
59
- # pn = PhoneNumber.new '5125486', '91', '385'
60
- # assert pn.number == '5125486'
61
- # assert pn.area_code == '91'
62
- # assert pn.country_code == '385'
63
- # end
64
- #
65
- # def test_parse_empty
66
- # assert_equal PhoneNumber.parse(''), nil
67
- # assert_equal PhoneNumber.parse(nil), nil
68
- # end
69
- #
70
- # def test_parse_long_without_special_characters
71
- # pn = PhoneNumber.parse "+385915125486"
72
- #
73
- # assert_equal pn.number, '5125486'
74
- # assert_equal pn.area_code, '91'
75
- # assert_equal pn.country_code, '385'
76
- # end
77
- #
78
- # def test_parse_zagreb_long_without_special_characters
79
- # pn = PhoneNumber.parse "+38513668734"
80
- #
81
- # assert_equal pn.number, '3668734'
82
- # assert_equal pn.area_code, '1'
83
- # assert_equal pn.country_code, '385'
84
- # end
85
- #
86
- # def test_parse_long_with_special_characters
87
- # pn = PhoneNumber.parse "+ 385 (91) 512 / 5486 "
88
- #
89
- # assert pn.number == '5125486'
90
- # assert pn.area_code == '91'
91
- # assert pn.country_code == '385'
92
- # end
93
- #
94
- # def test_parse_long_with_leading_zeros
95
- # pn = PhoneNumber.parse "00385915125486"
96
- #
97
- # assert pn.number == '5125486'
98
- # assert pn.area_code == '91'
99
- # assert pn.country_code == '385'
100
- # end
101
- #
102
- # def test_parse_zagreb_long_with_leading_zeros
103
- # pn = PhoneNumber.parse "0038513668734"
104
- #
105
- # assert pn.number == '3668734'
106
- # assert pn.area_code == '1'
107
- # assert pn.country_code == '385'
108
- # end
109
- #
110
- # def test_parse_short_without_special_characters_without_country
111
- # PhoneNumber.default_country_code = nil
112
- #
113
- # assert_raise RuntimeError do
114
- # pn = PhoneNumber.parse "0915125486"
115
- # end
116
- # end
117
- #
118
- # def test_parse_short_without_special_characters_with_country
119
- # PhoneNumber.default_country_code = '385'
120
- #
121
- # pn = PhoneNumber.parse "044885047"
122
- #
123
- # assert_equal pn.number, '885047'
124
- # assert_equal pn.area_code, '44'
125
- # assert pn.country_code == '385'
126
- # end
127
- #
128
- # def test_parse_zagreb_short_without_special_characters_with_country
129
- # PhoneNumber.default_country_code = '385'
130
- #
131
- # pn = PhoneNumber.parse "013668734"
132
- #
133
- # assert_equal pn.number, '3668734'
134
- # assert_equal pn.area_code, '1'
135
- # assert_equal pn.country_code, '385'
136
- # end
137
- #
138
- # def test_parse_short_with_special_characters_without_country
139
- # PhoneNumber.default_country_code = nil
140
- #
141
- # assert_raise RuntimeError do
142
- # pn = PhoneNumber.parse "091/512-5486"
143
- # end
144
- # end
145
- #
146
- # def test_parse_long_with_zero_in_brackets
147
- # PhoneNumber.default_country_code = nil
148
- #
149
- # pn = PhoneNumber.parse '+385 (0)1 366 8111'
150
- # assert_equal pn.country_code, '385'
151
- # assert_equal pn.area_code, '1'
152
- # assert_equal pn.number, '3668111'
153
- # end
154
- #
155
- #
156
- #
157
- # def test_to_s
158
- # PhoneNumber.default_country_code = nil
159
- # pn = PhoneNumber.new '5125486', '91', '385'
160
- # assert pn.to_s == '+385915125486'
161
- # end
162
- #
163
- # def test_to_s_without_country_code
164
- # PhoneNumber.default_country_code = '385'
165
- # pn = PhoneNumber.new '5125486', '91'
166
- # assert pn.format("0%a%n") == '0915125486'
167
- # end
168
- #
169
- # def test_format_special_with_country_code
170
- # PhoneNumber.default_country_code = nil
171
- # pn = PhoneNumber.new '5125486', '91', '385'
172
- # assert pn.format("+ %c (%a) %n") == '+ 385 (91) 5125486'
173
- # end
174
- #
175
- # def test_format_special_without_country_code
176
- # PhoneNumber.default_country_code = '385'
177
- # pn = PhoneNumber.new '5125486', '91'
178
- # assert_equal pn.format("%A/%f-%l"), '091/512-5486'
179
- # end
180
- #
181
- # def test_format_with_symbol_specifier
182
- # PhoneNumber.default_country_code = nil
183
- # pn = PhoneNumber.new '5125486', '91', '385'
184
- # assert_equal pn.format(:europe), '+385 (0) 91 512 5486'
185
- # end
186
- #
187
- # def test_doesnt_validate
188
- # assert_equal PhoneNumber.valid?('asdas'), false
189
- # assert_equal PhoneNumber.valid?('385915125486'), false
190
- # end
191
- #
192
- # def test_validates
193
- # PhoneNumber.default_country_code = nil
194
- # assert_equal PhoneNumber.valid?('00385915125486'), true
195
- # assert_equal PhoneNumber.valid?('+385915125486'), true
196
- # assert_equal PhoneNumber.valid?('+385 (91) 512 5486'), true
197
- # assert_equal PhoneNumber.valid?('+38547451588'), true
198
- #
199
- # PhoneNumber.default_country_code = '385'
200
- # assert_equal PhoneNumber.valid?('0915125486'), true
201
- # assert_equal PhoneNumber.valid?('091/512-5486'), true
202
- # assert_equal PhoneNumber.valid?('091/512-5486'), true
203
- # assert_equal PhoneNumber.valid?('091 512 54 86'), true
204
- # assert_equal PhoneNumber.valid?('091-512-54-86'), true
205
- # assert_equal PhoneNumber.valid?('047/451-588'), true
206
- # end
207
- #
208
- # def test_has_default_country_code
209
- # PhoneNumber.default_country_code = '385'
210
- #
211
- # assert_equal PhoneNumber.parse('+38547451588').has_default_country_code?, true
212
- # assert_equal PhoneNumber.parse('+38647451588').has_default_country_code?, false
213
- # end
214
- #
215
- # def test_has_default_area_code
216
- # PhoneNumber.default_country_code = '385'
217
- # PhoneNumber.default_area_code = '47'
218
- #
219
- # assert_equal PhoneNumber.parse('047/451-588').has_default_area_code?, true
220
- # assert_equal PhoneNumber.parse('032/336-1456').has_default_area_code?, false
221
- # end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phoney
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Habermann