people_places_things 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ module PeoplePlacesThings
2
+ class ZipCode
3
+ attr_accessor :base, :plus_four, :raw
4
+
5
+ def initialize(str)
6
+ tokens = str.strip.match(/^(\d{5})(-\d{4})?$/)[0].split('-') rescue nil
7
+ raise "Unsupported Format" if !tokens
8
+
9
+ self.base = tokens.first
10
+ self.plus_four = tokens[1] rescue nil
11
+ end
12
+
13
+ def to_s
14
+ [self.base, self.plus_four].compact.join('-')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'people_places_things/street_address'
2
+ require 'people_places_things/person_name'
3
+ require 'people_places_things/ansi_counties'
4
+ require 'people_places_things/phone_number'
5
+ require 'people_places_things/zip_code'
6
+ require 'people_places_things/state'
7
+ require 'people_places_things/location'
8
+
9
+ module PeoplePlacesThings
10
+ VERSION = File.read(File.join(File.dirname(__FILE__), 'people_places_things', 'VERSION')).chomp.strip rescue "Unknown"
11
+ end
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{people_places_things}
8
+ s.version = "2.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Danny Burkes"]
12
+ s.date = %q{2009-09-01}
13
+ s.description = %q{Parsers and formatters for person names, street addresses, city/state/zip, phone numbers, etc.}
14
+ s.email = %q{dburkes@netable.com}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "README.textile",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/people_places_things.rb",
24
+ "lib/people_places_things/VERSION",
25
+ "lib/people_places_things/ansi_counties.rb",
26
+ "lib/people_places_things/data/data.yml",
27
+ "lib/people_places_things/data/process_data.rb",
28
+ "lib/people_places_things/data/raw.txt",
29
+ "lib/people_places_things/location.rb",
30
+ "lib/people_places_things/person_name.rb",
31
+ "lib/people_places_things/phone_number.rb",
32
+ "lib/people_places_things/state.rb",
33
+ "lib/people_places_things/street_address.rb",
34
+ "lib/people_places_things/zip_code.rb",
35
+ "people_places_things.gemspec",
36
+ "spec/ansi_counties_spec.rb",
37
+ "spec/helper.rb",
38
+ "spec/location_spec.rb",
39
+ "spec/person_name_spec.rb",
40
+ "spec/phone_number_spec.rb",
41
+ "spec/state_spec.rb",
42
+ "spec/street_address_spec.rb",
43
+ "spec/zip_code_spec.rb"
44
+ ]
45
+ s.has_rdoc = true
46
+ s.homepage = %q{http://github.com/dburkes/people_places_things}
47
+ s.rdoc_options = ["--charset=UTF-8"]
48
+ s.require_paths = ["lib"]
49
+ s.rubygems_version = %q{1.3.1}
50
+ s.summary = %q{Parsers and formatters for person names, street addresses, city/state/zip, phone numbers, etc.}
51
+ s.test_files = [
52
+ "spec/ansi_counties_spec.rb",
53
+ "spec/helper.rb",
54
+ "spec/location_spec.rb",
55
+ "spec/person_name_spec.rb",
56
+ "spec/phone_number_spec.rb",
57
+ "spec/state_spec.rb",
58
+ "spec/street_address_spec.rb",
59
+ "spec/zip_code_spec.rb"
60
+ ]
61
+
62
+ if s.respond_to? :specification_version then
63
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
64
+ s.specification_version = 2
65
+
66
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
67
+ else
68
+ end
69
+ else
70
+ end
71
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec/helper'
2
+
3
+ describe ANSICounties do
4
+ it "should find code for valid state and county" do
5
+ ANSICounties.code_for('ga', 'fulton').should == 13121
6
+ end
7
+
8
+ it "should find code for valid state and county hash" do
9
+ ANSICounties.code_for(:state => 'ga', :county => 'fulton').should == 13121
10
+ end
11
+
12
+ it "should return nil for invalid state and county" do
13
+ ANSICounties.code_for(:state => 'ga', :county => 'fubar').should == nil
14
+ end
15
+
16
+ it "should find code for alternate st. form" do
17
+ ANSICounties.code_for(:state => 'MO', :county => 'saint LOUIS').should == 29510
18
+ end
19
+
20
+ it "should find data for valid code" do
21
+ ANSICounties.data_for(13121).should == { :state => 'GA', :county => 'FULTON' }
22
+ end
23
+
24
+ it "should return nil for invalid code" do
25
+ ANSICounties.data_for(1312111).should == nil
26
+ end
27
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'lib/people_places_things'
2
+ include PeoplePlacesThings
@@ -0,0 +1,52 @@
1
+ require 'spec/helper'
2
+
3
+ describe Location do
4
+ it "should parse city" do
5
+ test_location("san francisco", "san francisco", nil, nil)
6
+ end
7
+
8
+ it "should parse city state abbreviation" do
9
+ test_location("san francisco ca", "san francisco", "CA", nil)
10
+ end
11
+
12
+ it "should parse city state full" do
13
+ test_location("san francisco california", "san francisco", "CA", nil)
14
+ end
15
+
16
+ it "should parse city comma state" do
17
+ test_location("san francisco, ca", "san francisco", "CA", nil)
18
+ end
19
+
20
+ it "should parse city state zip" do
21
+ test_location("san francisco ca 94114-1212", "san francisco", "CA", '94114-1212')
22
+ end
23
+
24
+ it "should parse city comma state zip" do
25
+ test_location("san francisco, ca 94114-1212", "san francisco", "CA", '94114-1212')
26
+ end
27
+
28
+ it "should parse city zip" do
29
+ test_location("san francisco 94114-1212", "san francisco", nil, '94114-1212')
30
+ end
31
+
32
+ it "should parse state zip" do
33
+ test_location("ca 94114-1212", nil, "CA", '94114-1212')
34
+ end
35
+
36
+ it "should parse state" do
37
+ test_location("california", nil, "CA", nil)
38
+ end
39
+
40
+ it "should parse zip" do
41
+ test_location("94114-1212", nil, nil, '94114-1212')
42
+ end
43
+
44
+ private
45
+
46
+ def test_location(str, city, state, zip)
47
+ l = Location.new(str)
48
+ l.city.should == city if l.city
49
+ l.state.to_s(:abbr).should == state.upcase if l.state
50
+ l.zip.to_s.should == zip if l.zip
51
+ end
52
+ end
@@ -0,0 +1,175 @@
1
+ require 'spec/helper'
2
+
3
+ describe PersonName do
4
+ it "should parse first_middle_last" do
5
+ name = PersonName.new "george quincy drake peabody", :first_middle_last
6
+ name.first.should == 'george'
7
+ name.middle.should == 'quincy drake'
8
+ name.last.should == 'peabody'
9
+ end
10
+
11
+ it "should parse last_first_middle" do
12
+ name = PersonName.new "peabody george quincy drake", :last_first_middle
13
+ name.first.should == 'george'
14
+ name.middle.should == 'quincy drake'
15
+ name.last.should == 'peabody'
16
+ end
17
+
18
+ it "should parse last only in first_middle_last format" do
19
+ name = PersonName.new "peabody", :first_middle_last
20
+ name.first.should == nil
21
+ name.middle.should == nil
22
+ name.last.should == 'peabody'
23
+ end
24
+
25
+ it "should parse last only in last_first_middle format" do
26
+ name = PersonName.new "peabody", :last_first_middle
27
+ name.first.should == nil
28
+ name.middle.should == nil
29
+ name.last.should == 'peabody'
30
+ end
31
+
32
+ it "should parse first middle last suffix" do
33
+ name = PersonName.new "george f. peabody jr"
34
+ name.first.should == 'george'
35
+ name.middle.should == 'f.'
36
+ name.last.should == 'peabody'
37
+ name.suffix.should == 'jr'
38
+ end
39
+
40
+ it "should parse last suffix first middle" do
41
+ name = PersonName.new "peabody jr george f.", :last_first_middle
42
+ name.first.should == 'george'
43
+ name.middle.should == 'f.'
44
+ name.last.should == 'peabody'
45
+ name.suffix.should == 'jr'
46
+ end
47
+
48
+ it "should default to first_middle_last" do
49
+ name = PersonName.new "george quincy drake peabody"
50
+ name.first.should == 'george'
51
+ name.middle.should == 'quincy drake'
52
+ name.last.should == 'peabody'
53
+ end
54
+
55
+ it "should ignore spaces and commas" do
56
+ name = PersonName.new " peabody,george quincy drake ", :last_first_middle
57
+ name.first.should == 'george'
58
+ name.middle.should == 'quincy drake'
59
+ name.last.should == 'peabody'
60
+ end
61
+
62
+ it "should strip periods from initials" do
63
+ name = PersonName.new "george f. peabody", :first_middle_last
64
+ name.first.should == 'george'
65
+ name.middle.should == 'f.'
66
+ name.middle_i == 'f'
67
+ name.last.should == 'peabody'
68
+ end
69
+
70
+ it "should format for :first" do
71
+ name = PersonName.new "george quincy peabody"
72
+ name.to_s(:first).should == 'george'
73
+ end
74
+
75
+ it "should format for :middle" do
76
+ name = PersonName.new "george quincy peabody"
77
+ name.to_s(:middle).should == 'quincy'
78
+ end
79
+
80
+ it "should format for :last" do
81
+ name = PersonName.new "george quincy peabody"
82
+ name.to_s(:last).should == 'peabody'
83
+ end
84
+
85
+ it "should format for :full" do
86
+ name = PersonName.new "george quincy peabody"
87
+ name.to_s(:full).should == 'george quincy peabody'
88
+ end
89
+
90
+ it "should format for :full_reverse" do
91
+ name = PersonName.new "george quincy peabody jr."
92
+ name.to_s(:full_reverse).should == 'peabody george quincy jr'
93
+ end
94
+
95
+ it "should format for :first_space_last" do
96
+ name = PersonName.new "george quincy peabody"
97
+ name.to_s(:first_space_last).should == 'george peabody'
98
+ end
99
+
100
+ it "should format for :last_space_first" do
101
+ name = PersonName.new "george quincy peabody"
102
+ name.to_s(:last_space_first).should == 'peabody george'
103
+ end
104
+
105
+ it "should format for :last_comma_first" do
106
+ name = PersonName.new "george quincy peabody"
107
+ name.to_s(:last_comma_first).should == 'peabody,george'
108
+ end
109
+
110
+ it "should format for :last_comma_space_first" do
111
+ name = PersonName.new "george quincy peabody"
112
+ name.to_s(:last_comma_space_first).should == 'peabody, george'
113
+ end
114
+
115
+ it "should handle missing parts when formatting" do
116
+ PersonName.new('peabody').to_s(:last_comma_space_first).should == 'peabody'
117
+ end
118
+
119
+ it "should respond to individual accessors" do
120
+ name = PersonName.new "george quincy peabody"
121
+ name.first.should == 'george'
122
+ name.first_i.should == 'g'
123
+ name.middle.should == 'quincy'
124
+ name.middle_i.should == 'q'
125
+ name.last.should == 'peabody'
126
+ name.last_i.should == 'p'
127
+ end
128
+
129
+ it "should recognize exact equality" do
130
+ PersonName.new("george quincy peabody").should be_eql(PersonName.new("george quincy peabody"))
131
+ end
132
+
133
+ it "should ignore case for equality" do
134
+ PersonName.new("george quincy peabody").should be_eql(PersonName.new("george quincy PEABODY"))
135
+ end
136
+
137
+ it "should recognize subset equality" do
138
+ PersonName.new("george quincy peabody").should be_eql(PersonName.new("george peabody"))
139
+ end
140
+
141
+ it "should parse mc donald for first_middle_last" do
142
+ name = PersonName.new "george quincy drake mc donald", :first_middle_last
143
+ name.first.should == 'george'
144
+ name.middle.should == 'quincy drake'
145
+ name.last.should == 'mcdonald'
146
+ end
147
+
148
+ it "should parse mc donald for last_first_middle" do
149
+ name = PersonName.new "mc donald george quincy drake", :last_first_middle
150
+ name.first.should == 'george'
151
+ name.middle.should == 'quincy drake'
152
+ name.last.should == 'mcdonald'
153
+ end
154
+
155
+ it "should normalize suffixes" do
156
+ name = PersonName.new "george quincy peabody jr"
157
+ name2 = PersonName.new "peabody jr., george quincy", :last_first_middle
158
+ name.should be_eql(name2)
159
+ end
160
+
161
+ it "should support auto detect formatting for first_middle_last" do
162
+ name = PersonName.new "george f. peabody jr"
163
+ name.first.should == 'george'
164
+ name.middle.should == 'f.'
165
+ name.last.should == 'peabody'
166
+ name.suffix.should == 'jr'
167
+ end
168
+
169
+ it "should support auto detect formatting for last_first_middle" do
170
+ name = PersonName.new "peabody, george quincy drake"
171
+ name.first.should == 'george'
172
+ name.middle.should == 'quincy drake'
173
+ name.last.should == 'peabody'
174
+ end
175
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec/helper'
2
+
3
+ describe PhoneNumber do
4
+ it "should parse ten digits" do
5
+ phone = PhoneNumber.new '4045551212'
6
+ phone.area_code.should == '404'
7
+ phone.number.should == '5551212'
8
+ phone.exchange.should == '555'
9
+ phone.suffix.should == '1212'
10
+ end
11
+
12
+ it "should parse eleven digits" do
13
+ phone = PhoneNumber.new '14045551212'
14
+ phone.area_code.should == '404'
15
+ phone.number.should == '5551212'
16
+ phone.exchange.should == '555'
17
+ phone.suffix.should == '1212'
18
+ end
19
+
20
+ it "should ignore certain characters" do
21
+ phone = PhoneNumber.new '1 (404) 555-1212'
22
+ phone.area_code.should == '404'
23
+ phone.number.should == '5551212'
24
+ phone.exchange.should == '555'
25
+ phone.suffix.should == '1212'
26
+ end
27
+
28
+ it "should support international format, at least for US numbers, for now" do
29
+ phone = PhoneNumber.new '+1 404 555-1212'
30
+ phone.area_code.should == '404'
31
+ phone.number.should == '5551212'
32
+ phone.exchange.should == '555'
33
+ phone.suffix.should == '1212'
34
+ end
35
+
36
+ it "should throw exception on unsupported parse format" do
37
+ lambda { PhoneNumber.new('40455512') }.should raise_error
38
+ end
39
+
40
+ it "should format :full_digits" do
41
+ PhoneNumber.new('14045551212').to_s(:full_digits).should == '14045551212'
42
+ end
43
+
44
+ it "should format :local_digits" do
45
+ PhoneNumber.new('14045551212').to_s(:local_digits).should == '5551212'
46
+ end
47
+
48
+ it "should format :full_formatted" do
49
+ PhoneNumber.new('14045551212').to_s(:full_formatted).should == '1 (404) 555-1212'
50
+ end
51
+
52
+ it "should format :local_formatted" do
53
+ PhoneNumber.new('14045551212').to_s(:local_formatted).should == '555-1212'
54
+ end
55
+
56
+ it "should throw exception on unsupported to_sformat" do
57
+ lambda { PhoneNumber.new('14045551212').to_s(:bogus) }.should raise_error
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec/helper'
2
+
3
+ describe State do
4
+ it "should parse abbreviation" do
5
+ state = State.new 'ga'
6
+ state.sym.should == :ga
7
+ end
8
+
9
+ it "should parse full" do
10
+ state = State.new 'georgia'
11
+ state.sym.should == :ga
12
+ end
13
+
14
+ it "should throw exception on unsupported state" do
15
+ lambda { State.new('foo') }.should raise_error
16
+ end
17
+
18
+ it "should format :abbr" do
19
+ State.new('ga').to_s(:abbr).should == 'GA'
20
+ end
21
+
22
+ it "should format :full" do
23
+ State.new('ga').to_s(:full).should == 'Georgia'
24
+ end
25
+
26
+ it "should throw exception on unsupported to_s format" do
27
+ lambda { State.new('ga').to_s(:bogus) }.should raise_error
28
+ end
29
+ end