chorn-dialable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Chris Horn http://chorn.com/
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 ADDED
@@ -0,0 +1,22 @@
1
+
2
+ Phone numbers are not a big deal if you can validate the input at the time you've got a human right there. My enterprise tends not to have that ability, as we receive large files from clients with little or no validation done. Rather than abandon #s which don't validate, I wrote this to parse and normalize a string into a standard NANP phone number, possibly including an extension.
3
+
4
+ References: NANPA: North American Numbering Plan Administration http://nanpa.com/
5
+
6
+ require "dialable"
7
+ pn = Dialable::NANP.parse("+1(800)555-1212 ext 1234")
8
+
9
+ >> puts pn.to_s // Pretty output
10
+ 800-555-1212 x1234
11
+
12
+ >> puts pn.to_digits // Address book friendly
13
+ 8005551212 x1234
14
+
15
+ >> puts pn.to_dialable // PBX friendly
16
+ 8005551212
17
+
18
+ >> puts pn.extension
19
+ 1234
20
+
21
+
22
+ -chorn
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ == dialable
2
+
3
+ A gem that provides parsing and output of phone numbers according to NANPA standards.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = "dialable"
8
+ GEM_VERSION = "0.0.1"
9
+ AUTHOR = "Chris Horn"
10
+ EMAIL = "chorn@chorn.com"
11
+ HOMEPAGE = "http://github.com/chorn/dialable"
12
+ SUMMARY = "A gem that provides parsing and output of phone numbers according to NANPA standards."
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
20
+ s.summary = SUMMARY
21
+ s.description = s.summary
22
+ s.author = AUTHOR
23
+ s.email = EMAIL
24
+ s.homepage = HOMEPAGE
25
+
26
+ # Uncomment this to add a dependency
27
+ # s.add_dependency "foo"
28
+
29
+ s.require_path = 'lib'
30
+ s.autorequire = GEM
31
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ desc "Run specs"
37
+ Spec::Rake::SpecTask.new do |t|
38
+ t.spec_files = FileList['spec/**/*_spec.rb']
39
+ t.spec_opts = %w(-fs --color)
40
+ end
41
+
42
+
43
+ Rake::GemPackageTask.new(spec) do |pkg|
44
+ pkg.gem_spec = spec
45
+ end
46
+
47
+ desc "install the gem locally"
48
+ task :install => [:package] do
49
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
50
+ end
51
+
52
+ desc "create a gemspec file"
53
+ task :make_spec do
54
+ File.open("#{GEM}.gemspec", "w") do |file|
55
+ file.puts spec.to_ruby
56
+ end
57
+ end
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ TODO:
2
+ Make rspec actually test some stuff. To be fair I've already tested this against 3.5m numbers from my own system, but I can't post those. :)
data/dialable.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "dialable"
3
+ s.version = "0.0.1"
4
+ s.date = "2008-11-25"
5
+ s.summary = "Provides parsing and output of phone numbers according to NANPA standards."
6
+ s.email = "chorn@chorn.com"
7
+ s.homepage = "http://github.com/chorn/dialable"
8
+ s.description = "A gem that provides parsing and output of phone numbers according to NANPA standards."
9
+ s.has_rdoc = true
10
+ s.authors = ["Chris Horn"]
11
+ s.files = [ "LICENSE",
12
+ "README",
13
+ "README.rdoc",
14
+ "Rakefile",
15
+ "TODO",
16
+ "dialable.gemspec",
17
+ "lib/dialable.rb",
18
+ "script/destroy",
19
+ "script/generate",
20
+ "spec/dialable_spec.rb",
21
+ "spec/spec_helper.rb",
22
+ "test.rb"]
23
+ s.test_files = []
24
+ s.rdoc_options = []
25
+ end
data/lib/dialable.rb ADDED
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2008 Chris Horn http://chorn.com/
2
+ # See MIT-LICENSE.txt
3
+
4
+ # I'm pretty sure this whole thing sucks.
5
+ # TODO: Make it not suck.
6
+
7
+ # Comments are for suckers.
8
+
9
+ module Dialable
10
+ class NANP
11
+ ##
12
+ # Raised if something other than a valid NANP is supplied
13
+ class InvalidNANPError < StandardError
14
+ end
15
+
16
+ module ServiceCodes # ERC, Easily Recognizable Codes
17
+ ERC = {
18
+ 211 => "Community Information and Referral Services",
19
+ 311 => "Non-Emergency Police and Other Governmental Services",
20
+ 411 => "Local Directory Assistance",
21
+ 511 => "Traffic and Transportation Information (US); Provision of Weather and Traveller Information Services (Canada)",
22
+ 611 => "Repair Service",
23
+ 711 => "Telecommunications Relay Service (TRS)",
24
+ 811 => "Access to One Call Services to Protect Pipeline and Utilities from Excavation Damage (US); Non-Urgent Health Teletriage Services (Canada)",
25
+ 911 => "Emergency"
26
+ }
27
+ end
28
+
29
+ module Patterns
30
+ VALID = [
31
+ /^\D*1?\D*([2-9]\d\d)\D*(\d{3})\D*(\d{4})\D*[ex]+\D*(\d{1,5})\D*$/i,
32
+ /^\D*1?\D*([2-9]\d\d)[ $\\\.-]*(\d{3})[ $\\\.-]*(\d{4})[ $\\\.\*-]*(\d{1,5})\D*$/i,
33
+ /^\D*1?\D*([2-9]\d\d)\D*(\d{3})\D*(\d{4})\D*$/,
34
+ /^(\D*)(\d{3})\D*(\d{4})\D*$/,
35
+ /^\D*([2-9]11)\D*$/,
36
+ /^\D*1?\D*([2-9]\d\d)\D*(\d{3})\D*(\d{4})\D.*/ # Last ditch, just find a number
37
+ ]
38
+ end
39
+
40
+ attr_accessor :areacode, :prefix, :line, :extension
41
+
42
+ def initialize(parts={})
43
+ self.areacode = parts[:areacode] ? parts[:areacode] : nil
44
+ self.prefix = parts[:prefix] ? parts[:prefix] : nil
45
+ self.line = parts[:line] ? parts[:line] : nil
46
+ self.extension = parts[:extension] ? parts[:extension] : nil
47
+ end
48
+
49
+ def self.parse(number)
50
+ Patterns::VALID.each do |pat|
51
+ return Dialable::NANP.new(:areacode => $1, :prefix => $2, :line => $3, :extension => $4) if number =~ pat
52
+ end
53
+
54
+ raise InvalidNANPError, "Not a valid NANP Phone Number."
55
+ end
56
+
57
+ def erc?
58
+ return ServiceCodes::ERC[@areacode].nil?
59
+ end
60
+
61
+ def to_s
62
+ rtn = "#{@prefix}-#{@line}"
63
+ rtn = "#{@areacode}-#{rtn}" if @areacode
64
+ rtn = "#{rtn} x#{@extension}" if @extension
65
+ rtn
66
+ end
67
+
68
+ def to_digits
69
+ rtn = "#{@prefix}#{@line}"
70
+ rtn = "#{@areacode}#{rtn}" if @areacode
71
+ rtn = "#{rtn} x#{@extension}" if @extension
72
+ rtn
73
+ end
74
+
75
+ def to_dialable
76
+ rtn = "#{@prefix}#{@line}"
77
+ rtn = "#{@areacode}#{rtn}" if @areacode
78
+ rtn
79
+ end
80
+
81
+ def to_hash
82
+ return {
83
+ :areacode => @areacode,
84
+ :prefix => @prefix,
85
+ :line => @line,
86
+ :extension => @extension
87
+ }
88
+ end
89
+ end
90
+ end
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "dialable" do
4
+ it "should do nothing" do
5
+ true.should == true
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
data/test.rb ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright (c) 2008 Chris Horn http://chorn.com/
3
+ # See MIT-LICENSE.txt
4
+
5
+ require "dialable"
6
+
7
+ ARGV.each do |filename|
8
+ File.open(filename).each do |line|
9
+ line.gsub!(/"/,"")
10
+
11
+
12
+ next if line =~ /^(\d{10})$/ or line =~ /^\d{3}-\d{3}-\d{4}$/
13
+
14
+ begin
15
+ d = Dialable::NANP.parse(line)
16
+ rescue
17
+ d = "XXX-XXX-XXXX"
18
+ end
19
+
20
+ puts "#{d} #{line}"
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chorn-dialable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Horn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-25 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A gem that provides parsing and output of phone numbers according to NANPA standards.
17
+ email: chorn@chorn.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - LICENSE
26
+ - README
27
+ - README.rdoc
28
+ - Rakefile
29
+ - TODO
30
+ - dialable.gemspec
31
+ - lib/dialable.rb
32
+ - script/destroy
33
+ - script/generate
34
+ - spec/dialable_spec.rb
35
+ - spec/spec_helper.rb
36
+ - test.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/chorn/dialable
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Provides parsing and output of phone numbers according to NANPA standards.
63
+ test_files: []
64
+