@@ -12,8 +12,8 @@ def first_address(string)
12
12
def find_addresses ( string )
13
13
string . scan ( ADDRESS_PATTERN ) . collect { |a | hashify_results ( a ) } . compact
14
14
end
15
-
16
- # Pass it a block that recieves 2 parameters:
15
+
16
+ # Pass it a block that recieves 2 parameters:
17
17
# address hash
18
18
# matched address string ($&)
19
19
# Whatever your block returns will be used for the substition.
@@ -33,9 +33,9 @@ def replace_addresses(string)
33
33
useful_address? ( hash ) ? yield ( hash , $&) : match
34
34
end
35
35
end
36
-
36
+
37
37
private
38
-
38
+
39
39
def hashify_results ( matches )
40
40
return nil if matches . nil?
41
41
result = { }
@@ -46,16 +46,16 @@ def hashify_results(matches)
46
46
end
47
47
useful_address? ( result ) ? result : nil
48
48
end
49
-
49
+
50
50
def useful_address? ( hash )
51
- hash &&
51
+ hash &&
52
52
hash [ :street1 ] && ( hash [ :zip ] || hash [ :city ] && hash [ :state ] )
53
53
end
54
-
54
+
55
55
end
56
-
56
+
57
57
CAPTURE_MAP = [ :street1 , :street2 , :city , :state , :zip , :zip ]
58
-
58
+
59
59
STATES = <<-EOF
60
60
ALABAMA AL
61
61
ALASKA AK
@@ -117,9 +117,9 @@ def useful_address?(hash)
117
117
WISCONSIN WI
118
118
WYOMING WY
119
119
EOF
120
-
120
+
121
121
STATE_REGEX = STATES . split ( /\n / ) . collect { |n | n . scan ( /(\w .*\w )\s *([A-Z]{2})\s *$/ ) } . join ( "|" )
122
-
122
+
123
123
SECONDARY_UNIT_DESIGNATORS = <<-EOF
124
124
APARTMENT APT
125
125
BASEMENT BSMT
@@ -145,14 +145,14 @@ def useful_address?(hash)
145
145
UNIT UNIT
146
146
UPPER UPPR
147
147
EOF
148
-
148
+
149
149
SECONDARY_UNIT_DESIGNATORS_REGEX = SECONDARY_UNIT_DESIGNATORS . split ( /\n / ) . collect { |n | n . scan ( /(\w +)\s *(\w +)\s *$/ ) } . join ( "|" )
150
150
151
151
ADDRESS_PATTERN = /
152
152
(
153
153
\d + # A few numbers
154
154
\s +
155
- (?:[A-Za-z'.-]+\s ?){0,2} (?:[A-Za-z'.-]+) # Followed by a street name
155
+ (?:[A-Za-z'.-]+\s ?){1,5} # Followed by a street name
156
156
)
157
157
\s * ,? \s * # a comma, optionally
158
158
(
@@ -168,9 +168,9 @@ def useful_address?(hash)
168
168
\b (#{ STATE_REGEX } )\b # state
169
169
\s * ,? \s * # a comma, optionally
170
170
(\d {5})? # a zip code, optionally
171
- )
171
+ )
172
172
| # or, instead of city and state
173
173
(\d {5})? # a lone zip code will do
174
174
)
175
175
/xi
176
- end
176
+ end
0 commit comments