dialable 0.1.1 → 0.5.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/README.rdoc +1 -1
- data/Rakefile +5 -25
- data/{support → data}/nanpa.yaml +923 -572
- data/dialable.gemspec +5 -6
- data/lib/dialable.rb +13 -11
- data/spec/dialable_spec.rb +36 -3
- data/spec/spec_helper.rb +7 -2
- data/support/make_yaml_nanpa.rb +11 -6
- metadata +8 -22
- data/script/destroy +0 -14
- data/script/generate +0 -14
data/dialable.gemspec
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
Gem::Specification.new do |s|
|
|
2
|
+
s.licenses = ['MIT', 'LGPL-2']
|
|
2
3
|
s.name = "dialable"
|
|
3
|
-
s.version = "0.
|
|
4
|
-
s.date = "
|
|
4
|
+
s.version = "0.5.0"
|
|
5
|
+
s.date = "2011-06-22"
|
|
5
6
|
s.summary = "Provides parsing and output of phone numbers according to NANPA standards."
|
|
6
7
|
s.email = "chorn@chorn.com"
|
|
7
8
|
s.homepage = "http://github.com/chorn/dialable"
|
|
@@ -14,12 +15,10 @@ Gem::Specification.new do |s|
|
|
|
14
15
|
"TODO",
|
|
15
16
|
"dialable.gemspec",
|
|
16
17
|
"lib/dialable.rb",
|
|
17
|
-
"
|
|
18
|
-
"script/generate",
|
|
18
|
+
"data/nanpa.yaml",
|
|
19
19
|
"spec/dialable_spec.rb",
|
|
20
20
|
"spec/spec_helper.rb",
|
|
21
|
-
"support/make_yaml_nanpa.rb"
|
|
22
|
-
"support/nanpa.yaml",
|
|
21
|
+
"support/make_yaml_nanpa.rb"
|
|
23
22
|
]
|
|
24
23
|
s.test_files = []
|
|
25
24
|
s.rdoc_options = []
|
data/lib/dialable.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# Copyright (c) 2008-
|
|
1
|
+
# Copyright (c) 2008-2011 Chris Horn http://chorn.com/
|
|
2
2
|
# See MIT-LICENSE.txt
|
|
3
3
|
|
|
4
|
-
# TODO: Make this less sucky.
|
|
5
|
-
|
|
6
4
|
require "yaml"
|
|
5
|
+
require "tzinfo"
|
|
6
|
+
require 'rubygems'
|
|
7
7
|
|
|
8
8
|
module Dialable
|
|
9
9
|
class NANP
|
|
@@ -29,7 +29,7 @@ module Dialable
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
##
|
|
32
|
-
#
|
|
32
|
+
# Regexs to match valid phone numbers
|
|
33
33
|
module Patterns
|
|
34
34
|
VALID = [
|
|
35
35
|
/^\D*1?\D*([2-9]\d\d)\D*(\d{3})\D*(\d{4})\D*[ex]+\D*(\d{1,5})\D*$/i,
|
|
@@ -44,10 +44,12 @@ module Dialable
|
|
|
44
44
|
##
|
|
45
45
|
# Valid area codes per nanpa.com
|
|
46
46
|
module AreaCodes
|
|
47
|
-
|
|
47
|
+
data_path = Gem::datadir('dialable')
|
|
48
|
+
data_path ||= File.join(File.dirname(__FILE__), '..', 'data')
|
|
49
|
+
NANP = YAML.load_file(data_path + "/nanpa.yaml")
|
|
48
50
|
end
|
|
49
51
|
|
|
50
|
-
attr_accessor :areacode, :prefix, :line, :extension, :location, :country, :timezones, :relative_timezones
|
|
52
|
+
attr_accessor :areacode, :prefix, :line, :extension, :location, :country, :timezones, :relative_timezones, :raw_timezone
|
|
51
53
|
|
|
52
54
|
def initialize(parts={})
|
|
53
55
|
self.areacode = parts[:areacode] ? parts[:areacode] : nil
|
|
@@ -62,13 +64,14 @@ module Dialable
|
|
|
62
64
|
@areacode = raw_code
|
|
63
65
|
self.location = AreaCodes::NANP[code][:location] if AreaCodes::NANP[code][:location]
|
|
64
66
|
self.country = AreaCodes::NANP[code][:country] if AreaCodes::NANP[code][:country]
|
|
67
|
+
self.raw_timezone = AreaCodes::NANP[code][:timezone] if AreaCodes::NANP[code][:timezone]
|
|
65
68
|
|
|
66
69
|
if AreaCodes::NANP[code][:timezone]
|
|
67
70
|
self.timezones = []
|
|
68
71
|
self.relative_timezones = []
|
|
69
72
|
tz = AreaCodes::NANP[code][:timezone]
|
|
70
|
-
|
|
71
|
-
local_utc_offset =
|
|
73
|
+
now = Time.now
|
|
74
|
+
local_utc_offset = now.utc_offset/3600
|
|
72
75
|
|
|
73
76
|
if tz =~ /UTC(-\d+)/
|
|
74
77
|
self.timezones << tz
|
|
@@ -78,7 +81,7 @@ module Dialable
|
|
|
78
81
|
tz.split(//).each do |zone| # http://www.timeanddate.com/library/abbreviations/timezones/na/
|
|
79
82
|
zone = "HA" if zone == "H"
|
|
80
83
|
zone = "AK" if zone == "K"
|
|
81
|
-
tz = zone + (
|
|
84
|
+
tz = zone + (now.dst? ? "D" : "S") + "T" # This is cludgey
|
|
82
85
|
self.timezones << tz
|
|
83
86
|
delta = nil
|
|
84
87
|
if Time.zone_offset(tz)
|
|
@@ -94,7 +97,7 @@ module Dialable
|
|
|
94
97
|
when /HA?[SD]T/
|
|
95
98
|
delta = -10 - local_utc_offset
|
|
96
99
|
end
|
|
97
|
-
delta = delta - 1 if
|
|
100
|
+
delta = delta - 1 if delta and now.dst?
|
|
98
101
|
end
|
|
99
102
|
# puts "#{delta} // #{Time.zone_offset(tz)} // #{tz} // #{local_utc_offset}"
|
|
100
103
|
self.relative_timezones << delta if delta
|
|
@@ -121,7 +124,6 @@ module Dialable
|
|
|
121
124
|
# end
|
|
122
125
|
# rt
|
|
123
126
|
# end
|
|
124
|
-
|
|
125
127
|
|
|
126
128
|
def self.parse(number)
|
|
127
129
|
Patterns::VALID.each do |pattern|
|
data/spec/dialable_spec.rb
CHANGED
|
@@ -1,7 +1,40 @@
|
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
|
|
3
|
-
describe
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
describe Dialable do
|
|
4
|
+
describe "with a full NANP number with extension" do
|
|
5
|
+
subject { Dialable::NANP.parse("+1(307)555-1212 ext 1234") }
|
|
6
|
+
|
|
7
|
+
it("should extract the area code") { subject.areacode.should == "307" }
|
|
8
|
+
it("should extract the prefix") { subject.prefix.should == "555" }
|
|
9
|
+
it("should extract the line number") { subject.line.should == "1212" }
|
|
10
|
+
it("should extract the extension") { subject.extension.should == "1234" }
|
|
11
|
+
it("should determine the time zone") { subject.timezones.should == ["MDT"] }
|
|
6
12
|
end
|
|
13
|
+
|
|
14
|
+
NANP = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data') + "/nanpa.yaml")
|
|
15
|
+
NANP.delete(:created)
|
|
16
|
+
NANP.each do |nanp|
|
|
17
|
+
areacode = nanp[0]
|
|
18
|
+
describe "recognize the area code #{areacode} in ()'s" do
|
|
19
|
+
subject { Dialable::NANP.parse("(#{areacode})5551212") }
|
|
20
|
+
it("should extract the area code") { subject.areacode.should == areacode.to_s }
|
|
21
|
+
end
|
|
22
|
+
describe "recognize the area code #{areacode} with garbage" do
|
|
23
|
+
subject { Dialable::NANP.parse("noise #{areacode}noise5551212") }
|
|
24
|
+
it("should extract the area code") { subject.areacode.should == areacode.to_s }
|
|
25
|
+
end
|
|
26
|
+
describe "with a sloppy areacode of #{areacode} and number" do
|
|
27
|
+
subject { Dialable::NANP.parse("+1-#{areacode}555 1212") }
|
|
28
|
+
it("should match the location") { subject.location.should == nanp[1][:location] }
|
|
29
|
+
end
|
|
30
|
+
describe "with areacode of #{areacode} and a sloppy extension" do
|
|
31
|
+
subject { Dialable::NANP.parse("1 #{areacode} 867 5309 xAAA0001") }
|
|
32
|
+
it("should match the country") { subject.country.should == nanp[1][:country] }
|
|
33
|
+
end
|
|
34
|
+
describe "with areacode of #{areacode} and lots of garbage" do
|
|
35
|
+
subject { Dialable::NANP.parse("1 #{areacode}yadayada867BLAH5309....derp") }
|
|
36
|
+
it("should match the timezone") { subject.raw_timezone.should == nanp[1][:timezone] }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
7
40
|
end
|
data/spec/spec_helper.rb
CHANGED
data/support/make_yaml_nanpa.rb
CHANGED
|
@@ -11,19 +11,24 @@ system('mdb-export AllNPAs.mdb "`mdb-tables -1 AllNPAs.mdb`" > AllNPAs.csv')
|
|
|
11
11
|
all = CSV.read("AllNPAs.csv")
|
|
12
12
|
h = all[0]
|
|
13
13
|
|
|
14
|
+
in_service = h.index("In Service?")
|
|
15
|
+
location = h.index("Location")
|
|
16
|
+
country = h.index("Country")
|
|
17
|
+
tz = h.index("Time Zone")
|
|
18
|
+
npa = h.index("NPA")
|
|
19
|
+
|
|
14
20
|
nanpa = { :created => Time.now }
|
|
15
21
|
|
|
16
22
|
all.each do |row|
|
|
17
|
-
next unless row[
|
|
18
|
-
areacode = row.
|
|
23
|
+
next unless row[in_service] == "Yes"
|
|
24
|
+
areacode = row[npa].to_s.to_i
|
|
19
25
|
nanpa[areacode] = {}
|
|
20
|
-
nanpa[areacode][:
|
|
21
|
-
nanpa[areacode][:
|
|
22
|
-
nanpa[areacode][:
|
|
26
|
+
nanpa[areacode][:country] = row[country].to_s if row[country].to_s.size > 0
|
|
27
|
+
nanpa[areacode][:timezone] = row[tz].to_s if row[tz].to_s.size > 0
|
|
28
|
+
nanpa[areacode][:location] = row[location].to_s if row[location].to_s.size > 0 and row[location].to_s !~ /NANP Area/i and row[location].to_s !~ /^#{row[country].to_s}$/i
|
|
23
29
|
end
|
|
24
30
|
|
|
25
31
|
puts nanpa.to_yaml
|
|
26
32
|
|
|
27
33
|
FileUtils.rm ["AllNPAs.zip", "AllNPAs.mdb", "AllNPAs.csv"]
|
|
28
34
|
|
|
29
|
-
|
metadata
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dialable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
segments:
|
|
7
|
-
- 0
|
|
8
|
-
- 1
|
|
9
|
-
- 1
|
|
10
|
-
version: 0.1.1
|
|
4
|
+
prerelease:
|
|
5
|
+
version: 0.5.0
|
|
11
6
|
platform: ruby
|
|
12
7
|
authors:
|
|
13
8
|
- Chris Horn
|
|
@@ -15,8 +10,7 @@ autorequire:
|
|
|
15
10
|
bindir: bin
|
|
16
11
|
cert_chain: []
|
|
17
12
|
|
|
18
|
-
date:
|
|
19
|
-
default_executable:
|
|
13
|
+
date: 2011-06-22 00:00:00 Z
|
|
20
14
|
dependencies: []
|
|
21
15
|
|
|
22
16
|
description: A gem that provides parsing and output of phone numbers according to NANPA (North American Numbering Plan Administration) standards. If possible, time zones are populated by abbreviation as well as offset relative to the local timezone.
|
|
@@ -34,16 +28,14 @@ files:
|
|
|
34
28
|
- TODO
|
|
35
29
|
- dialable.gemspec
|
|
36
30
|
- lib/dialable.rb
|
|
37
|
-
-
|
|
38
|
-
- script/generate
|
|
31
|
+
- data/nanpa.yaml
|
|
39
32
|
- spec/dialable_spec.rb
|
|
40
33
|
- spec/spec_helper.rb
|
|
41
34
|
- support/make_yaml_nanpa.rb
|
|
42
|
-
- support/nanpa.yaml
|
|
43
|
-
has_rdoc: true
|
|
44
35
|
homepage: http://github.com/chorn/dialable
|
|
45
|
-
licenses:
|
|
46
|
-
|
|
36
|
+
licenses:
|
|
37
|
+
- MIT
|
|
38
|
+
- LGPL-2
|
|
47
39
|
post_install_message:
|
|
48
40
|
rdoc_options: []
|
|
49
41
|
|
|
@@ -54,23 +46,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
54
46
|
requirements:
|
|
55
47
|
- - ">="
|
|
56
48
|
- !ruby/object:Gem::Version
|
|
57
|
-
hash: 3
|
|
58
|
-
segments:
|
|
59
|
-
- 0
|
|
60
49
|
version: "0"
|
|
61
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
51
|
none: false
|
|
63
52
|
requirements:
|
|
64
53
|
- - ">="
|
|
65
54
|
- !ruby/object:Gem::Version
|
|
66
|
-
hash: 3
|
|
67
|
-
segments:
|
|
68
|
-
- 0
|
|
69
55
|
version: "0"
|
|
70
56
|
requirements: []
|
|
71
57
|
|
|
72
58
|
rubyforge_project:
|
|
73
|
-
rubygems_version: 1.
|
|
59
|
+
rubygems_version: 1.8.5
|
|
74
60
|
signing_key:
|
|
75
61
|
specification_version: 3
|
|
76
62
|
summary: Provides parsing and output of phone numbers according to NANPA standards.
|
data/script/destroy
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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)
|