phone_classifier 0.0.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.
@@ -0,0 +1,81 @@
1
+ Phone Classifier
2
+ ================
3
+
4
+ Used to quickly classifiy a phone number into :landline, :mobile or :forbidden
5
+
6
+ Installation
7
+ ------------
8
+
9
+ gem install phone_classifier
10
+
11
+ Usage
12
+ -----
13
+
14
+ PhoneClassifier.new('41791234567').kind # => :mobile
15
+ PhoneClassifier.new('41791234567').mobile? # => true
16
+ PhoneClassifier.new('41791234567').forbidden? # => false
17
+
18
+
19
+ Supported Countries
20
+ -------------------
21
+
22
+ * +1, USA (will only reject service numbers, all other numbers considered mobile). Blame the US numbering systems
23
+ * +6, Greece
24
+ * +31, Netherlands
25
+ * +30, Greece
26
+ * +31, Netherlands
27
+ * +32, Belgium
28
+ * +33, France
29
+ * +34, Spain
30
+ * +36, Hungary
31
+ * +39, Italy
32
+ * +40, Romania
33
+ * +41, Switerzland
34
+ * +43, Austria
35
+ * +44, UK
36
+ * +45, Denmark
37
+ * +46, Sweden
38
+ * +47, Norway
39
+ * +48, Poland
40
+ * +49, Germany
41
+ * +212, Morocco
42
+ * +213, Algeria
43
+ * +233, Ghana
44
+ * +234, Nigeria
45
+ * +249, Sudan
46
+ * +350, Gibraltar
47
+ * +351, Portugal
48
+ * +352, Luxembourg
49
+ * +353, Ireland
50
+ * +354, Iceland
51
+ * +356, Malta
52
+ * +357, Cyprus
53
+ * +358, Finland
54
+ * +359, Bulgaria
55
+ * +370, Lithuania
56
+ * +372, Estonia
57
+ * +377, Monaco
58
+ * +386, Slovenia
59
+ * +420, Czech Republic
60
+ * +421, Slovakia
61
+
62
+
63
+ Where is it used
64
+ ----------------
65
+
66
+ Used in Mobino[http://mobino.com], a mobile payment service, to classifiy client phone numbers
67
+
68
+ Related
69
+ -------
70
+
71
+ PhoneClassifier uses the fantastic 'Phony'[http://github.com/floere/phony.git] library to normalize numbers. Phony is
72
+ the tool of choice if you need to work with phone numbers in Ruby.
73
+
74
+ Contributing
75
+ ------------
76
+
77
+ 1. Fork it
78
+ 2. Create your feature branch
79
+ 3. Commit your changes
80
+ 4. Push to the branch
81
+ 5. Create new Pull Request
@@ -0,0 +1,127 @@
1
+ require 'phony'
2
+
3
+ class PhoneClassifier
4
+
5
+ # authorative info: http://www.itu.int/oth/T0202.aspx?parent=T0202
6
+ # Numbering schemes for mobile numbers
7
+ #
8
+ @@mobile = {
9
+ '1' => %w{ [2-9].. }, # USA
10
+ '30' => %w{ 6.. }, # Greece
11
+ '31' => %w{6}, # Netherlands
12
+ '32' => %w{4..}, # Belgium
13
+ '33' => %w{6 7}, # France
14
+ '34' => %w{6. 7.}, # Spain
15
+ '36' => %w{ 20 30 31 70 }, # Hungary
16
+ '39' => %w{3...}, # Italy
17
+ '40' => %w{ 7. }, # Romania
18
+ '41' => %w{ 7[6-9] }, # Switerzland
19
+ '43' => %w{ 67 68 644 65. 66. }, # Austria
20
+ '44' => %w{ 7[4-9].. }, # UK
21
+ '45' => %w{ 2[0-9] 30 31 40 41 42 5[0-3] 60 61 71 81 9[1-3] }, # Denmark
22
+ '46' => %w{ 70 72 73 76 }, # Sweden
23
+ '47' => %w{ 9.. 4.. 58. 59. }, # Norway
24
+ '48' => %w{50. 51. 53. 60. 66. 69. 72. 78. 79. 88.}, # Poland
25
+ '49' => %w{ 151 152 157 159 160 162 163 17[0-9] }, # Germany
26
+ '212' => %w{ 6. }, # Morocco
27
+ '213' => %w{ 55 66 697 699 77 790 796 }, # Algeria
28
+ '233' => %w{ 20 23 24 26 27 28 50 54 57 },
29
+ '234' => %w{ 702[1-9] 70[3-9] 8[01][2-9] },
30
+ '249' => %w{ 9. }, # Sudan
31
+ '350' => %w{ 5. 6. }, # Gibraltar
32
+ '351' => %w{ 9. }, # Portugal
33
+ '352' => %w{ 621 628 661 668 691 698 }, # Luxembourg
34
+ '353' => %w{ 8[2-9] }, # Ireland
35
+ '354' => %w{ 6.. 7.. 8..}, # Iceland
36
+ '356' => %w{ 77 79 98 99 }, # Malta
37
+ '357' => %w{ 95 96 97 99 }, # Cyprus
38
+ '358' => %w{ 4. 50 }, # Finland
39
+ '359' => %w{ 430 87. 88. 89. 98. }, # Bulgaria
40
+ '370' => %w{ 6.. }, # Lithuania
41
+ '372' => %w{ 5.. 81. 82.}, # Estonia
42
+ '377' => %w{ 4. 6 }, # Monaco
43
+ '386' => %w{ 30 40 31 41 51 71 70 64}, # Slovenia
44
+ '420' => %w{ 60[1-8] 72. 73. 77. 79. 91 }, # Czech Republic
45
+ '421' => %w{ 9.. }, # Slovakia
46
+ }
47
+
48
+ @@forbidden = {
49
+ '1' => %w{ 1.. }, # USA
50
+ '30' => %w{ 8.. 9.. }, # Greece
51
+ '31' => %w{84 85 87 88 91 112 676 800 900 906 909}, # Netherlands
52
+ '32' => %w{70 800 90[0-9]}, # Belgium
53
+ '33' => %w{8}, # France
54
+ '34' => %w{10 11 80 90}, # Spain
55
+ '36' => %w{ 40 41 50 55 60 71 80 90 91 }, # Hungary
56
+ '39' => %w{1 7 8}, # Italy
57
+ '40' => %w{ 8. 9. }, # Romania
58
+ '41' => %w{ 74 800 840 842 844 848 860 868 900 901 906 }, # Switzerland
59
+ '43' => %w{ 718 720 780 8.. 9.. }, # Austria
60
+ '44' => %w{800 808 842 843 844 845 870 871 872 873 900 908 909 982}, # UK
61
+ '45' => %w{10 11 18 70 80 90}, # Denmark
62
+ '46' => %w{ 900 939 944 99. },
63
+ '47' => %w{ 8.. }, # Norway
64
+ '48' => %w{70., 80.}, # Poland
65
+ '49' => %w{ 1. 164 168 169 180 181 19[0-4] 800 900 }, # Germany
66
+ '212' => %w{ 8. }, # Morocco
67
+ '213' => %w{ 8.. 9.. }, # Algeria
68
+ '233' => %w{ 1 4 6 7 8 9 },
69
+ '234' => %w{ 8.. 9.. },
70
+ '249' => %w{ }, # Sudan
71
+ '350' => %w{ 8... }, # Gibraltar
72
+ '351' => %w{ 800 80. }, # Portugal
73
+ '352' => %w{ 118 12 13 800 801 900 901 905 }, # Luxembourg
74
+ '353' => %w{ 76 80 81 800 }, # Ireland
75
+ '354' => %w{ 8.. 9.. }, # Iceland
76
+ '356' => %w{ }, # Malta
77
+ '357' => %w{ 80 90 }, # Cyprus
78
+ '358' => %w{ 6.. 7.. 8.. }, # Finland
79
+ '359' => %w{ 800 900 }, # Bulgaria
80
+ '370' => %w{ 800 9.. }, # Lithuania
81
+ '372' => %w{ 1.. 800 }, # Estonia
82
+ '377' => %w{ }, # Monaco
83
+ '386' => %w{ 80 89 90 }, # Slovenia
84
+ '420' => %w{ 8.. 9.. }, # Czech republic
85
+ '421' => %w{ }, # Slovakia
86
+ }
87
+
88
+ def initialize number
89
+ @number = number
90
+ end
91
+
92
+ def kind
93
+ case
94
+ when self.mobile? then
95
+ :mobile
96
+ when self.forbidden? then
97
+ :forbidden
98
+ else
99
+ :landline
100
+ end
101
+ end
102
+
103
+ def mobile?
104
+ return is_number_of_type @@mobile
105
+ end
106
+
107
+
108
+ def forbidden?
109
+ return is_number_of_type @@forbidden
110
+ end
111
+
112
+ def is_number_of_type numbers
113
+ parts = Phony.split Phony.normalize(@number)
114
+ forbidden_numbers = numbers[parts.shift]
115
+ prefix = parts.shift
116
+
117
+ # This is a hotfix for strange behaviour.
118
+ # I don't know why but some numbers are split ["45", false, "40", "53", "25", "77"]
119
+ until prefix.is_a? String
120
+ prefix = parts.shift
121
+ end
122
+
123
+ forbidden_numbers.each { |n| return true if prefix.match(/^#{n}$/) } unless forbidden_numbers.nil?
124
+ false
125
+ end
126
+
127
+ end
@@ -0,0 +1,297 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'spec_helper'
4
+
5
+
6
+ describe PhoneClassifier do
7
+
8
+ context "it classifies a phone number" do
9
+
10
+ let(:phone_number) { "41791234567" }
11
+
12
+ context "mobile numbers" do
13
+ it "correctly classifies a mobile number" do
14
+ PhoneClassifier.new(phone_number).kind.should == :mobile
15
+ end
16
+
17
+ it "correctly classifies a mobile number" do
18
+ PhoneClassifier.new(phone_number).mobile?.should == true
19
+ end
20
+
21
+
22
+ end
23
+
24
+ context "forbidden numbers" do
25
+ let(:phone_number) { "4180012345" }
26
+ it "correctly classifies a forbidden number" do
27
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
28
+ end
29
+
30
+ it "correctly classifies a forbidden number" do
31
+ PhoneClassifier.new(phone_number).forbidden?.should == true
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+
38
+ context "kinds of numbers" do
39
+
40
+
41
+ context "Belgium Numbers" do
42
+
43
+ it "should set BE mobile numbers" do
44
+ phone_number = "32497546747"
45
+ PhoneClassifier.new(phone_number).kind.should == :mobile
46
+ end
47
+
48
+ it "should set BE land numbers" do
49
+ phone_number = "326279584"
50
+ PhoneClassifier.new(phone_number).kind.should == :landline
51
+
52
+ end
53
+
54
+ end
55
+
56
+ context "Swiss Numbers" do
57
+
58
+ it "should set CH mobile numbers" do
59
+ phone_number = "41791234567"
60
+ PhoneClassifier.new(phone_number).kind.should == :mobile
61
+
62
+ end
63
+
64
+ it "should set CH land numbers" do
65
+ phone_number = "41444010930"
66
+ PhoneClassifier.new(phone_number).kind.should == :landline
67
+ end
68
+
69
+ end
70
+
71
+ context "German Numbers" do
72
+
73
+ it "should set DE mobile numbers" do
74
+ phone_number = "49170123456"
75
+ PhoneClassifier.new(phone_number).kind.should == :mobile
76
+ end
77
+
78
+ it "should set DE land numbers" do
79
+ phone_number = "4977317983830"
80
+ PhoneClassifier.new(phone_number).kind.should == :landline
81
+ end
82
+
83
+ it "should set DE service and paid numbers" do
84
+ phone_number = "491805878323"
85
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
86
+ end
87
+
88
+ end
89
+
90
+ context "Danish Numbers" do
91
+
92
+ it "should set DK mobile numbers" do
93
+ phone_number = "4540532577"
94
+ PhoneClassifier.new(phone_number).kind.should == :mobile
95
+ end
96
+
97
+ it "should set DK mobile numbers" do
98
+ phone_number = "4529727515"
99
+ PhoneClassifier.new(phone_number).kind.should == :mobile
100
+ end
101
+
102
+ it "should set DK forbidden numbers" do
103
+ phone_number = "4580727515"
104
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
105
+ end
106
+
107
+ it "should set DK land numbers" do
108
+ phone_number = "4545862577"
109
+ PhoneClassifier.new(phone_number).kind.should == :landline
110
+ end
111
+
112
+ end
113
+
114
+ context "French Numbers" do
115
+
116
+ it "should set FR forbidden numbers" do
117
+ phone_number = "33810459827"
118
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
119
+ end
120
+
121
+ it "should set FR mobile numbers (6)" do
122
+ phone_number = "33653832227"
123
+ PhoneClassifier.new(phone_number).kind.should == :mobile
124
+ end
125
+
126
+ it "should set FR mobile numbers (7)" do
127
+ phone_number = "33753832227"
128
+ PhoneClassifier.new(phone_number).kind.should == :mobile
129
+ end
130
+
131
+ it "should set FR land numbers" do
132
+ phone_number = "33953832227"
133
+ PhoneClassifier.new(phone_number).kind.should == :landline
134
+ end
135
+
136
+ end
137
+
138
+ context "Italian Numbers" do
139
+
140
+ it "should set IT mobile numbers" do
141
+ phone_number = "393357210499"
142
+ PhoneClassifier.new(phone_number).kind.should == :mobile
143
+ end
144
+
145
+ it "should set IT land numbers" do
146
+ phone_number = "390117210499"
147
+ PhoneClassifier.new(phone_number).kind.should == :landline
148
+ end
149
+
150
+ end
151
+
152
+ context "Netherlands Numbers" do
153
+
154
+ it "should set NL mobile numbers" do
155
+ phone_number = "31612345678"
156
+ PhoneClassifier.new(phone_number).kind.should == :mobile
157
+ end
158
+
159
+ it "should set NL land numbers" do
160
+ phone_number = "31201234567"
161
+ PhoneClassifier.new(phone_number).kind.should == :landline
162
+ end
163
+ it "should set NL forbidden numbers" do
164
+ phone_number = "318001234567"
165
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
166
+ end
167
+
168
+ end
169
+
170
+ context "Polish Numbers" do
171
+
172
+ it "should set PL mobile numbers" do
173
+ phone_number = "48501224455"
174
+ PhoneClassifier.new(phone_number).kind.should == :mobile
175
+ end
176
+
177
+ it "should set PL land numbers" do
178
+ phone_number = "48121123123"
179
+ PhoneClassifier.new(phone_number).kind.should == :landline
180
+ end
181
+
182
+ it "should set PL forbidden numbers" do
183
+ phone_number = "488001234567"
184
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
185
+ end
186
+
187
+ end
188
+
189
+ context "Spanish Numbers" do
190
+
191
+ it "should set ES mobile numbers" do
192
+ phone_number = "34 611 123 123"
193
+ PhoneClassifier.new(phone_number).kind.should == :mobile
194
+ end
195
+
196
+ it "should set ES forbidden numbers (800)" do
197
+ phone_number = "34800 123 123"
198
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
199
+ end
200
+
201
+ it "should set ES forbidden numbers (900)" do
202
+ phone_number = "34901 123 123"
203
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
204
+ end
205
+
206
+ it "should set ES land numbers" do
207
+ phone_number = "34822 123 123"
208
+ PhoneClassifier.new(phone_number).kind.should == :landline
209
+ end
210
+
211
+ end
212
+
213
+ context "UK Numbers" do
214
+
215
+ it "should set UK mobile numbers" do
216
+ phone_number = "447412121314"
217
+ PhoneClassifier.new(phone_number).kind.should == :mobile
218
+ end
219
+
220
+ it "should set UK mobile numbers" do
221
+ phone_number = "44778533097"
222
+ PhoneClassifier.new(phone_number).kind.should == :mobile
223
+ end
224
+
225
+ it "should set UK land numbers" do
226
+ phone_number = "442012121314"
227
+ PhoneClassifier.new(phone_number).kind.should == :landline
228
+ end
229
+
230
+ it "should set UK forbidden numbers" do
231
+ phone_number = "4480012121314"
232
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
233
+ end
234
+
235
+ end
236
+
237
+
238
+ context "Nigerian Numbers" do
239
+
240
+ it "should set Nigeria mobile numbers" do
241
+ phone_number = "2347021 555 5555"
242
+ PhoneClassifier.new(phone_number).kind.should == :mobile
243
+ end
244
+
245
+ it "should set Nigeria mobile numbers" do
246
+ phone_number = "234704 555 5555"
247
+ PhoneClassifier.new(phone_number).kind.should == :mobile
248
+ end
249
+
250
+ it "should set Nigeria mobile numbers (80x)" do
251
+ phone_number = "234804 555 5555"
252
+ PhoneClassifier.new(phone_number).kind.should == :mobile
253
+ end
254
+
255
+ it "should set Nigeria mobile numbers (81x)" do
256
+ phone_number = "234815 555 5555"
257
+ PhoneClassifier.new(phone_number).kind.should == :mobile
258
+ end
259
+
260
+ it "should set Nigeria land numbers" do
261
+ phone_number = "2341 555 5555"
262
+ PhoneClassifier.new(phone_number).kind.should == :landline
263
+ end
264
+
265
+ it "should set Nigeria forbidden numbers" do
266
+ phone_number = "234800 555 5555"
267
+ PhoneClassifier.new(phone_number).kind.should == :forbidden
268
+ end
269
+
270
+ end
271
+
272
+
273
+ context "Luxemborgian Numbers" do
274
+
275
+ it "should set Luxembourg mobile numbers" do
276
+ phone_number = "352661 555 555"
277
+ PhoneClassifier.new(phone_number).kind.should == :mobile
278
+ end
279
+
280
+ it "should set Luxemborgian land numbers" do
281
+ phone_number = "3522467 55 55 55"
282
+ PhoneClassifier.new(phone_number).kind.should == :landline
283
+ end
284
+
285
+ #it "should set Luxemborgian forbidden numbers" do
286
+ # phone_number = TelephoneNumber.make(:base_number => "800 5 55 55",
287
+ # :country_code => "352",
288
+ # :telephonable => @client)
289
+ # phone_number.valid?
290
+ # phone_number.kind.should == :forbidden
291
+ #end
292
+
293
+ end
294
+
295
+ end
296
+
297
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phone_classifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jens-Christian Fischer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: phony
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.8'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.8'
30
+ description: Classifies some countries phone numbers into :mobile, :forbidden and
31
+ :landline
32
+ email: jcf+phone_classifier@mobino.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files:
36
+ - README.md
37
+ files:
38
+ - lib/phone_classifier.rb
39
+ - README.md
40
+ - spec/phone_classifier_spec.rb
41
+ homepage: http://github.com/mobino/phone_classifier
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Classification of phone numbers
65
+ test_files:
66
+ - spec/phone_classifier_spec.rb