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.
@@ -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.1.1"
4
- s.date = "2010-06-28"
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
- "script/destroy",
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 = []
@@ -1,9 +1,9 @@
1
- # Copyright (c) 2008-2010 Chris Horn http://chorn.com/
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
- # Regex's to match valid phone numbers
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
- NANP = YAML.load_file(File.dirname(__FILE__) + "/../support/nanpa.yaml")
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
- t = Time.now
71
- local_utc_offset = t.utc_offset/3600
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 + (t.dst? ? "D" : "S") + "T" # This is cludgey
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 t.dst?
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|
@@ -1,7 +1,40 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
- describe "dialable" do
4
- it "should do nothing" do
5
- true.should == true
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
@@ -1,2 +1,7 @@
1
- $TESTING=true
2
- $:.push File.join(File.dirname(__FILE__), '..', 'lib')
1
+ $LOAD_PATH.push File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require "rubygems"
4
+ require "dialable"
5
+
6
+ RSpec.configure do |config|
7
+ end
@@ -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[h.index("In Service?")] == "Yes"
18
- areacode = row.values_at(h.index("NPA")).to_s.to_i
23
+ next unless row[in_service] == "Yes"
24
+ areacode = row[npa].to_s.to_i
19
25
  nanpa[areacode] = {}
20
- nanpa[areacode][:location] = row.values_at(h.index("Location")).to_s if row.values_at(h.index("Location")).to_s.size > 0 and row.values_at(h.index("Location")).to_s.size =~ /NANP area/
21
- nanpa[areacode][:country] = row.values_at(h.index("Country")).to_s if row.values_at(h.index("Country")).to_s.size > 0
22
- nanpa[areacode][:timezone] = row.values_at(h.index("Time Zone")).to_s if row.values_at(h.index("Time Zone")).to_s.size > 0
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
- hash: 25
5
- prerelease: false
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: 2010-06-28 00:00:00 -04:00
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
- - script/destroy
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.3.7
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.
@@ -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)
@@ -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)