ie_phone_numbers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +12 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/ie_phone_numbers.gemspec +16 -0
- data/lib/ie_phone_numbers.rb +40 -0
- data/lib/ie_phone_numbers/version.rb +3 -0
- data/spec/ie_phone_numbers_spec.rb +65 -0
- metadata +71 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 BorrowMyDoggy
|
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
|
+
# ie_phone_numbers
|
2
|
+
|
3
|
+
A Ruby library for validating and formatting Irish phone numbers. Heavily influenced by the uk_phone_numbers gem from GoCardless.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```console
|
8
|
+
$ gem install ie_phone_numbers
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Validating a phone number
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require 'ie_phone_numbers'
|
17
|
+
|
18
|
+
IEPhoneNumbers.valid? "0218713456"
|
19
|
+
# => true
|
20
|
+
```
|
21
|
+
|
22
|
+
### Formatting a phone number
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'ie_phone_numbers'
|
26
|
+
|
27
|
+
IEPhoneNumbers.format "0218713456"
|
28
|
+
# => "021 871 3456"
|
29
|
+
```
|
30
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.expand_path('../lib/ie_phone_numbers/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'ie_phone_numbers'
|
5
|
+
gem.version = IEPhoneNumbers::VERSION.dup
|
6
|
+
gem.authors = ['Simon Reed']
|
7
|
+
gem.email = ['simonpreed@gmail.com']
|
8
|
+
gem.description = 'A Ruby library for validating and formatting Irish phone numbers.'
|
9
|
+
gem.summary = 'A Ruby library for validating and formatting IE phone numbers.'
|
10
|
+
gem.homepage = 'https://github.com/simonreed/ie_phone_numbers'
|
11
|
+
|
12
|
+
gem.add_development_dependency 'rspec', '~> 2.6'
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- spec/*`.split("\n")
|
16
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module IEPhoneNumbers
|
2
|
+
AREA_CODES_WITH_SUBSCRIBER_NUMBER_LIMITS = {
|
3
|
+
'1'=>'7', '21'=>'', '22'=>'', '23'=>'', '24'=>'', '25'=>'', '242'=>'',
|
4
|
+
'225'=>'', '26'=>'', '27'=>'', '28'=>'', '29'=>'', '402'=>'', '404'=>'',
|
5
|
+
'405'=>'','41'=>'', '42'=>'', '43'=>'', '44'=>'', '45'=>'', '46'=>'',
|
6
|
+
'47'=>'', '48'=>'', '49'=>'', '51'=>'', '52'=>'', '53'=>'', '54'=>'',
|
7
|
+
'55'=>'', '56'=>'', '57'=>'', '58'=>'5', '59'=>'7', '502'=>'', '504'=>'',
|
8
|
+
'505'=>'5', '506'=>'', '509'=>'', '61'=>'', '62'=>'', '63'=>'', '64'=>'',
|
9
|
+
'65'=>'', '66'=>'', '67'=>'', '68'=>'', '69'=>'', '71'=>'', '74'=>'',
|
10
|
+
'818'=>'6', '90'=>'', '91'=>'', '92'=>'', '93'=>'', '94'=>'', '95'=>'',
|
11
|
+
'96'=>'', '97'=>'', '98'=>'', '99'=>'', '83'=>'7', '85'=>'7', '86'=>'7',
|
12
|
+
'87'=>'7', '88'=>'7', '89'=>'7', '83'=>'7', '85'=>'7', '86'=>'7',
|
13
|
+
'87'=>'7', '88'=>'7', '89' => '7', '1800' =>'6', '1850' =>'6','1890' =>'6'
|
14
|
+
}
|
15
|
+
REGEXPS = []
|
16
|
+
|
17
|
+
def self.pattern_to_regexp(area_code,subscriber_number_limit='')
|
18
|
+
if subscriber_number_limit == ''
|
19
|
+
subscriber_number_limit = '2,4'
|
20
|
+
else
|
21
|
+
subscriber_number_limit = subscriber_number_limit.to_i - 3
|
22
|
+
end
|
23
|
+
regexp = "(0?#{area_code})(\\d{3})(\\d{#{subscriber_number_limit}})"
|
24
|
+
Regexp.new("\\A#{regexp}\\z")
|
25
|
+
end
|
26
|
+
|
27
|
+
AREA_CODES_WITH_SUBSCRIBER_NUMBER_LIMITS.each do | area_code, number_limit |
|
28
|
+
REGEXPS << pattern_to_regexp(area_code, number_limit)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.valid?(number)
|
32
|
+
REGEXPS.select { |r| r.match(number) }.any?
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.format(number, opts = {})
|
36
|
+
match = REGEXPS.map { |r| r.match(number) }.reject(&:nil?).first
|
37
|
+
match ? match[1..-1].join(opts[:separator] || ' ') : false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'ie_phone_numbers'
|
2
|
+
|
3
|
+
describe IEPhoneNumbers do
|
4
|
+
describe "::REGEXPS" do
|
5
|
+
subject { IEPhoneNumbers::REGEXPS }
|
6
|
+
it { should have(IEPhoneNumbers::AREA_CODES_WITH_SUBSCRIBER_NUMBER_LIMITS.
|
7
|
+
count).items }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ".pattern_to_regexp" do
|
11
|
+
def regexp_for(area_code, subscriber_number_limit)
|
12
|
+
subject.pattern_to_regexp(area_code, subscriber_number_limit).source
|
13
|
+
end
|
14
|
+
|
15
|
+
it "given a String pattern returns a Regexp" do
|
16
|
+
subject.pattern_to_regexp('21','').should be_a Regexp
|
17
|
+
end
|
18
|
+
|
19
|
+
it "handle a blank subscriber number limit" do
|
20
|
+
regexp_for('21','').should include("(0?21)(\\d{3})(\\d{2,4})")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "handle a set subscriber number limit" do
|
24
|
+
regexp_for('21','7').should include("(0?21)(\\d{3})(\\d{4})")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".valid?" do
|
29
|
+
it "returns true for matching numbers" do
|
30
|
+
subject.valid?('0219876543').should be_true
|
31
|
+
subject.valid?('021987654').should be_true
|
32
|
+
subject.valid?('02198765').should be_true
|
33
|
+
subject.valid?('2198765').should be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns false for invalid numbers" do
|
37
|
+
subject.valid?('0319876543').should be_false
|
38
|
+
subject.valid?('02198765435').should be_false
|
39
|
+
subject.valid?('2198765435').should be_false
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns false for multiline strings containing a valid number" do
|
43
|
+
subject.valid?("02198765435\nnotaphonenumber\n").should be_false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ".format" do
|
48
|
+
it "returns false for invalid numbers" do
|
49
|
+
subject.format('02198765435').should be_false
|
50
|
+
end
|
51
|
+
|
52
|
+
it "formats long-prefix numbers correctly" do
|
53
|
+
subject.format('0219876543').should == '021 987 6543'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "formats mobile numbers correctly" do
|
57
|
+
subject.format('0890000000').should == '089 000 0000'
|
58
|
+
end
|
59
|
+
|
60
|
+
it "uses a custom separator when provided" do
|
61
|
+
subject.format('0219876543', separator: '-').should == '021-987-6543'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ie_phone_numbers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Simon Reed
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !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: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.6'
|
30
|
+
description: A Ruby library for validating and formatting Irish phone numbers.
|
31
|
+
email:
|
32
|
+
- simonpreed@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- CHANGELOG.md
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- ie_phone_numbers.gemspec
|
43
|
+
- lib/ie_phone_numbers.rb
|
44
|
+
- lib/ie_phone_numbers/version.rb
|
45
|
+
- spec/ie_phone_numbers_spec.rb
|
46
|
+
homepage: https://github.com/simonreed/ie_phone_numbers
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: A Ruby library for validating and formatting IE phone numbers.
|
70
|
+
test_files:
|
71
|
+
- spec/ie_phone_numbers_spec.rb
|