dburkes-people_places_things 1.3.0 → 2.0.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 -5
- data/VERSION +1 -1
- data/lib/people_places_things/location/location.rb +2 -2
- data/lib/people_places_things/person_name/person_name.rb +32 -70
- data/lib/people_places_things/phone_number/phone_number.rb +12 -30
- data/lib/people_places_things/state/state.rb +7 -21
- data/lib/people_places_things/street_address/street_address.rb +17 -34
- data/lib/people_places_things/zip_code/zip_code.rb +7 -34
- data/people_places_things.gemspec +1 -1
- data/spec/person_name_spec.rb +30 -37
- data/spec/phone_number_spec.rb +10 -10
- data/spec/state_spec.rb +6 -6
- data/spec/street_address_spec.rb +17 -17
- data/spec/zip_code_spec.rb +7 -11
- metadata +1 -1
data/README.textile
CHANGED
@@ -7,7 +7,7 @@ h2. Using PeoplePlacesThings
|
|
7
7
|
<pre><code>
|
8
8
|
require 'people_places_things'
|
9
9
|
|
10
|
-
name = PersonName.
|
10
|
+
name = PersonName.new("george quincy harold peabody jr.")
|
11
11
|
name.first => "george"
|
12
12
|
name.first_i => "g"
|
13
13
|
name.last => "peabody"
|
@@ -16,10 +16,10 @@ h2. Using PeoplePlacesThings
|
|
16
16
|
name.to_s(:last_comma_first) => "peabody,george"
|
17
17
|
name.to_s(:full) => "george quincy peabody jr."
|
18
18
|
|
19
|
-
name2 = PersonName.
|
19
|
+
name2 = PersonName.new("peabody jr., george quincy harold", :last_first_middle)
|
20
20
|
name.eql?(name2) => true
|
21
21
|
|
22
|
-
addr = StreetAddress.
|
22
|
+
addr = StreetAddress.new("204-b ne. 1st ave suite 4")
|
23
23
|
addr.number => "204-b"
|
24
24
|
addr.pre_direction => :northeast
|
25
25
|
addr.name => "1st"
|
@@ -32,7 +32,7 @@ h2. Using PeoplePlacesThings
|
|
32
32
|
ANSICounties.code_for(:state => 'ga', :county => 'fulton') => 13121
|
33
33
|
ANSICounties.data_for(13121) => { :state => 'GA', :county => 'FULTON' }
|
34
34
|
|
35
|
-
phone = PhoneNumber.
|
35
|
+
phone = PhoneNumber.new '14045551212'
|
36
36
|
phone.country_code => 1
|
37
37
|
phone.area_code => '404'
|
38
38
|
phone.number => '5551212'
|
@@ -40,10 +40,20 @@ h2. Using PeoplePlacesThings
|
|
40
40
|
phone.suffix => '1212'
|
41
41
|
phone.to_s => '1 (404) 555-1212'
|
42
42
|
|
43
|
-
zip = ZipCode.
|
43
|
+
zip = ZipCode.new '30306-3522'
|
44
44
|
zip.base => '30306'
|
45
45
|
zip.plus_four => '3522'
|
46
46
|
zip.to_s => '30306-3522'
|
47
|
+
|
48
|
+
state = State.new 'california'
|
49
|
+
state.sym => :ca
|
50
|
+
state.to_s => 'California'
|
51
|
+
state.to_s(:abbr) => 'CA'
|
52
|
+
|
53
|
+
location = Location.new 'san francisco, ca 94114-1222'
|
54
|
+
location.city => 'san francisco'
|
55
|
+
location.state.to_s => 'California'
|
56
|
+
location.zip.base => '94114'
|
47
57
|
</code></pre>
|
48
58
|
|
49
59
|
h2. Data source
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0
|
@@ -8,12 +8,12 @@ class Location
|
|
8
8
|
|
9
9
|
# try to parse last token as zip
|
10
10
|
#
|
11
|
-
self.zip = ZipCode.
|
11
|
+
self.zip = ZipCode.new(tokens.last) rescue nil
|
12
12
|
tokens = tokens.slice(0..-2) if self.zip
|
13
13
|
|
14
14
|
# try to parse last token as state
|
15
15
|
#
|
16
|
-
self.state = State.
|
16
|
+
self.state = State.new(tokens.last) rescue nil
|
17
17
|
tokens = tokens.slice(0..-2) if self.state
|
18
18
|
|
19
19
|
# remainder must be city
|
@@ -1,140 +1,103 @@
|
|
1
1
|
class PersonName
|
2
|
-
attr_accessor :
|
3
|
-
attr_accessor :raw
|
2
|
+
attr_accessor :first, :middle, :last, :suffix, :raw
|
4
3
|
|
5
|
-
def initialize(
|
6
|
-
@parts = options.inject({}) do |parts, (k, v)|
|
7
|
-
sym_key = (k.to_sym rescue key) || key
|
8
|
-
parts[sym_key] = v if SUPPORTED_PARTS.include?(sym_key)
|
9
|
-
parts
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.parse(string, fmt = :auto_detect)
|
4
|
+
def initialize(str, fmt = :auto_detect)
|
14
5
|
raise "Unsupported Format" if !PARSE_FORMATS.include?(fmt)
|
15
6
|
|
16
7
|
if fmt == :auto_detect
|
17
|
-
fmt =
|
8
|
+
fmt = str.include?(',') ? :last_first_middle : :first_middle_last
|
18
9
|
end
|
19
10
|
|
20
|
-
|
21
|
-
|
22
|
-
parts =
|
11
|
+
self.raw = str
|
12
|
+
|
13
|
+
parts = str.split(/\s|,/).collect {|p| p.strip}.reject {|p| PersonName.blank?(p) || p == ',' }
|
23
14
|
|
24
15
|
if parts.size == 1
|
25
|
-
|
16
|
+
self.last = parts.first
|
26
17
|
else
|
27
18
|
case fmt
|
28
19
|
when :first_middle_last
|
29
20
|
if parts.size > 2 and SUPPORTED_SUFFIXES.detect {|s| s.casecmp(parts.last) == 0}
|
30
|
-
|
21
|
+
self.suffix = PersonName.normalize_suffix(parts.last)
|
31
22
|
parts.delete_at(parts.size - 1)
|
32
23
|
end
|
33
24
|
|
34
|
-
|
35
|
-
|
25
|
+
self.first = parts.first if parts.size > 0
|
26
|
+
self.last = parts.last if parts.size > 1
|
36
27
|
|
37
28
|
if parts.size > 2 && ODD_LAST_NAME_PREFIXES.detect {|s| s.casecmp(parts[-2]) == 0}
|
38
|
-
|
29
|
+
self.last = "#{parts[-2]}#{self.last}"
|
39
30
|
parts.delete_at(parts.size - 2)
|
40
31
|
end
|
41
32
|
|
42
|
-
|
33
|
+
self.middle = parts[1..(parts.size - 2)].join(' ') if parts.size > 2
|
43
34
|
|
44
35
|
when :last_first_middle
|
45
|
-
|
36
|
+
self.last = parts.first if parts.size > 0
|
46
37
|
|
47
|
-
if parts.size > 1 && ODD_LAST_NAME_PREFIXES.detect {|s| s.casecmp(
|
48
|
-
|
38
|
+
if parts.size > 1 && ODD_LAST_NAME_PREFIXES.detect {|s| s.casecmp(self.last) == 0}
|
39
|
+
self.last << parts[1]
|
49
40
|
parts.delete_at(1)
|
50
41
|
end
|
51
42
|
|
52
43
|
if parts.size > 2 and SUPPORTED_SUFFIXES.detect {|s| s.casecmp(parts[1]) == 0}
|
53
|
-
|
44
|
+
self.suffix = PersonName.normalize_suffix(parts[1])
|
54
45
|
parts.delete_at(1)
|
55
46
|
end
|
56
47
|
|
57
|
-
|
58
|
-
|
48
|
+
self.first = parts[1] if parts.size > 1
|
49
|
+
self.middle = parts[2..(parts.size - 1)].join(' ') if parts.size > 2
|
59
50
|
end
|
60
51
|
end
|
61
|
-
|
62
|
-
n = PersonName.new args
|
63
|
-
n.raw = string
|
64
|
-
n
|
65
52
|
end
|
66
53
|
|
67
|
-
def self.format(options, fmt=:full)
|
68
|
-
PersonName.new(options).to_s(fmt)
|
69
|
-
end
|
70
|
-
|
71
|
-
def [](which)
|
72
|
-
@parts[which]
|
73
|
-
end
|
74
|
-
|
75
54
|
def to_s(fmt = :full)
|
76
55
|
raise "Unsupported Format" if !OUTPUT_FORMATS.include?(fmt)
|
77
56
|
|
78
57
|
case fmt
|
79
58
|
when :first, :middle, :last
|
80
|
-
|
59
|
+
self.send(fmt)
|
81
60
|
|
82
61
|
when :full
|
83
|
-
[
|
62
|
+
[self.first, self.middle, self.last, self.suffix].compact.join(' ')
|
84
63
|
|
85
64
|
when :full_reverse
|
86
|
-
[
|
65
|
+
[self.last, self.first, self.middle, self.suffix].compact.join(' ')
|
87
66
|
|
88
67
|
when :first_space_last
|
89
|
-
[
|
68
|
+
[self.first, self.last].compact.join(' ')
|
90
69
|
|
91
70
|
when :last_space_first
|
92
|
-
[
|
71
|
+
[self.last, self.first].compact.join(' ')
|
93
72
|
|
94
73
|
when :last_comma_first
|
95
|
-
[
|
74
|
+
[self.last, self.first].compact.join(',')
|
96
75
|
|
97
76
|
when :last_comma_space_first
|
98
|
-
[(
|
77
|
+
[(self.first ? "#{self.last}," : self.last), self.first].compact.join(' ')
|
99
78
|
end
|
100
79
|
end
|
101
80
|
|
102
|
-
def first
|
103
|
-
@parts[:first]
|
104
|
-
end
|
105
|
-
|
106
81
|
def first_i
|
107
|
-
first[0,1] rescue nil
|
108
|
-
end
|
109
|
-
|
110
|
-
def middle
|
111
|
-
@parts[:middle]
|
82
|
+
self.first[0,1] rescue nil
|
112
83
|
end
|
113
84
|
|
114
85
|
def middle_i
|
115
|
-
middle[0,1] rescue nil
|
116
|
-
end
|
117
|
-
|
118
|
-
def last
|
119
|
-
@parts[:last]
|
86
|
+
self.middle[0,1] rescue nil
|
120
87
|
end
|
121
88
|
|
122
89
|
def last_i
|
123
|
-
last[0,1] rescue nil
|
124
|
-
end
|
125
|
-
|
126
|
-
def suffix
|
127
|
-
@parts[:suffix]
|
90
|
+
self.last[0,1] rescue nil
|
128
91
|
end
|
129
92
|
|
130
93
|
def eql?(other, initials_only=false)
|
131
94
|
if other.is_a?(PersonName)
|
132
|
-
|
95
|
+
[:first, :middle, :last].all? do |k|
|
133
96
|
msg = (k != :last && initials_only) ? "#{k}_i" : k
|
134
|
-
self.send(msg)
|
97
|
+
me = self.send(msg)
|
98
|
+
them = other.send(msg)
|
99
|
+
me && them ? me.casecmp(them) == 0 : true
|
135
100
|
end
|
136
|
-
else
|
137
|
-
false
|
138
101
|
end
|
139
102
|
end
|
140
103
|
|
@@ -151,7 +114,6 @@ class PersonName
|
|
151
114
|
suffix.match(/\w+/)[0] rescue suffix
|
152
115
|
end
|
153
116
|
|
154
|
-
SUPPORTED_PARTS = [:first,:middle,:last, :suffix]
|
155
117
|
SUPPORTED_SUFFIXES = %w(II III IV V JR JR. SR SR.)
|
156
118
|
ODD_LAST_NAME_PREFIXES = %w(MC ST ST.)
|
157
119
|
end
|
@@ -1,45 +1,27 @@
|
|
1
1
|
class PhoneNumber
|
2
|
-
|
3
|
-
@@SUPPORTED_PARTS.each {|attr| attr_accessor attr}
|
4
|
-
attr_accessor :raw
|
2
|
+
attr_accessor :country_code, :area_code, :number, :exchange, :suffix, :raw
|
5
3
|
|
6
|
-
def initialize(
|
7
|
-
|
8
|
-
send("#{k}=", parts[k]) if @@SUPPORTED_PARTS.include?(k)
|
9
|
-
end
|
10
|
-
|
11
|
-
raise "Unsupported Format" if !self.exchange || !self.suffix
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.parse(string)
|
15
|
-
extract = string.strip.match(/^([-+()\d ]+)$/)[0].gsub(/[^\d]/, '') rescue nil
|
4
|
+
def initialize(str)
|
5
|
+
extract = str.strip.match(/^([-+()\d ]+)$/)[0].gsub(/[^\d]/, '') rescue nil
|
16
6
|
raise "Unsupported Format" if !extract || extract.length < 10 || extract.length > 11
|
17
7
|
|
18
|
-
parts = {}
|
19
|
-
|
20
8
|
if extract.length == 11
|
21
|
-
|
9
|
+
self.country_code = extract.slice!(0..0)
|
22
10
|
else
|
23
|
-
|
11
|
+
self.country_code = '1'
|
24
12
|
end
|
25
13
|
|
26
|
-
raise "Unsupported Format" if
|
14
|
+
raise "Unsupported Format" if self.country_code != '1'
|
27
15
|
|
28
|
-
|
16
|
+
self.area_code = extract.slice!(0..2)
|
29
17
|
|
30
|
-
|
18
|
+
self.number = extract.dup
|
31
19
|
|
32
|
-
|
20
|
+
self.exchange = extract.slice!(0..2)
|
33
21
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
ret.raw = string
|
38
|
-
ret
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.format(parts, fmt)
|
42
|
-
PhoneNumber.new(parts).to_s(fmt)
|
22
|
+
self.suffix = extract
|
23
|
+
|
24
|
+
raise "Unsupported Format" if !self.exchange || !self.suffix
|
43
25
|
end
|
44
26
|
|
45
27
|
def to_s(fmt = :full_formatted)
|
@@ -1,31 +1,17 @@
|
|
1
1
|
class State
|
2
|
-
attr_accessor :raw
|
3
|
-
attr_accessor :sym
|
2
|
+
attr_accessor :sym, :raw
|
4
3
|
|
5
|
-
def initialize(
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.parse(string)
|
11
|
-
token = string.strip.downcase
|
12
|
-
sym = nil
|
4
|
+
def initialize(str)
|
5
|
+
self.raw = str
|
6
|
+
token = str.strip.downcase
|
13
7
|
|
14
8
|
if FORWARD.has_key?(token.to_sym)
|
15
|
-
sym = token.to_sym
|
9
|
+
self.sym = token.to_sym
|
16
10
|
elsif REVERSE.has_key?(token)
|
17
|
-
sym = REVERSE[token]
|
11
|
+
self.sym = REVERSE[token]
|
18
12
|
end
|
19
13
|
|
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)
|
14
|
+
raise "Unsupported Format" if !self.sym
|
29
15
|
end
|
30
16
|
|
31
17
|
def to_s(fmt = :full)
|
@@ -1,33 +1,22 @@
|
|
1
1
|
class StreetAddress
|
2
|
-
|
3
|
-
@@SUPPORTED_PARTS.each {|attr| attr_accessor attr}
|
4
|
-
attr_accessor :raw
|
2
|
+
attr_accessor :number, :pre_direction, :name, :suffix, :post_direction, :unit_type, :unit, :raw
|
5
3
|
|
6
|
-
def initialize(
|
7
|
-
|
8
|
-
send("#{k}=", parts[k]) if @@SUPPORTED_PARTS.include?(k)
|
9
|
-
end
|
10
|
-
|
11
|
-
validate_parts
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.parse(string)
|
15
|
-
tokens = string.split(/[\s,]/).select {|s| !s.empty?}
|
16
|
-
parts = {}
|
4
|
+
def initialize(str)
|
5
|
+
tokens = str.split(/[\s,]/).select {|s| !s.empty?}
|
17
6
|
|
18
7
|
# Check the first token for leading numericality. If so, set number to the first token, and delete it
|
19
8
|
#
|
20
9
|
if tokens.first =~ /(^\d+.*)/
|
21
|
-
|
10
|
+
self.number = $1
|
22
11
|
tokens.shift
|
23
12
|
end
|
24
13
|
|
25
14
|
# If at least two tokens remain, check next-to-last token as unit type. If so, set unit_type and unit, and delete the tokens
|
26
15
|
#
|
27
16
|
if tokens.size > 1
|
28
|
-
|
29
|
-
if
|
30
|
-
|
17
|
+
self.unit_type = StreetAddress.find_token(tokens[-2], UNIT_TYPES)
|
18
|
+
if self.unit_type
|
19
|
+
self.unit = tokens[-1]
|
31
20
|
tokens.slice!(tokens.size - 2, 2)
|
32
21
|
end
|
33
22
|
end
|
@@ -35,8 +24,8 @@ class StreetAddress
|
|
35
24
|
# If at least one token remains, check last token for directionality. If so, set post_direction and delete the token
|
36
25
|
#
|
37
26
|
if tokens.size > 0
|
38
|
-
|
39
|
-
if
|
27
|
+
self.post_direction = StreetAddress.find_token(tokens[-1], DIRECTIONS)
|
28
|
+
if self.post_direction
|
40
29
|
post_direction_token = tokens[-1]
|
41
30
|
tokens.slice!(tokens.size - 1)
|
42
31
|
end
|
@@ -45,33 +34,27 @@ class StreetAddress
|
|
45
34
|
# If at least one token remains, check last token for suffix. If so, self set.suffix and delete the token
|
46
35
|
#
|
47
36
|
if tokens.size > 0
|
48
|
-
|
49
|
-
tokens.slice!(tokens.size - 1) if
|
37
|
+
self.suffix = StreetAddress.find_token(tokens[-1], SUFFIXES)
|
38
|
+
tokens.slice!(tokens.size - 1) if self.suffix
|
50
39
|
end
|
51
40
|
|
52
41
|
# If at least two tokens remain, check first for directionality. If so, set pre_direction and delete token
|
53
42
|
#
|
54
43
|
if tokens.size > 1
|
55
|
-
|
56
|
-
tokens.shift if
|
44
|
+
self.pre_direction = StreetAddress.find_token(tokens.first, DIRECTIONS)
|
45
|
+
tokens.shift if self.pre_direction
|
57
46
|
end
|
58
47
|
|
59
48
|
# if any tokens remain, set joined remaining tokens as name, otherwise, set name to post_direction, if set, and set post_direction to nil
|
60
49
|
#
|
61
50
|
if tokens.size > 0
|
62
|
-
|
51
|
+
self.name = tokens.join(' ')
|
63
52
|
else
|
64
|
-
|
65
|
-
|
53
|
+
self.name = post_direction_token
|
54
|
+
self.post_direction = nil
|
66
55
|
end
|
67
56
|
|
68
|
-
|
69
|
-
ret.raw = string
|
70
|
-
ret
|
71
|
-
end
|
72
|
-
|
73
|
-
def self.format(parts)
|
74
|
-
StreetAddress.new(parts).to_s
|
57
|
+
validate_parts
|
75
58
|
end
|
76
59
|
|
77
60
|
def to_s
|
@@ -1,42 +1,15 @@
|
|
1
1
|
class ZipCode
|
2
|
-
|
3
|
-
@@SUPPORTED_PARTS.each {|attr| attr_accessor attr}
|
4
|
-
attr_accessor :raw
|
2
|
+
attr_accessor :base, :plus_four, :raw
|
5
3
|
|
6
|
-
def initialize(
|
7
|
-
|
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
|
4
|
+
def initialize(str)
|
5
|
+
tokens = str.strip.match(/^(\d{5})(-\d{4})?$/)[0].split('-') rescue nil
|
14
6
|
raise "Unsupported Format" if !tokens
|
15
7
|
|
16
|
-
|
17
|
-
|
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)
|
8
|
+
self.base = tokens.first
|
9
|
+
self.plus_four = tokens[1] rescue nil
|
27
10
|
end
|
28
11
|
|
29
|
-
def to_s
|
30
|
-
|
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
|
12
|
+
def to_s
|
13
|
+
[self.base, self.plus_four].compact.join('-')
|
39
14
|
end
|
40
|
-
|
41
|
-
OUTPUT_FORMATS = [:base, :plus_four]
|
42
15
|
end
|
data/spec/person_name_spec.rb
CHANGED
@@ -2,35 +2,35 @@ require 'spec/helper'
|
|
2
2
|
|
3
3
|
describe PersonName do
|
4
4
|
it "should parse first_middle_last" do
|
5
|
-
name = PersonName.
|
5
|
+
name = PersonName.new "george quincy drake peabody", :first_middle_last
|
6
6
|
name.first.should == 'george'
|
7
7
|
name.middle.should == 'quincy drake'
|
8
8
|
name.last.should == 'peabody'
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should parse last_first_middle" do
|
12
|
-
name = PersonName.
|
12
|
+
name = PersonName.new "peabody george quincy drake", :last_first_middle
|
13
13
|
name.first.should == 'george'
|
14
14
|
name.middle.should == 'quincy drake'
|
15
15
|
name.last.should == 'peabody'
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should parse last only in first_middle_last format" do
|
19
|
-
name = PersonName.
|
19
|
+
name = PersonName.new "peabody", :first_middle_last
|
20
20
|
name.first.should == nil
|
21
21
|
name.middle.should == nil
|
22
22
|
name.last.should == 'peabody'
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should parse last only in last_first_middle format" do
|
26
|
-
name = PersonName.
|
26
|
+
name = PersonName.new "peabody", :last_first_middle
|
27
27
|
name.first.should == nil
|
28
28
|
name.middle.should == nil
|
29
29
|
name.last.should == 'peabody'
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should parse first middle last suffix" do
|
33
|
-
name = PersonName.
|
33
|
+
name = PersonName.new "george f. peabody jr"
|
34
34
|
name.first.should == 'george'
|
35
35
|
name.middle.should == 'f.'
|
36
36
|
name.last.should == 'peabody'
|
@@ -38,7 +38,7 @@ describe PersonName do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should parse last suffix first middle" do
|
41
|
-
name = PersonName.
|
41
|
+
name = PersonName.new "peabody jr george f.", :last_first_middle
|
42
42
|
name.first.should == 'george'
|
43
43
|
name.middle.should == 'f.'
|
44
44
|
name.last.should == 'peabody'
|
@@ -46,21 +46,21 @@ describe PersonName do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should default to first_middle_last" do
|
49
|
-
name = PersonName.
|
49
|
+
name = PersonName.new "george quincy drake peabody"
|
50
50
|
name.first.should == 'george'
|
51
51
|
name.middle.should == 'quincy drake'
|
52
52
|
name.last.should == 'peabody'
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should ignore spaces and commas" do
|
56
|
-
name = PersonName.
|
56
|
+
name = PersonName.new " peabody,george quincy drake ", :last_first_middle
|
57
57
|
name.first.should == 'george'
|
58
58
|
name.middle.should == 'quincy drake'
|
59
59
|
name.last.should == 'peabody'
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should strip periods from initials" do
|
63
|
-
name = PersonName.
|
63
|
+
name = PersonName.new "george f. peabody", :first_middle_last
|
64
64
|
name.first.should == 'george'
|
65
65
|
name.middle.should == 'f.'
|
66
66
|
name.middle_i == 'f'
|
@@ -68,63 +68,56 @@ describe PersonName do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
it "should format for :first" do
|
71
|
-
name = PersonName.new
|
71
|
+
name = PersonName.new "george quincy peabody"
|
72
72
|
name.to_s(:first).should == 'george'
|
73
73
|
end
|
74
74
|
|
75
75
|
it "should format for :middle" do
|
76
|
-
name = PersonName.new
|
76
|
+
name = PersonName.new "george quincy peabody"
|
77
77
|
name.to_s(:middle).should == 'quincy'
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should format for :last" do
|
81
|
-
name = PersonName.new
|
81
|
+
name = PersonName.new "george quincy peabody"
|
82
82
|
name.to_s(:last).should == 'peabody'
|
83
83
|
end
|
84
84
|
|
85
85
|
it "should format for :full" do
|
86
|
-
name = PersonName.new
|
86
|
+
name = PersonName.new "george quincy peabody"
|
87
87
|
name.to_s(:full).should == 'george quincy peabody'
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should format for :full_reverse" do
|
91
|
-
name = PersonName.new
|
92
|
-
name.to_s(:full_reverse).should == 'peabody george quincy jr
|
91
|
+
name = PersonName.new "george quincy peabody jr."
|
92
|
+
name.to_s(:full_reverse).should == 'peabody george quincy jr'
|
93
93
|
end
|
94
94
|
|
95
95
|
it "should format for :first_space_last" do
|
96
|
-
name = PersonName.new
|
96
|
+
name = PersonName.new "george quincy peabody"
|
97
97
|
name.to_s(:first_space_last).should == 'george peabody'
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should format for :last_space_first" do
|
101
|
-
name = PersonName.new
|
101
|
+
name = PersonName.new "george quincy peabody"
|
102
102
|
name.to_s(:last_space_first).should == 'peabody george'
|
103
103
|
end
|
104
104
|
|
105
105
|
it "should format for :last_comma_first" do
|
106
|
-
name = PersonName.new
|
106
|
+
name = PersonName.new "george quincy peabody"
|
107
107
|
name.to_s(:last_comma_first).should == 'peabody,george'
|
108
108
|
end
|
109
109
|
|
110
110
|
it "should format for :last_comma_space_first" do
|
111
|
-
name = PersonName.new
|
111
|
+
name = PersonName.new "george quincy peabody"
|
112
112
|
name.to_s(:last_comma_space_first).should == 'peabody, george'
|
113
113
|
end
|
114
114
|
|
115
115
|
it "should handle missing parts when formatting" do
|
116
|
-
PersonName.new(
|
117
|
-
end
|
118
|
-
|
119
|
-
it "should respond to index method" do
|
120
|
-
name = PersonName.new :first => 'george', :middle => 'quincy', :last => 'peabody'
|
121
|
-
name[:first].should == 'george'
|
122
|
-
name[:middle].should == 'quincy'
|
123
|
-
name[:last].should == 'peabody'
|
116
|
+
PersonName.new('peabody').to_s(:last_comma_space_first).should == 'peabody'
|
124
117
|
end
|
125
118
|
|
126
119
|
it "should respond to individual accessors" do
|
127
|
-
name = PersonName.new
|
120
|
+
name = PersonName.new "george quincy peabody"
|
128
121
|
name.first.should == 'george'
|
129
122
|
name.first_i.should == 'g'
|
130
123
|
name.middle.should == 'quincy'
|
@@ -134,39 +127,39 @@ describe PersonName do
|
|
134
127
|
end
|
135
128
|
|
136
129
|
it "should recognize exact equality" do
|
137
|
-
PersonName.
|
130
|
+
PersonName.new("george quincy peabody").should be_eql(PersonName.new("george quincy peabody"))
|
138
131
|
end
|
139
132
|
|
140
133
|
it "should ignore case for equality" do
|
141
|
-
PersonName.
|
134
|
+
PersonName.new("george quincy peabody").should be_eql(PersonName.new("george quincy PEABODY"))
|
142
135
|
end
|
143
136
|
|
144
137
|
it "should recognize subset equality" do
|
145
|
-
PersonName.
|
138
|
+
PersonName.new("george quincy peabody").should be_eql(PersonName.new("george peabody"))
|
146
139
|
end
|
147
140
|
|
148
141
|
it "should parse mc donald for first_middle_last" do
|
149
|
-
name = PersonName.
|
142
|
+
name = PersonName.new "george quincy drake mc donald", :first_middle_last
|
150
143
|
name.first.should == 'george'
|
151
144
|
name.middle.should == 'quincy drake'
|
152
145
|
name.last.should == 'mcdonald'
|
153
146
|
end
|
154
147
|
|
155
148
|
it "should parse mc donald for last_first_middle" do
|
156
|
-
name = PersonName.
|
149
|
+
name = PersonName.new "mc donald george quincy drake", :last_first_middle
|
157
150
|
name.first.should == 'george'
|
158
151
|
name.middle.should == 'quincy drake'
|
159
152
|
name.last.should == 'mcdonald'
|
160
153
|
end
|
161
154
|
|
162
155
|
it "should normalize suffixes" do
|
163
|
-
name = PersonName.
|
164
|
-
name2 = PersonName.
|
156
|
+
name = PersonName.new "george quincy peabody jr"
|
157
|
+
name2 = PersonName.new "peabody jr., george quincy", :last_first_middle
|
165
158
|
name.should be_eql(name2)
|
166
159
|
end
|
167
160
|
|
168
161
|
it "should support auto detect formatting for first_middle_last" do
|
169
|
-
name = PersonName.
|
162
|
+
name = PersonName.new "george f. peabody jr"
|
170
163
|
name.first.should == 'george'
|
171
164
|
name.middle.should == 'f.'
|
172
165
|
name.last.should == 'peabody'
|
@@ -174,7 +167,7 @@ describe PersonName do
|
|
174
167
|
end
|
175
168
|
|
176
169
|
it "should support auto detect formatting for last_first_middle" do
|
177
|
-
name = PersonName.
|
170
|
+
name = PersonName.new "peabody, george quincy drake"
|
178
171
|
name.first.should == 'george'
|
179
172
|
name.middle.should == 'quincy drake'
|
180
173
|
name.last.should == 'peabody'
|
data/spec/phone_number_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec/helper'
|
|
2
2
|
|
3
3
|
describe PhoneNumber do
|
4
4
|
it "should parse ten digits" do
|
5
|
-
phone = PhoneNumber.
|
5
|
+
phone = PhoneNumber.new '4045551212'
|
6
6
|
phone.area_code.should == '404'
|
7
7
|
phone.number.should == '5551212'
|
8
8
|
phone.exchange.should == '555'
|
@@ -10,7 +10,7 @@ describe PhoneNumber do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should parse eleven digits" do
|
13
|
-
phone = PhoneNumber.
|
13
|
+
phone = PhoneNumber.new '14045551212'
|
14
14
|
phone.area_code.should == '404'
|
15
15
|
phone.number.should == '5551212'
|
16
16
|
phone.exchange.should == '555'
|
@@ -18,7 +18,7 @@ describe PhoneNumber do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should ignore certain characters" do
|
21
|
-
phone = PhoneNumber.
|
21
|
+
phone = PhoneNumber.new '1 (404) 555-1212'
|
22
22
|
phone.area_code.should == '404'
|
23
23
|
phone.number.should == '5551212'
|
24
24
|
phone.exchange.should == '555'
|
@@ -26,7 +26,7 @@ describe PhoneNumber do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should support international format, at least for US numbers, for now" do
|
29
|
-
phone = PhoneNumber.
|
29
|
+
phone = PhoneNumber.new '+1 404 555-1212'
|
30
30
|
phone.area_code.should == '404'
|
31
31
|
phone.number.should == '5551212'
|
32
32
|
phone.exchange.should == '555'
|
@@ -34,26 +34,26 @@ describe PhoneNumber do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should throw exception on unsupported parse format" do
|
37
|
-
lambda { PhoneNumber.
|
37
|
+
lambda { PhoneNumber.new('40455512') }.should raise_error
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should format :full_digits" do
|
41
|
-
PhoneNumber.
|
41
|
+
PhoneNumber.new('14045551212').to_s(:full_digits).should == '14045551212'
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should format :local_digits" do
|
45
|
-
PhoneNumber.
|
45
|
+
PhoneNumber.new('14045551212').to_s(:local_digits).should == '5551212'
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should format :full_formatted" do
|
49
|
-
PhoneNumber.
|
49
|
+
PhoneNumber.new('14045551212').to_s(:full_formatted).should == '1 (404) 555-1212'
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should format :local_formatted" do
|
53
|
-
PhoneNumber.
|
53
|
+
PhoneNumber.new('14045551212').to_s(:local_formatted).should == '555-1212'
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should throw exception on unsupported to_sformat" do
|
57
|
-
lambda { PhoneNumber.
|
57
|
+
lambda { PhoneNumber.new('14045551212').to_s(:bogus) }.should raise_error
|
58
58
|
end
|
59
59
|
end
|
data/spec/state_spec.rb
CHANGED
@@ -2,28 +2,28 @@ require 'spec/helper'
|
|
2
2
|
|
3
3
|
describe State do
|
4
4
|
it "should parse abbreviation" do
|
5
|
-
state = State.
|
5
|
+
state = State.new 'ga'
|
6
6
|
state.sym.should == :ga
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should parse full" do
|
10
|
-
state = State.
|
10
|
+
state = State.new 'georgia'
|
11
11
|
state.sym.should == :ga
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should throw exception on unsupported state" do
|
15
|
-
lambda { State.
|
15
|
+
lambda { State.new('foo') }.should raise_error
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should format :abbr" do
|
19
|
-
State.
|
19
|
+
State.new('ga').to_s(:abbr).should == 'GA'
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should format :full" do
|
23
|
-
State.
|
23
|
+
State.new('ga').to_s(:full).should == 'Georgia'
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should throw exception on unsupported to_s format" do
|
27
|
-
lambda { State.
|
27
|
+
lambda { State.new('ga').to_s(:bogus) }.should raise_error
|
28
28
|
end
|
29
29
|
end
|
data/spec/street_address_spec.rb
CHANGED
@@ -2,21 +2,21 @@ require 'spec/helper'
|
|
2
2
|
|
3
3
|
describe StreetAddress do
|
4
4
|
it "should parse number street" do
|
5
|
-
addr = StreetAddress.
|
5
|
+
addr = StreetAddress.new "123 Main Street"
|
6
6
|
addr.number.should == '123'
|
7
7
|
addr.name.should == 'Main'
|
8
8
|
addr.suffix.should == :street
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should parse number with letter" do
|
12
|
-
addr = StreetAddress.
|
12
|
+
addr = StreetAddress.new "204-B Main Street"
|
13
13
|
addr.number.should == '204-B'
|
14
14
|
addr.name.should == 'Main'
|
15
15
|
addr.suffix.should == :street
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should parse pre direction" do
|
19
|
-
addr = StreetAddress.
|
19
|
+
addr = StreetAddress.new "123 E Main Street"
|
20
20
|
addr.number.should == '123'
|
21
21
|
addr.pre_direction.should == :east
|
22
22
|
addr.name.should == 'Main'
|
@@ -24,7 +24,7 @@ describe StreetAddress do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should parse post direction" do
|
27
|
-
addr = StreetAddress.
|
27
|
+
addr = StreetAddress.new "123 Main Street NE"
|
28
28
|
addr.number.should == '123'
|
29
29
|
addr.name.should == 'Main'
|
30
30
|
addr.suffix.should == :street
|
@@ -32,7 +32,7 @@ describe StreetAddress do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should parse pre and post direction" do
|
35
|
-
addr = StreetAddress.
|
35
|
+
addr = StreetAddress.new "123 E Main Street North"
|
36
36
|
addr.number.should == '123'
|
37
37
|
addr.pre_direction.should == :east
|
38
38
|
addr.name.should == 'Main'
|
@@ -41,7 +41,7 @@ describe StreetAddress do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it "should parse street names that look like directions" do
|
44
|
-
addr = StreetAddress.
|
44
|
+
addr = StreetAddress.new "123 E E St"
|
45
45
|
addr.number.should == '123'
|
46
46
|
addr.pre_direction.should == :east
|
47
47
|
addr.name.should == 'E'
|
@@ -49,7 +49,7 @@ describe StreetAddress do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should parse street names that look like directions, with post directions" do
|
52
|
-
addr = StreetAddress.
|
52
|
+
addr = StreetAddress.new "123 E E St NE"
|
53
53
|
addr.number.should == '123'
|
54
54
|
addr.pre_direction.should == :east
|
55
55
|
addr.name.should == 'E'
|
@@ -58,7 +58,7 @@ describe StreetAddress do
|
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should parse abbreviations" do
|
61
|
-
addr = StreetAddress.
|
61
|
+
addr = StreetAddress.new "123 e. 1st ave"
|
62
62
|
addr.number.should == '123'
|
63
63
|
addr.pre_direction.should == :east
|
64
64
|
addr.name.should == '1st'
|
@@ -66,7 +66,7 @@ describe StreetAddress do
|
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should parse suites" do
|
69
|
-
addr = StreetAddress.
|
69
|
+
addr = StreetAddress.new "123 E E St NE Ste 23"
|
70
70
|
addr.number.should == '123'
|
71
71
|
addr.pre_direction.should == :east
|
72
72
|
addr.name.should == 'E'
|
@@ -77,7 +77,7 @@ describe StreetAddress do
|
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should parse apartments" do
|
80
|
-
addr = StreetAddress.
|
80
|
+
addr = StreetAddress.new "123 E E St NE Apartment 4"
|
81
81
|
addr.number.should == '123'
|
82
82
|
addr.pre_direction.should == :east
|
83
83
|
addr.name.should == 'E'
|
@@ -88,7 +88,7 @@ describe StreetAddress do
|
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should parse numbers" do
|
91
|
-
addr = StreetAddress.
|
91
|
+
addr = StreetAddress.new '123 E E St NE # 5'
|
92
92
|
addr.number.should == '123'
|
93
93
|
addr.pre_direction.should == :east
|
94
94
|
addr.name.should == 'E'
|
@@ -99,34 +99,34 @@ describe StreetAddress do
|
|
99
99
|
end
|
100
100
|
|
101
101
|
it "should parse no number" do
|
102
|
-
addr = StreetAddress.
|
102
|
+
addr = StreetAddress.new "Westside Highway"
|
103
103
|
addr.number.should == nil
|
104
104
|
addr.name.should == 'Westside'
|
105
105
|
addr.suffix.should == :highway
|
106
106
|
end
|
107
107
|
|
108
108
|
it "should parse directional street with suffix" do
|
109
|
-
addr = StreetAddress.
|
109
|
+
addr = StreetAddress.new "12 north avenue"
|
110
110
|
addr.number.should == '12'
|
111
111
|
addr.name.should == 'north'
|
112
112
|
addr.suffix.should == :avenue
|
113
113
|
end
|
114
114
|
|
115
115
|
it "should parse directional street without suffix" do
|
116
|
-
addr = StreetAddress.
|
116
|
+
addr = StreetAddress.new "12 north"
|
117
117
|
addr.number.should == '12'
|
118
118
|
addr.name.should == 'north'
|
119
119
|
end
|
120
120
|
|
121
121
|
it "should parse directional street with postdir" do
|
122
|
-
addr = StreetAddress.
|
122
|
+
addr = StreetAddress.new "12 north w"
|
123
123
|
addr.number.should == '12'
|
124
124
|
addr.name.should == 'north'
|
125
125
|
addr.post_direction.should == :west
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should parse directional street with postdir and unit" do
|
129
|
-
addr = StreetAddress.
|
129
|
+
addr = StreetAddress.new "12 n sw apt. 2"
|
130
130
|
addr.number.should == '12'
|
131
131
|
addr.name.should == 'n'
|
132
132
|
addr.post_direction.should == :southwest
|
@@ -135,7 +135,7 @@ describe StreetAddress do
|
|
135
135
|
end
|
136
136
|
|
137
137
|
it "should handle commas" do
|
138
|
-
addr = StreetAddress.
|
138
|
+
addr = StreetAddress.new "123 E E St NE, suite 23"
|
139
139
|
addr.number.should == '123'
|
140
140
|
addr.pre_direction.should == :east
|
141
141
|
addr.name.should == 'E'
|
data/spec/zip_code_spec.rb
CHANGED
@@ -2,30 +2,26 @@ require 'spec/helper'
|
|
2
2
|
|
3
3
|
describe ZipCode do
|
4
4
|
it "should parse base" do
|
5
|
-
zip = ZipCode.
|
5
|
+
zip = ZipCode.new '30306'
|
6
6
|
zip.base.should == '30306'
|
7
7
|
zip.plus_four.should == nil
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should parse plus four" do
|
11
|
-
zip = ZipCode.
|
11
|
+
zip = ZipCode.new '30306-3522'
|
12
12
|
zip.base.should == '30306'
|
13
13
|
zip.plus_four.should == '3522'
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "should throw exception on unsupported parse format" do
|
17
|
-
lambda { ZipCode.
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should format :base" do
|
21
|
-
ZipCode.parse('30306-3522').to_s(:base).should == '30306'
|
17
|
+
lambda { ZipCode.new('303065344') }.should raise_error
|
22
18
|
end
|
23
19
|
|
24
|
-
it "should
|
25
|
-
ZipCode.
|
20
|
+
it "should convert to string" do
|
21
|
+
ZipCode.new('30306-3522').to_s.should == '30306-3522'
|
26
22
|
end
|
27
23
|
|
28
24
|
it "should throw exception on unsupported to_s format" do
|
29
|
-
lambda { ZipCode.
|
25
|
+
lambda { ZipCode.new('30306-3522').to_s(:bogus) }.should raise_error
|
30
26
|
end
|
31
27
|
end
|