number_recognizer 1.0.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.
- data/MIT-LICENSE +20 -0
- data/Readme.md +60 -0
- data/lib/number_recognizer.rb +125 -0
- data/lib/number_recognizer/format_dsl.rb +18 -0
- data/number_recognizer.gemspec +30 -0
- data/spec/number_recognizer_spec.rb +340 -0
- data/spec/spec_helper.rb +3 -0
- metadata +60 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Wes Oldenbeuving
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Readme.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
NumberRecognizer
|
2
|
+
================
|
3
|
+
|
4
|
+
NumberRecognizer is library to recognize mobile phone numbers. It can make educated guesses to correct local numbers into numbers in international format.
|
5
|
+
|
6
|
+
Examples
|
7
|
+
--------
|
8
|
+
|
9
|
+
require "number_recognizer"
|
10
|
+
@nc = NumberRecognizer.new("0612345678")
|
11
|
+
@nc.valid_or_correct_mobile? # => true
|
12
|
+
@nc.country_name # => "Netherlands"
|
13
|
+
@nc.country # => "31"
|
14
|
+
@nc.prefix # => "31"
|
15
|
+
@nc.local_number # => "612345678"
|
16
|
+
@nc.normalized_number # => "31612345678"
|
17
|
+
|
18
|
+
Supported countries
|
19
|
+
-------------------
|
20
|
+
|
21
|
+
It does not aim for worldwide coverage and correction, as that is not the problem I'm trying to solve.
|
22
|
+
If you want to add a country, feel free to send me a pull request or create a feature request.
|
23
|
+
|
24
|
+
* Australia (614xxxxxxxx)
|
25
|
+
* Belgium (324xxxxxxxx)
|
26
|
+
* England (447xxxxxxxx, 447xxxxxxxxx)
|
27
|
+
* Germany (491xxxxxxxxx, 491xxxxxxxxxx)
|
28
|
+
* Netherlands (316xxxxxxxx)
|
29
|
+
* Portugal (3519xxxxxxxx)
|
30
|
+
* Spain (346xxxxxxxx, 347xxxxxxxx)
|
31
|
+
|
32
|
+
Some countries have some number ranges not covered. The regular expressions cover those, so check [lib/number_recognizer.rb](http://github.com/Narnach/number_recognizer/blob/master/lib/number_recognizer.rb) for the details.
|
33
|
+
|
34
|
+
It is also possible to add your own custom format:
|
35
|
+
|
36
|
+
NumberRecognizer.add_format :country => "Utopia", :mobile=>true, :format => /(99)(9\d{8})/, :country_code=>999
|
37
|
+
|
38
|
+
Installation
|
39
|
+
------------
|
40
|
+
|
41
|
+
Simply install the gem from rubygems:
|
42
|
+
|
43
|
+
gem install number_recognizer
|
44
|
+
|
45
|
+
If you use Bundler, add it to your Gemfile and the do a bundle install:
|
46
|
+
|
47
|
+
gem "number_recognizer"
|
48
|
+
|
49
|
+
Bug tracking / feature requests
|
50
|
+
-------------------------------
|
51
|
+
|
52
|
+
I use Github issues for this: https://github.com/Narnach/number_recognizer/issues
|
53
|
+
|
54
|
+
Disclaimer
|
55
|
+
----------
|
56
|
+
|
57
|
+
It scratches my own itch and does not aim to solve everyone's problems.
|
58
|
+
If it works for you, great! If it does not; fork it, patch it and send me a pull request.
|
59
|
+
|
60
|
+
Written by Wes 'Narnach' Oldenbeuving in 2009. Licensed under the MIT license.
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require_relative "number_recognizer/format_dsl"
|
3
|
+
|
4
|
+
class NumberRecognizer
|
5
|
+
include FormatDsl
|
6
|
+
|
7
|
+
# Input
|
8
|
+
attr_accessor :number, :old_number, :country_name, :country, :local_number, :prefix
|
9
|
+
|
10
|
+
add_format :country => "Netherlands", :mobile=>true, :format => /(31)(6\d{8})/
|
11
|
+
add_format :country => "Belgium", :mobile=>true, :format => /(32)(4\d{8})/
|
12
|
+
add_format :country => "Germany", :mobile=>true, :format => /(49)(1([567])\d{8,9})/ # http://en.wikipedia.org/wiki/Phone_numbers_in_Germany
|
13
|
+
add_format :country => "England", :mobile=>true, :format => /(44)(7\d{8,9})/ # http://en.wikipedia.org/wiki/Telephone_numbers_in_England
|
14
|
+
add_format :country => "Australia", :mobile=>true, :format => /(61)(4\d{8})/ # http://en.wikipedia.org/wiki/Telephone_numbers_in_Australia
|
15
|
+
add_format :country => "Portugal", :mobile=>true, :format => /(351)(9\d{8})/
|
16
|
+
add_format :country => "Spain", :mobile=>true, :format => /(34)((6\d|7[1-9])\d{7})/ # http://en.wikipedia.org/wiki/Telephone_numbers_in_Spain
|
17
|
+
|
18
|
+
add_format :country => "Netherlands", :mobile=>false, :format => /(31)([123457890]\d{8})/
|
19
|
+
add_format :country => "Germany", :mobile=>false, :format => /(49)(\d([0123489])\d{8,9})/
|
20
|
+
add_format :country => "Suriname", :mobile=>false, :format => /(597)(\d{7,7})/
|
21
|
+
add_format :country => "Dutch Antilles", :mobile=>false, :format => /(599)(\d{7,7})/
|
22
|
+
add_format :country => "England", :mobile=>false, :format => /(44)([0-68-9]\d{8,9})/
|
23
|
+
add_format :country => "Australia", :mobile=>false, :format => /(61)([1-35-9]\d{8})/
|
24
|
+
|
25
|
+
def initialize(number)
|
26
|
+
@number = number
|
27
|
+
@parsed = false
|
28
|
+
@corrected = false
|
29
|
+
|
30
|
+
@valid = nil
|
31
|
+
@mobile = nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def mobile?
|
35
|
+
parse unless @parsed
|
36
|
+
@mobile
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid?
|
40
|
+
parse unless @parsed
|
41
|
+
@valid
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_or_correct_mobile?(country_bias=nil)
|
45
|
+
correct(country_bias)
|
46
|
+
valid? && mobile?
|
47
|
+
end
|
48
|
+
|
49
|
+
def normalized_number
|
50
|
+
parse unless @parsed
|
51
|
+
"#{prefix}#{local_number}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def correct(country_bias=nil)
|
55
|
+
@corrected = false
|
56
|
+
old_number = number
|
57
|
+
case number
|
58
|
+
when /^0?9([136]\d{7})$/ #this must come before NL !
|
59
|
+
self.number = "3519#{$1}"
|
60
|
+
when /^0?7([1-9]\d{7})$/ # 07 can't be followed by a 0 in spain. It's legal in England.
|
61
|
+
prefix = pick_biased_country([44,34], country_bias)
|
62
|
+
self.number = "#{prefix}7#{$1}"
|
63
|
+
when /^0?7(0\d{7})$/ # 07 can't be followed by a 0 in spain. It's legal in England.
|
64
|
+
prefix = pick_biased_country([44], country_bias)
|
65
|
+
self.number = "#{prefix}7#{$1}"
|
66
|
+
when /^0?7(\d{9})$/ # Spanish numbers have 8 digits after the 07. English numbers can also have 9 digits after the 07.
|
67
|
+
prefix = pick_biased_country([44], country_bias)
|
68
|
+
self.number = "#{prefix}7#{$1}"
|
69
|
+
when /^0?6+(\d{8})$/
|
70
|
+
prefix = pick_biased_country([31,34], country_bias)
|
71
|
+
self.number = "#{prefix}6#{$1}"
|
72
|
+
when /^0?4(\d{8})$/
|
73
|
+
prefix = pick_biased_country([32,61], country_bias)
|
74
|
+
self.number = "#{prefix}4#{$1}"
|
75
|
+
else
|
76
|
+
# No correction, so no need re-parse
|
77
|
+
@corrected = true
|
78
|
+
return valid?
|
79
|
+
end
|
80
|
+
self.old_number = old_number
|
81
|
+
@parsed = false
|
82
|
+
@corrected = true
|
83
|
+
valid?
|
84
|
+
end
|
85
|
+
|
86
|
+
def type
|
87
|
+
return nil unless valid?
|
88
|
+
"#{country_name} #{mobile? ? "mobile" : "landline"}"
|
89
|
+
end
|
90
|
+
|
91
|
+
def country=(country)
|
92
|
+
@country = country.to_s
|
93
|
+
end
|
94
|
+
|
95
|
+
def prefix=(prefix)
|
96
|
+
@prefix = prefix.to_s
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def parse
|
102
|
+
@parsed = true
|
103
|
+
@valid = false
|
104
|
+
number = self.number.to_s.sub(/^0+/,'')
|
105
|
+
format = self.class.formats.find {|format| number.match(format[:format])}
|
106
|
+
unless format
|
107
|
+
return
|
108
|
+
end
|
109
|
+
@mobile = format[:mobile]
|
110
|
+
self.country_name = format[:country]
|
111
|
+
|
112
|
+
match = number.match(format[:format])
|
113
|
+
self.prefix = match[1]
|
114
|
+
self.country = format[:country_code] || self.prefix
|
115
|
+
self.local_number = match[2]
|
116
|
+
@valid = true
|
117
|
+
nil
|
118
|
+
end
|
119
|
+
|
120
|
+
def pick_biased_country(allowed_countries, bias=nil)
|
121
|
+
country_bias = [bias].flatten.compact
|
122
|
+
whitelisted_bias = country_bias & allowed_countries
|
123
|
+
return whitelisted_bias.first || allowed_countries.first
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class NumberRecognizer
|
3
|
+
module FormatDsl
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def add_format(options={})
|
10
|
+
formats << {:country=>options[:country], :mobile=>options[:mobile], :format=>options[:format], :country_code=>options[:country_code]}
|
11
|
+
end
|
12
|
+
|
13
|
+
def formats
|
14
|
+
@formats ||= []
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
# Project
|
3
|
+
s.name = 'number_recognizer'
|
4
|
+
s.summary = "NumberRecognizer is library to recognize mobile phone numbers. It can make educated guesses to correct local numbers into numbers in international format."
|
5
|
+
s.description = s.summary
|
6
|
+
s.version = '1.0.0'
|
7
|
+
s.date = '2012-03-27'
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Wes Oldenbeuving"]
|
10
|
+
s.email = "narnach@me.com"
|
11
|
+
s.homepage = "http://www.github.com/narnach/number_recognizer"
|
12
|
+
|
13
|
+
# Files
|
14
|
+
root_files = %w[Readme.md number_recognizer.gemspec MIT-LICENSE]
|
15
|
+
bin_files = []
|
16
|
+
lib_files = %w[number_recognizer number_recognizer/format_dsl]
|
17
|
+
s.bindir = "bin"
|
18
|
+
s.require_path = "lib"
|
19
|
+
s.executables = bin_files
|
20
|
+
s.test_files = %w[number_recognizer].map{|f| "spec/#{f}_spec.rb"}
|
21
|
+
s.files = root_files + s.test_files + bin_files.map {|f| 'bin/%s' % f} + lib_files.map {|f| 'lib/%s.rb' % f} + %w[spec/spec_helper.rb]
|
22
|
+
|
23
|
+
# rdoc
|
24
|
+
s.has_rdoc = true
|
25
|
+
s.extra_rdoc_files = %w[ Readme.md]
|
26
|
+
s.rdoc_options << '--inline-source' << '--line-numbers' << '--main' << 'Readme.md'
|
27
|
+
|
28
|
+
# Requirements
|
29
|
+
s.required_ruby_version = ">= 1.8.0"
|
30
|
+
end
|
@@ -0,0 +1,340 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require File.join(File.dirname(__FILE__), %w[spec_helper])
|
3
|
+
require 'number_recognizer'
|
4
|
+
|
5
|
+
describe NumberRecognizer do
|
6
|
+
it 'should normalize numbers' do
|
7
|
+
@nc = NumberRecognizer.new('0031612345678')
|
8
|
+
@nc.valid?
|
9
|
+
@nc.country.should == '31'
|
10
|
+
@nc.local_number.should == '612345678'
|
11
|
+
@nc.normalized_number.should == '31612345678'
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'valid?' do
|
15
|
+
it 'should be false when a nil number is provided' do
|
16
|
+
@nc = NumberRecognizer.new(nil)
|
17
|
+
@nc.should_not be_valid
|
18
|
+
@nc.country_name.should be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should recognize 0031612345678' do
|
22
|
+
@nc = NumberRecognizer.new('0031612345678')
|
23
|
+
@nc.should be_valid
|
24
|
+
@nc.country.should == '31'
|
25
|
+
@nc.local_number.should == '612345678'
|
26
|
+
@nc.country_name.should == 'Netherlands'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should recognize 0031201234567' do
|
30
|
+
@nc = NumberRecognizer.new('0031201234567')
|
31
|
+
@nc.should be_valid
|
32
|
+
@nc.country.should == '31'
|
33
|
+
@nc.local_number.should == '201234567'
|
34
|
+
@nc.country_name.should == 'Netherlands'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should recognize 0032412345678' do
|
38
|
+
@nc = NumberRecognizer.new('0032412345678')
|
39
|
+
@nc.should be_valid
|
40
|
+
@nc.country.should == '32'
|
41
|
+
@nc.local_number.should == '412345678'
|
42
|
+
@nc.country_name.should == 'Belgium'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should recognize 005971234567' do
|
46
|
+
@nc = NumberRecognizer.new('005971234567')
|
47
|
+
@nc.should be_valid
|
48
|
+
@nc.country.should == '597'
|
49
|
+
@nc.local_number.should == '1234567'
|
50
|
+
@nc.country_name.should == 'Suriname'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should recognize 005991234567' do
|
54
|
+
@nc = NumberRecognizer.new('005991234567')
|
55
|
+
@nc.should be_valid
|
56
|
+
@nc.country.should == '599'
|
57
|
+
@nc.local_number.should == '1234567'
|
58
|
+
@nc.country_name.should == 'Dutch Antilles'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should recognize 00441234567890' do
|
62
|
+
@nc = NumberRecognizer.new('00441234567890')
|
63
|
+
@nc.should be_valid
|
64
|
+
@nc.country.should == '44'
|
65
|
+
@nc.local_number.should == '1234567890'
|
66
|
+
@nc.country_name.should == 'England'
|
67
|
+
@nc.should_not be_mobile
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should recognize 0044123456789' do
|
71
|
+
@nc = NumberRecognizer.new('0044123456789')
|
72
|
+
@nc.should be_valid
|
73
|
+
@nc.country.should == '44'
|
74
|
+
@nc.local_number.should == '123456789'
|
75
|
+
@nc.country_name.should == 'England'
|
76
|
+
@nc.should_not be_mobile
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should recognize 0044712345678' do
|
80
|
+
@nc = NumberRecognizer.new('0044712345678')
|
81
|
+
@nc.should be_valid
|
82
|
+
@nc.should be_valid_or_correct_mobile
|
83
|
+
@nc.country.should == '44'
|
84
|
+
@nc.local_number.should == '712345678'
|
85
|
+
@nc.country_name.should == 'England'
|
86
|
+
@nc.should be_mobile
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should recognize 00447123456789' do
|
90
|
+
@nc = NumberRecognizer.new('00447123456789')
|
91
|
+
@nc.should be_valid
|
92
|
+
@nc.should be_valid_or_correct_mobile
|
93
|
+
@nc.country.should == '44'
|
94
|
+
@nc.local_number.should == '7123456789'
|
95
|
+
@nc.country_name.should == 'England'
|
96
|
+
@nc.should be_mobile
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should recognize 0061451124205' do
|
100
|
+
@nc = NumberRecognizer.new('0061451124205')
|
101
|
+
@nc.should be_valid
|
102
|
+
@nc.country.should == '61'
|
103
|
+
@nc.local_number.should == '451124205'
|
104
|
+
@nc.country_name.should == 'Australia'
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should recognize 00351927123456' do
|
108
|
+
@nc = NumberRecognizer.new('00351927123456')
|
109
|
+
@nc.should be_valid
|
110
|
+
@nc.country.should == '351'
|
111
|
+
@nc.local_number.should == '927123456'
|
112
|
+
@nc.country_name.should == 'Portugal'
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should recognize 0034612345678' do
|
116
|
+
@nc = NumberRecognizer.new('0034612345678')
|
117
|
+
@nc.should be_valid
|
118
|
+
@nc.country.should == '34'
|
119
|
+
@nc.should be_mobile
|
120
|
+
@nc.local_number.should == '612345678'
|
121
|
+
@nc.country_name.should == 'Spain'
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should recognize 0034712345678' do
|
125
|
+
@nc = NumberRecognizer.new('0034712345678')
|
126
|
+
@nc.should be_valid
|
127
|
+
@nc.country.should == '34'
|
128
|
+
@nc.should be_mobile
|
129
|
+
@nc.local_number.should == '712345678'
|
130
|
+
@nc.country_name.should == 'Spain'
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should recognize 004912345678' do
|
134
|
+
@nc = NumberRecognizer.new('00491512345678')
|
135
|
+
@nc.should be_valid
|
136
|
+
@nc.country.should == '49'
|
137
|
+
@nc.should be_mobile
|
138
|
+
@nc.local_number.should == '1512345678'
|
139
|
+
@nc.country_name.should == 'Germany'
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should recognize 0049123456789' do
|
143
|
+
@nc = NumberRecognizer.new('004916123456789')
|
144
|
+
@nc.should be_valid
|
145
|
+
@nc.country.should == '49'
|
146
|
+
@nc.should be_mobile
|
147
|
+
@nc.local_number.should == '16123456789'
|
148
|
+
@nc.country_name.should == 'Germany'
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'should NOT recognize 0034702345678 as a Spanish number' do
|
152
|
+
@nc = NumberRecognizer.new('0034702345678')
|
153
|
+
@nc.should_not be_valid_or_correct_mobile(34)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "correct" do
|
158
|
+
it 'should correct 06612345678 to 0031612345678' do
|
159
|
+
@nc = NumberRecognizer.new('06612345678')
|
160
|
+
@nc.should_not be_valid
|
161
|
+
|
162
|
+
@nc.correct.should be_true
|
163
|
+
@nc.number.should == '31612345678'
|
164
|
+
@nc.old_number.should == '06612345678'
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'should correct 0612345678 to 0031612345678' do
|
168
|
+
@nc = NumberRecognizer.new('0612345678')
|
169
|
+
@nc.should_not be_valid
|
170
|
+
|
171
|
+
@nc.correct.should be_true
|
172
|
+
@nc.number.should == '31612345678'
|
173
|
+
@nc.old_number.should == '0612345678'
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'should correct 07123456789 to 00447123456789' do
|
177
|
+
@nc = NumberRecognizer.new('07123456789')
|
178
|
+
@nc.should_not be_valid
|
179
|
+
|
180
|
+
@nc.correct.should be_true
|
181
|
+
@nc.number.should == '447123456789'
|
182
|
+
@nc.old_number.should == '07123456789'
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'should correct 7123456789 to 00447123456789' do
|
186
|
+
@nc = NumberRecognizer.new('7123456789')
|
187
|
+
@nc.should_not be_valid
|
188
|
+
|
189
|
+
@nc.correct.should be_true
|
190
|
+
@nc.number.should == '447123456789'
|
191
|
+
@nc.old_number.should == '7123456789'
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'should correct 0412345678 to 32412345678' do
|
195
|
+
@nc = NumberRecognizer.new('0412345678')
|
196
|
+
@nc.should_not be_valid
|
197
|
+
|
198
|
+
@nc.correct.should be_true
|
199
|
+
@nc.number.should == '32412345678'
|
200
|
+
@nc.old_number.should == '0412345678'
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'should correct 0412345678 to 61412345678 given a country-bias for 61' do
|
204
|
+
@nc = NumberRecognizer.new('0412345678')
|
205
|
+
@nc.should_not be_valid
|
206
|
+
|
207
|
+
@nc.correct(61).should be_true
|
208
|
+
@nc.number.should == '61412345678'
|
209
|
+
@nc.old_number.should == '0412345678'
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'should correct 0612345678 to 34612345678 given a country-bias for 34' do
|
213
|
+
@nc = NumberRecognizer.new('0612345678')
|
214
|
+
@nc.should_not be_valid
|
215
|
+
|
216
|
+
@nc.correct(34).should be_true
|
217
|
+
@nc.number.should == '34612345678'
|
218
|
+
@nc.old_number.should == '0612345678'
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'should correct 0712345678 to 34712345678 given a country-bias for 34' do
|
222
|
+
@nc = NumberRecognizer.new('0712345678')
|
223
|
+
@nc.should_not be_valid
|
224
|
+
|
225
|
+
@nc.correct(34).should be_true
|
226
|
+
@nc.number.should == '34712345678'
|
227
|
+
@nc.old_number.should == '0712345678'
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
describe 'valid or correct mobile' do
|
232
|
+
it 'should correct 0612345678 to 0031612345678' do
|
233
|
+
@nc = NumberRecognizer.new('0612345678')
|
234
|
+
@nc.should be_valid_or_correct_mobile
|
235
|
+
|
236
|
+
@nc.country_name.should == 'Netherlands'
|
237
|
+
@nc.country.should == '31'
|
238
|
+
@nc.local_number.should == '612345678'
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'should correct 612345678 to 0031612345678' do
|
242
|
+
@nc = NumberRecognizer.new('612345678')
|
243
|
+
@nc.should be_valid_or_correct_mobile
|
244
|
+
|
245
|
+
@nc.country_name.should == 'Netherlands'
|
246
|
+
@nc.country.should == '31'
|
247
|
+
@nc.local_number.should == '612345678'
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'should accept 0032412345678' do
|
251
|
+
@nc = NumberRecognizer.new('0032412345678')
|
252
|
+
@nc.should be_valid_or_correct_mobile
|
253
|
+
|
254
|
+
@nc.country_name.should == 'Belgium'
|
255
|
+
@nc.country.should == '32'
|
256
|
+
@nc.local_number.should == '412345678'
|
257
|
+
end
|
258
|
+
|
259
|
+
it 'should accept 0412345678 as Australia given a country-bias for 61' do
|
260
|
+
@nc = NumberRecognizer.new('0412345678')
|
261
|
+
@nc.valid_or_correct_mobile?(61).should be_true
|
262
|
+
|
263
|
+
@nc.country_name.should == 'Australia'
|
264
|
+
@nc.country.should == '61'
|
265
|
+
@nc.local_number.should == '412345678'
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'should accept 412345678 as Australia given a country-bias for 61' do
|
269
|
+
@nc = NumberRecognizer.new('412345678')
|
270
|
+
@nc.valid_or_correct_mobile?(61).should be_true
|
271
|
+
|
272
|
+
@nc.country_name.should == 'Australia'
|
273
|
+
@nc.country.should == '61'
|
274
|
+
@nc.local_number.should == '412345678'
|
275
|
+
end
|
276
|
+
|
277
|
+
it 'should correct 0913773785 to 00351913773785' do
|
278
|
+
@nc = NumberRecognizer.new('0913773785')
|
279
|
+
@nc.should be_valid_or_correct_mobile
|
280
|
+
|
281
|
+
@nc.country_name.should == 'Portugal'
|
282
|
+
@nc.country.should == '351'
|
283
|
+
@nc.local_number.should == '913773785'
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'should correct 913773785 to 00351913773785' do
|
287
|
+
@nc = NumberRecognizer.new('913773785')
|
288
|
+
@nc.should be_valid_or_correct_mobile
|
289
|
+
|
290
|
+
@nc.country_name.should == 'Portugal'
|
291
|
+
@nc.country.should == '351'
|
292
|
+
@nc.local_number.should == '913773785'
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should return false for a landline number" do
|
296
|
+
@nc = NumberRecognizer.new('31201234567')
|
297
|
+
@nc.should_not be_valid_or_correct_mobile
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should return false for a corrupted mobile number" do
|
301
|
+
@nc = NumberRecognizer.new('061234567')
|
302
|
+
@nc.should_not be_valid_or_correct_mobile
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
describe 'mobile?' do
|
307
|
+
it 'should recognize 31612345678 as a mobile number' do
|
308
|
+
@nc = NumberRecognizer.new('0031612345678')
|
309
|
+
@nc.should be_mobile
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'should recognize 31201234567 as not mobile number' do
|
313
|
+
@nc = NumberRecognizer.new('31201234567')
|
314
|
+
@nc.should_not be_mobile
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
describe "type" do
|
319
|
+
it "should compose country and mobile/landline status" do
|
320
|
+
@nc = NumberRecognizer.new('31612345678')
|
321
|
+
@nc.type.should == "Netherlands mobile"
|
322
|
+
@nc = NumberRecognizer.new('31201234567')
|
323
|
+
@nc.type.should == "Netherlands landline"
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
describe "custom format" do
|
328
|
+
it 'should use the custom country' do
|
329
|
+
NumberRecognizer.add_format :country => "Utopia", :mobile=>true, :format => /(999)(\d{8})/, :country_code=>99
|
330
|
+
@nc = NumberRecognizer.new('0099912345678')
|
331
|
+
@nc.should be_valid
|
332
|
+
@nc.should be_mobile
|
333
|
+
@nc.country.should == '99'
|
334
|
+
@nc.prefix.should == '999'
|
335
|
+
@nc.local_number.should == '12345678'
|
336
|
+
@nc.country_name.should == 'Utopia'
|
337
|
+
@nc.normalized_number.should == "99912345678"
|
338
|
+
end
|
339
|
+
end
|
340
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: number_recognizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Wes Oldenbeuving
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: NumberRecognizer is library to recognize mobile phone numbers. It can
|
15
|
+
make educated guesses to correct local numbers into numbers in international format.
|
16
|
+
email: narnach@me.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- Readme.md
|
21
|
+
files:
|
22
|
+
- Readme.md
|
23
|
+
- number_recognizer.gemspec
|
24
|
+
- MIT-LICENSE
|
25
|
+
- spec/number_recognizer_spec.rb
|
26
|
+
- lib/number_recognizer.rb
|
27
|
+
- lib/number_recognizer/format_dsl.rb
|
28
|
+
- spec/spec_helper.rb
|
29
|
+
homepage: http://www.github.com/narnach/number_recognizer
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --inline-source
|
34
|
+
- --line-numbers
|
35
|
+
- --main
|
36
|
+
- Readme.md
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.8.0
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.11
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: NumberRecognizer is library to recognize mobile phone numbers. It can make
|
57
|
+
educated guesses to correct local numbers into numbers in international format.
|
58
|
+
test_files:
|
59
|
+
- spec/number_recognizer_spec.rb
|
60
|
+
has_rdoc: true
|