dburkes-people_places_things 1.1.3 → 1.2.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.textile +15 -0
- data/VERSION +1 -1
- data/lib/people_places_things.rb +1 -0
- data/lib/people_places_things/phone_number/phone_number.rb +27 -1
- data/lib/people_places_things/zip_code/zip_code.rb +42 -0
- data/people_places_things.gemspec +6 -3
- data/spec/phone_number_spec.rb +21 -1
- data/spec/zip_code_spec.rb +31 -0
- metadata +4 -1
data/README.textile
CHANGED
@@ -4,6 +4,7 @@ PeoplePlacesThings is a collection of ruby classes for parsing and formatting U.
|
|
4
4
|
|
5
5
|
h2. Using PeoplePlacesThings
|
6
6
|
|
7
|
+
<pre><code>
|
7
8
|
require 'people_places_things'
|
8
9
|
|
9
10
|
name = PersonName.parse("george quincy harold peabody jr.")
|
@@ -30,6 +31,20 @@ h2. Using PeoplePlacesThings
|
|
30
31
|
ANSICounties.code_for('GA', 'FULTON') => 13121
|
31
32
|
ANSICounties.code_for(:state => 'ga', :county => 'fulton') => 13121
|
32
33
|
ANSICounties.data_for(13121) => { :state => 'GA', :county => 'FULTON' }
|
34
|
+
|
35
|
+
phone = PhoneNumber.parse '14045551212'
|
36
|
+
phone.country_code => 1
|
37
|
+
phone.area_code => '404'
|
38
|
+
phone.number => '5551212'
|
39
|
+
phone.exchange => '555'
|
40
|
+
phone.suffix => '1212'
|
41
|
+
phone.to_s => '1 (404) 555-1212'
|
42
|
+
|
43
|
+
zip = ZipCode.parse '30306-3522'
|
44
|
+
zip.base => '30306'
|
45
|
+
zip.plus_four => '3522'
|
46
|
+
zip.to_s => '30306-3522'
|
47
|
+
</code></pre>
|
33
48
|
|
34
49
|
h2. Data source
|
35
50
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/lib/people_places_things.rb
CHANGED
@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'people_places_things', 'street_addres
|
|
2
2
|
require File.join(File.dirname(__FILE__), 'people_places_things', 'person_name', 'person_name')
|
3
3
|
require File.join(File.dirname(__FILE__), 'people_places_things', 'ansi_counties', 'ansi_counties')
|
4
4
|
require File.join(File.dirname(__FILE__), 'people_places_things', 'phone_number', 'phone_number')
|
5
|
+
require File.join(File.dirname(__FILE__), 'people_places_things', 'zip_code', 'zip_code')
|
5
6
|
|
6
7
|
module PeoplePlacesThings
|
7
8
|
VERSION = File.read('VERSION').chomp.strip rescue "Unknown"
|
@@ -7,10 +7,12 @@ class PhoneNumber
|
|
7
7
|
parts.keys.each do |k|
|
8
8
|
send("#{k}=", parts[k]) if @@SUPPORTED_PARTS.include?(k)
|
9
9
|
end
|
10
|
+
|
11
|
+
raise "Unsupported Format" if !self.exchange || !self.suffix
|
10
12
|
end
|
11
13
|
|
12
14
|
def self.parse(string)
|
13
|
-
extract = string.match(
|
15
|
+
extract = string.strip.match(/^([-+()\d ]+)$/)[0].gsub(/[^\d]/, '') rescue nil
|
14
16
|
raise "Unsupported Format" if !extract || extract.length < 10 || extract.length > 11
|
15
17
|
|
16
18
|
parts = {}
|
@@ -35,4 +37,28 @@ class PhoneNumber
|
|
35
37
|
ret.raw = string
|
36
38
|
ret
|
37
39
|
end
|
40
|
+
|
41
|
+
def self.format(parts, fmt)
|
42
|
+
PhoneNumber.new(parts).to_s(fmt)
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s(fmt = :full_formatted)
|
46
|
+
raise "Unsupported Format" if !OUTPUT_FORMATS.include?(fmt)
|
47
|
+
|
48
|
+
case fmt
|
49
|
+
when :full_digits
|
50
|
+
"#{self.country_code}#{self.area_code}#{self.exchange}#{self.suffix}"
|
51
|
+
|
52
|
+
when :local_digits
|
53
|
+
"#{self.exchange}#{self.suffix}"
|
54
|
+
|
55
|
+
when :full_formatted
|
56
|
+
"#{self.country_code} (#{self.area_code}) #{self.exchange}-#{self.suffix}"
|
57
|
+
|
58
|
+
when :local_formatted
|
59
|
+
"#{self.exchange}-#{self.suffix}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
OUTPUT_FORMATS = [:full_digits, :local_digits, :full_formatted, :local_formatted]
|
38
64
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class ZipCode
|
2
|
+
@@SUPPORTED_PARTS = [:base, :plus_four]
|
3
|
+
@@SUPPORTED_PARTS.each {|attr| attr_accessor attr}
|
4
|
+
attr_accessor :raw
|
5
|
+
|
6
|
+
def initialize(parts={})
|
7
|
+
parts.keys.each do |k|
|
8
|
+
send("#{k}=", parts[k]) if @@SUPPORTED_PARTS.include?(k)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.parse(string)
|
13
|
+
tokens = string.strip.match(/^(\d{5})(-\d{4})?$/)[0].split('-') rescue nil
|
14
|
+
raise "Unsupported Format" if !tokens
|
15
|
+
|
16
|
+
parts = {}
|
17
|
+
parts[:base] = tokens.first
|
18
|
+
parts[:plus_four] = tokens[1] rescue nil
|
19
|
+
|
20
|
+
ret = ZipCode.new parts
|
21
|
+
ret.raw = string
|
22
|
+
ret
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.format(parts, fmt)
|
26
|
+
ZipCode.new(parts).to_s(fmt)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s(fmt = :plus_four)
|
30
|
+
raise "Unsupported Format" if !OUTPUT_FORMATS.include?(fmt)
|
31
|
+
|
32
|
+
case fmt
|
33
|
+
when :base
|
34
|
+
self.base
|
35
|
+
|
36
|
+
when :plus_four
|
37
|
+
[self.base, self.plus_four].compact.join('-')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
OUTPUT_FORMATS = [:base, :plus_four]
|
42
|
+
end
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{people_places_things}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Danny Burkes"]
|
@@ -28,12 +28,14 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/people_places_things/person_name/person_name.rb",
|
29
29
|
"lib/people_places_things/phone_number/phone_number.rb",
|
30
30
|
"lib/people_places_things/street_address/street_address.rb",
|
31
|
+
"lib/people_places_things/zip_code/zip_code.rb",
|
31
32
|
"people_places_things.gemspec",
|
32
33
|
"spec/ansi_counties_spec.rb",
|
33
34
|
"spec/helper.rb",
|
34
35
|
"spec/person_name_spec.rb",
|
35
36
|
"spec/phone_number_spec.rb",
|
36
|
-
"spec/street_address_spec.rb"
|
37
|
+
"spec/street_address_spec.rb",
|
38
|
+
"spec/zip_code_spec.rb"
|
37
39
|
]
|
38
40
|
s.has_rdoc = true
|
39
41
|
s.homepage = %q{http://github.com/dburkes/people_places_things}
|
@@ -46,7 +48,8 @@ Gem::Specification.new do |s|
|
|
46
48
|
"spec/helper.rb",
|
47
49
|
"spec/person_name_spec.rb",
|
48
50
|
"spec/phone_number_spec.rb",
|
49
|
-
"spec/street_address_spec.rb"
|
51
|
+
"spec/street_address_spec.rb",
|
52
|
+
"spec/zip_code_spec.rb"
|
50
53
|
]
|
51
54
|
|
52
55
|
if s.respond_to? :specification_version then
|
data/spec/phone_number_spec.rb
CHANGED
@@ -33,7 +33,27 @@ describe PhoneNumber do
|
|
33
33
|
phone.suffix.should == '1212'
|
34
34
|
end
|
35
35
|
|
36
|
-
it "should throw exception on unsupported format" do
|
36
|
+
it "should throw exception on unsupported parse format" do
|
37
37
|
lambda { PhoneNumber.parse('40455512') }.should raise_error
|
38
38
|
end
|
39
|
+
|
40
|
+
it "should format :full_digits" do
|
41
|
+
PhoneNumber.parse('14045551212').to_s(:full_digits).should == '14045551212'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should format :local_digits" do
|
45
|
+
PhoneNumber.parse('14045551212').to_s(:local_digits).should == '5551212'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should format :full_formatted" do
|
49
|
+
PhoneNumber.parse('14045551212').to_s(:full_formatted).should == '1 (404) 555-1212'
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should format :local_formatted" do
|
53
|
+
PhoneNumber.parse('14045551212').to_s(:local_formatted).should == '555-1212'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should throw exception on unsupported to_sformat" do
|
57
|
+
lambda { PhoneNumber.parse('14045551212').to_s(:bogus) }.should raise_error
|
58
|
+
end
|
39
59
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec/helper'
|
2
|
+
|
3
|
+
describe ZipCode do
|
4
|
+
it "should parse base" do
|
5
|
+
zip = ZipCode.parse '30306'
|
6
|
+
zip.base.should == '30306'
|
7
|
+
zip.plus_four.should == nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should parse plus four" do
|
11
|
+
zip = ZipCode.parse '30306-3522'
|
12
|
+
zip.base.should == '30306'
|
13
|
+
zip.plus_four.should == '3522'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should throw exception on unsupported parse format" do
|
17
|
+
lambda { ZipCode.parse('303065344') }.should raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should format :base" do
|
21
|
+
ZipCode.parse('30306-3522').to_s(:base).should == '30306'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should format :plus_four" do
|
25
|
+
ZipCode.parse('30306-3522').to_s(:plus_four).should == '30306-3522'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should throw exception on unsupported to_s format" do
|
29
|
+
lambda { ZipCode.parse('30306-3522').to_s(:bogus) }.should raise_error
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dburkes-people_places_things
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Burkes
|
@@ -34,12 +34,14 @@ files:
|
|
34
34
|
- lib/people_places_things/person_name/person_name.rb
|
35
35
|
- lib/people_places_things/phone_number/phone_number.rb
|
36
36
|
- lib/people_places_things/street_address/street_address.rb
|
37
|
+
- lib/people_places_things/zip_code/zip_code.rb
|
37
38
|
- people_places_things.gemspec
|
38
39
|
- spec/ansi_counties_spec.rb
|
39
40
|
- spec/helper.rb
|
40
41
|
- spec/person_name_spec.rb
|
41
42
|
- spec/phone_number_spec.rb
|
42
43
|
- spec/street_address_spec.rb
|
44
|
+
- spec/zip_code_spec.rb
|
43
45
|
has_rdoc: true
|
44
46
|
homepage: http://github.com/dburkes/people_places_things
|
45
47
|
post_install_message:
|
@@ -72,3 +74,4 @@ test_files:
|
|
72
74
|
- spec/person_name_spec.rb
|
73
75
|
- spec/phone_number_spec.rb
|
74
76
|
- spec/street_address_spec.rb
|
77
|
+
- spec/zip_code_spec.rb
|