numero 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +48 -1
- data/lib/numero.rb +5 -4
- data/lib/numero/rails/validations.rb +2 -2
- data/test/numero_test.rb +6 -6
- metadata +2 -2
data/README.markdown
CHANGED
@@ -1,3 +1,50 @@
|
|
1
1
|
# Numero
|
2
2
|
|
3
|
-
This tiny gem contains helpers for validating
|
3
|
+
This tiny gem contains helpers for validating North American phone numbers.
|
4
|
+
|
5
|
+
Add it to your `Gemfile`, or install as a plugin.
|
6
|
+
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
Ruby:
|
11
|
+
|
12
|
+
Numero.valid_number?('15554442222') #=> true
|
13
|
+
Numero.valid_number?('194512') #=> false
|
14
|
+
|
15
|
+
|
16
|
+
Rails Validations:
|
17
|
+
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
include Numero::Rails::Validations
|
20
|
+
end
|
21
|
+
|
22
|
+
u = User.new(:number => '1234')
|
23
|
+
u.valid? #=> false
|
24
|
+
|
25
|
+
|
26
|
+
## The North American Numbering Plan
|
27
|
+
|
28
|
+
Numero validates phone numbers based on the
|
29
|
+
[North American Numbering Plan](http://en.wikipedia.org/wiki/North_American_Numbering_Plan).
|
30
|
+
|
31
|
+
The NANP breaks phone numbers into 4 components:
|
32
|
+
|
33
|
+
|A| B | C | D |
|
34
|
+
+1-555-444-2222
|
35
|
+
|
36
|
+
A. ITU country code
|
37
|
+
B. NPA - area code
|
38
|
+
C. NXX - Central Office code (exchange)
|
39
|
+
D. XXXX - Subscriber Number
|
40
|
+
|
41
|
+
Each section allows for a specific set of numbers:
|
42
|
+
|
43
|
+
* **ITU**: This is always `+1` for North American numbers, although, it is
|
44
|
+
most often written without the `+`.
|
45
|
+
* **NPA**: First digit must be in the range `[2-9]`, second and third digit
|
46
|
+
can be any number `[0-9]`.
|
47
|
+
* **NXX**: First digit must be in the range `[2-9]`, second and third digit
|
48
|
+
can be any number `[0-9]`.
|
49
|
+
* **XXXX**: [0-9] for each digit
|
50
|
+
|
data/lib/numero.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module Numero
|
2
|
-
VERSION = Version = '0.0.
|
2
|
+
VERSION = Version = '0.0.2'
|
3
3
|
|
4
|
-
|
4
|
+
# See http://en.wikipedia.org/wiki/North_American_Numbering_Plan
|
5
|
+
VALID_PHONE_NUMBER = /^1?([2-9][0-9]{2})([2-9][0-9]{2})([0-9]{4})$/
|
5
6
|
|
6
|
-
def self.
|
7
|
-
!! number.to_s.match(
|
7
|
+
def self.valid_number?(number)
|
8
|
+
!! number.to_s.match(VALID_PHONE_NUMBER)
|
8
9
|
end
|
9
10
|
end
|
@@ -8,14 +8,14 @@ module Numero
|
|
8
8
|
if Numero::Rails.major < 3
|
9
9
|
validates_presence_of :number
|
10
10
|
validates_numericality_of :number, :allow_blank => true
|
11
|
-
validates_format_of :number, :with => Numero::
|
11
|
+
validates_format_of :number, :with => Numero::VALID_PHONE_NUMBER
|
12
12
|
|
13
13
|
# Rails 3
|
14
14
|
else
|
15
15
|
validates :number,
|
16
16
|
:presence => true,
|
17
17
|
:numericality => { :allow_blank => true },
|
18
|
-
:format => { :with => Numero::
|
18
|
+
:format => { :with => Numero::VALID_PHONE_NUMBER }
|
19
19
|
|
20
20
|
end
|
21
21
|
end
|
data/test/numero_test.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
3
|
context 'Numero' do
|
4
|
-
test 'validates a
|
4
|
+
test 'validates a North American phone number' do
|
5
5
|
valid_number = 15185554488
|
6
|
-
valid_number10 =
|
7
|
-
invalid_number =
|
6
|
+
valid_number10 = 5185554488
|
7
|
+
invalid_number = 11456581535
|
8
8
|
|
9
|
-
assert Numero.
|
10
|
-
assert Numero.
|
11
|
-
assert ! Numero.
|
9
|
+
assert Numero.valid_number?(valid_number)
|
10
|
+
assert Numero.valid_number?(valid_number10)
|
11
|
+
assert ! Numero.valid_number?(invalid_number)
|
12
12
|
end
|
13
13
|
end
|