dburkes-people_places_things 1.2.0 → 1.3.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/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.3.0
@@ -3,6 +3,8 @@ require File.join(File.dirname(__FILE__), 'people_places_things', '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
5
  require File.join(File.dirname(__FILE__), 'people_places_things', 'zip_code', 'zip_code')
6
+ require File.join(File.dirname(__FILE__), 'people_places_things', 'state', 'state')
7
+ require File.join(File.dirname(__FILE__), 'people_places_things', 'location', 'location')
6
8
 
7
9
  module PeoplePlacesThings
8
10
  VERSION = File.read('VERSION').chomp.strip rescue "Unknown"
@@ -0,0 +1,28 @@
1
+ class Location
2
+ attr_accessor :city, :state, :zip, :raw
3
+
4
+ def initialize(str)
5
+ self.raw = str
6
+
7
+ tokens = str.split(/\s|,/).collect {|t| t.strip}
8
+
9
+ # try to parse last token as zip
10
+ #
11
+ self.zip = ZipCode.parse(tokens.last) rescue nil
12
+ tokens = tokens.slice(0..-2) if self.zip
13
+
14
+ # try to parse last token as state
15
+ #
16
+ self.state = State.parse(tokens.last) rescue nil
17
+ tokens = tokens.slice(0..-2) if self.state
18
+
19
+ # remainder must be city
20
+ #
21
+ self.city = tokens.join(' ').strip
22
+ self.city = nil if self.city.empty?
23
+ end
24
+
25
+ def to_s
26
+ [[self.city, (self.state.to_s(:abbr) rescue nil)].compact.join(','), self.zip.to_s].compact.join(' ')
27
+ end
28
+ end
@@ -0,0 +1,95 @@
1
+ class State
2
+ attr_accessor :raw
3
+ attr_accessor :sym
4
+
5
+ def initialize(sym)
6
+ raise "Unsupported state" if !FORWARD.has_key?(sym)
7
+ self.sym = sym
8
+ end
9
+
10
+ def self.parse(string)
11
+ token = string.strip.downcase
12
+ sym = nil
13
+
14
+ if FORWARD.has_key?(token.to_sym)
15
+ sym = token.to_sym
16
+ elsif REVERSE.has_key?(token)
17
+ sym = REVERSE[token]
18
+ end
19
+
20
+ raise "Unsupported Format" if !sym
21
+
22
+ ret = State.new sym
23
+ ret.raw = string
24
+ ret
25
+ end
26
+
27
+ def self.format(sym, fmt)
28
+ State.new(sym).to_s(fmt)
29
+ end
30
+
31
+ def to_s(fmt = :full)
32
+ raise "Unsupported Format" if !OUTPUT_FORMATS.include?(fmt)
33
+ fmt == :full ? FORWARD[self.sym].capitalize : self.sym.to_s.upcase
34
+ end
35
+
36
+ OUTPUT_FORMATS = [:abbr, :full]
37
+
38
+ private
39
+
40
+ FORWARD = {
41
+ :al => "alabama",
42
+ :ak => "alaska",
43
+ :az => "arizona",
44
+ :ar => "arkansas",
45
+ :ca => "california",
46
+ :co => "colorado",
47
+ :ct => "connecticut",
48
+ :de => "delaware",
49
+ :dc => "district of columbia",
50
+ :fl => "florida",
51
+ :ga => "georgia",
52
+ :hi => "hawaii",
53
+ :id => "idaho",
54
+ :il => "illinois",
55
+ :in => "indiana",
56
+ :ia => "iowa",
57
+ :ks => "kansas",
58
+ :ky => "kentucky",
59
+ :la => "louisiana",
60
+ :me => "maine",
61
+ :md => "maryland",
62
+ :ma => "massachusetts",
63
+ :mi => "michigan",
64
+ :mn => "minnesota",
65
+ :ms => "mississippi",
66
+ :mo => "missouri",
67
+ :mt => "montana",
68
+ :ne => "nebraska",
69
+ :nv => "nevada",
70
+ :nh => "new hampshire",
71
+ :nj => "new jersey",
72
+ :nm => "new mexico",
73
+ :ny => "new york",
74
+ :nc => "north carolina",
75
+ :nd => "north dakota",
76
+ :oh => "ohio",
77
+ :ok => "oklahoma",
78
+ :or => "oregon",
79
+ :pa => "pennsylvania",
80
+ :ri => "Rhode island",
81
+ :sc => "south carolina",
82
+ :sd => "south dakota",
83
+ :tn => "tennessee",
84
+ :tx => "texas",
85
+ :ut => "utah",
86
+ :vt => "vermont",
87
+ :va => "virginia",
88
+ :wa => "washington",
89
+ :wv => "west virginia",
90
+ :wi => "wisconsin",
91
+ :wy => "wyoming",
92
+ }
93
+
94
+ REVERSE = FORWARD.inject({}) {|r, f| r[f[1]] = f[0]; r}
95
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{people_places_things}
8
- s.version = "1.2.0"
8
+ s.version = "1.3.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"]
12
- s.date = %q{2009-08-28}
12
+ s.date = %q{2009-08-31}
13
13
  s.description = %q{Parsers and formatters for person names, street addresses, city/state/zip, phone numbers, etc.}
14
14
  s.email = %q{dburkes@netable.com}
15
15
  s.extra_rdoc_files = [
@@ -25,15 +25,19 @@ Gem::Specification.new do |s|
25
25
  "lib/people_places_things/ansi_counties/data/data.yml",
26
26
  "lib/people_places_things/ansi_counties/data/process_data.rb",
27
27
  "lib/people_places_things/ansi_counties/data/raw.txt",
28
+ "lib/people_places_things/location/location.rb",
28
29
  "lib/people_places_things/person_name/person_name.rb",
29
30
  "lib/people_places_things/phone_number/phone_number.rb",
31
+ "lib/people_places_things/state/state.rb",
30
32
  "lib/people_places_things/street_address/street_address.rb",
31
33
  "lib/people_places_things/zip_code/zip_code.rb",
32
34
  "people_places_things.gemspec",
33
35
  "spec/ansi_counties_spec.rb",
34
36
  "spec/helper.rb",
37
+ "spec/location_spec.rb",
35
38
  "spec/person_name_spec.rb",
36
39
  "spec/phone_number_spec.rb",
40
+ "spec/state_spec.rb",
37
41
  "spec/street_address_spec.rb",
38
42
  "spec/zip_code_spec.rb"
39
43
  ]
@@ -46,8 +50,10 @@ Gem::Specification.new do |s|
46
50
  s.test_files = [
47
51
  "spec/ansi_counties_spec.rb",
48
52
  "spec/helper.rb",
53
+ "spec/location_spec.rb",
49
54
  "spec/person_name_spec.rb",
50
55
  "spec/phone_number_spec.rb",
56
+ "spec/state_spec.rb",
51
57
  "spec/street_address_spec.rb",
52
58
  "spec/zip_code_spec.rb"
53
59
  ]
@@ -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,29 @@
1
+ require 'spec/helper'
2
+
3
+ describe State do
4
+ it "should parse abbreviation" do
5
+ state = State.parse 'ga'
6
+ state.sym.should == :ga
7
+ end
8
+
9
+ it "should parse full" do
10
+ state = State.parse 'georgia'
11
+ state.sym.should == :ga
12
+ end
13
+
14
+ it "should throw exception on unsupported state" do
15
+ lambda { State.parse('foo') }.should raise_error
16
+ end
17
+
18
+ it "should format :abbr" do
19
+ State.parse('ga').to_s(:abbr).should == 'GA'
20
+ end
21
+
22
+ it "should format :full" do
23
+ State.parse('ga').to_s(:full).should == 'Georgia'
24
+ end
25
+
26
+ it "should throw exception on unsupported to_s format" do
27
+ lambda { State.parse('ga').to_s(:bogus) }.should raise_error
28
+ end
29
+ 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.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Burkes
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-28 00:00:00 -07:00
12
+ date: 2009-08-31 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,19 +31,24 @@ files:
31
31
  - lib/people_places_things/ansi_counties/data/data.yml
32
32
  - lib/people_places_things/ansi_counties/data/process_data.rb
33
33
  - lib/people_places_things/ansi_counties/data/raw.txt
34
+ - lib/people_places_things/location/location.rb
34
35
  - lib/people_places_things/person_name/person_name.rb
35
36
  - lib/people_places_things/phone_number/phone_number.rb
37
+ - lib/people_places_things/state/state.rb
36
38
  - lib/people_places_things/street_address/street_address.rb
37
39
  - lib/people_places_things/zip_code/zip_code.rb
38
40
  - people_places_things.gemspec
39
41
  - spec/ansi_counties_spec.rb
40
42
  - spec/helper.rb
43
+ - spec/location_spec.rb
41
44
  - spec/person_name_spec.rb
42
45
  - spec/phone_number_spec.rb
46
+ - spec/state_spec.rb
43
47
  - spec/street_address_spec.rb
44
48
  - spec/zip_code_spec.rb
45
49
  has_rdoc: true
46
50
  homepage: http://github.com/dburkes/people_places_things
51
+ licenses:
47
52
  post_install_message:
48
53
  rdoc_options:
49
54
  - --charset=UTF-8
@@ -64,14 +69,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
69
  requirements: []
65
70
 
66
71
  rubyforge_project:
67
- rubygems_version: 1.2.0
72
+ rubygems_version: 1.3.5
68
73
  signing_key:
69
74
  specification_version: 2
70
75
  summary: Parsers and formatters for person names, street addresses, city/state/zip, phone numbers, etc.
71
76
  test_files:
72
77
  - spec/ansi_counties_spec.rb
73
78
  - spec/helper.rb
79
+ - spec/location_spec.rb
74
80
  - spec/person_name_spec.rb
75
81
  - spec/phone_number_spec.rb
82
+ - spec/state_spec.rb
76
83
  - spec/street_address_spec.rb
77
84
  - spec/zip_code_spec.rb