uk_phone_numbers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +12 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/lib/uk_phone_numbers/version.rb +3 -0
- data/lib/uk_phone_numbers.rb +54 -0
- data/spec/uk_phone_numbers_spec.rb +71 -0
- data/uk_phone_numbers.gemspec +15 -0
- metadata +65 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 GoCardless
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# uk_phone_numbers
|
2
|
+
|
3
|
+
A Ruby library for validating and formatting UK phone numbers.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```console
|
8
|
+
$ gem install uk_phone_numbers
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Validating a phone number
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require 'uk_phone_numbers'
|
17
|
+
|
18
|
+
UKPhoneNumbers.valid? "02087123456"
|
19
|
+
# => true
|
20
|
+
```
|
21
|
+
|
22
|
+
### Formatting a phone number
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'uk_phone_numbers'
|
26
|
+
|
27
|
+
UKPhoneNumbers.format "02087123456"
|
28
|
+
# => "020 8712 3456"
|
29
|
+
```
|
30
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module UKPhoneNumbers
|
2
|
+
# http://www.area-codes.org.uk/formatting.shtml#programmers
|
3
|
+
PATTERNS = <<-PATTERNS
|
4
|
+
(01###) #####[#]
|
5
|
+
(011#) ### ####
|
6
|
+
(01#1) ### ####
|
7
|
+
(013873) #####
|
8
|
+
(015242) #####
|
9
|
+
(015394) #####
|
10
|
+
(015395) #####
|
11
|
+
(015396) #####
|
12
|
+
(016973) #####
|
13
|
+
(016974) #####
|
14
|
+
(016977) ####[#]
|
15
|
+
(017683) #####
|
16
|
+
(017684) #####
|
17
|
+
(017687) #####
|
18
|
+
(019467) #####
|
19
|
+
(02#) #### ####
|
20
|
+
03## ### ####
|
21
|
+
05### ######
|
22
|
+
0500 ######
|
23
|
+
07### ######
|
24
|
+
08## ### ###[#]
|
25
|
+
09## ### ####
|
26
|
+
PATTERNS
|
27
|
+
|
28
|
+
def self.pattern_to_regexp(pattern)
|
29
|
+
regexp = pattern.dup
|
30
|
+
regexp.gsub!(/[()]/, '')
|
31
|
+
regexp = regexp.split.map { |p| "(#{p})" }.join
|
32
|
+
regexp.gsub!(/\[([^\]]*)\]/, '(?:\1)?')
|
33
|
+
regexp.gsub!(/#/, '\d')
|
34
|
+
Regexp.new("^#{regexp}$")
|
35
|
+
end
|
36
|
+
|
37
|
+
REGEXPS = []
|
38
|
+
# Convert each of the patterns to a regexp
|
39
|
+
PATTERNS.split("\n").map(&:strip).each do |pattern|
|
40
|
+
REGEXPS << pattern_to_regexp(pattern)
|
41
|
+
end
|
42
|
+
# Reverse-sort the regexps by how many digits they contain (their specificity)
|
43
|
+
REGEXPS.sort_by! { |r| -r.source.scan(/\d/).length }
|
44
|
+
|
45
|
+
def self.valid?(number)
|
46
|
+
REGEXPS.select { |r| r.match(number) }.any?
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.format(number, opts = {})
|
50
|
+
match = REGEXPS.map { |r| r.match(number) }.reject(&:nil?).first
|
51
|
+
match ? match[1..-1].join(opts[:separator] || ' ') : false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'uk_phone_numbers'
|
2
|
+
|
3
|
+
describe UKPhoneNumbers do
|
4
|
+
describe "::REGEXPS" do
|
5
|
+
subject { UKPhoneNumbers::REGEXPS }
|
6
|
+
it { should have(UKPhoneNumbers::PATTERNS.split("\n").length).items }
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ".pattern_to_regexp" do
|
10
|
+
def regexp_for(pattern)
|
11
|
+
subject.pattern_to_regexp(pattern).source
|
12
|
+
end
|
13
|
+
|
14
|
+
it "given a String pattern returns a Regexp" do
|
15
|
+
subject.pattern_to_regexp("0#").should be_a Regexp
|
16
|
+
end
|
17
|
+
|
18
|
+
it "strips parens from the pattern" do
|
19
|
+
regexp_for("()").should == "^$"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "parenthesises whitespace-separated sections" do
|
23
|
+
regexp_for("0 12 345").should == "^(0)(12)(345)$"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "replaces # with \\d" do
|
27
|
+
regexp_for("0#").should == "^(0\\d)$"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "replaces bracketed expressions with optional non-capturing groups" do
|
31
|
+
regexp_for("0[12]3[4]5").should == "^(0(?:12)?3(?:4)?5)$"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "handles hashes inside brackets" do
|
35
|
+
regexp_for("0[#]1[#]2").should == "^(0(?:\\d)?1(?:\\d)?2)$"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe ".valid?" do
|
40
|
+
it "returns true for matching numbers" do
|
41
|
+
subject.valid?('08456123123').should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns false for invalid numbers" do
|
45
|
+
subject.valid?('08456123123123').should be_false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe ".format" do
|
50
|
+
it "returns false for invalid numbers" do
|
51
|
+
subject.format('08456123123123').should be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
it "formats long-prefix numbers correctly" do
|
55
|
+
subject.format('01946700000').should == '019467 00000'
|
56
|
+
end
|
57
|
+
|
58
|
+
it "formats mobile numbers correctly" do
|
59
|
+
subject.format('07900000000').should == '07900 000000'
|
60
|
+
end
|
61
|
+
|
62
|
+
it "formats 08xx numbers correctly" do
|
63
|
+
subject.format('08456123123').should == '0845 612 3123'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "uses a custom separator when provided" do
|
67
|
+
subject.format('08456123123', separator: '-').should == '0845-612-3123'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path('../lib/uk_phone_numbers/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'uk_phone_numbers'
|
5
|
+
gem.version = UKPhoneNumbers::VERSION.dup
|
6
|
+
gem.authors = ['Harry Marr']
|
7
|
+
gem.email = ['harry@gocardless.com']
|
8
|
+
gem.summary = 'A Ruby library for parsing and formatting UK phone numbers.'
|
9
|
+
gem.homepage = 'https://github.com/gocardless/uk_phone_numbers'
|
10
|
+
|
11
|
+
gem.add_development_dependency 'rspec', '~> 2.6'
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split("\n")
|
14
|
+
gem.test_files = `git ls-files -- spec/*`.split("\n")
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uk_phone_numbers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Harry Marr
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70215946414560 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.6'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70215946414560
|
25
|
+
description:
|
26
|
+
email:
|
27
|
+
- harry@gocardless.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- lib/uk_phone_numbers.rb
|
37
|
+
- lib/uk_phone_numbers/version.rb
|
38
|
+
- spec/uk_phone_numbers_spec.rb
|
39
|
+
- uk_phone_numbers.gemspec
|
40
|
+
homepage: https://github.com/gocardless/uk_phone_numbers
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.15
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A Ruby library for parsing and formatting UK phone numbers.
|
64
|
+
test_files:
|
65
|
+
- spec/uk_phone_numbers_spec.rb
|