4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/wolf_core/application/barton/parsing.rb', line 4
def split_address(address_string)
address_string ||= ''
address = { street: nil, city: nil, state: nil, zip: nil }
city_and_state_regex_found = false
city_and_state_regex = /\b([A-Za-z\s]+),\s*([A-Za-z]{2})\b/
zip_regex = /\b\d{5}(?:-\d{4})?\b/
street_regex = /\A([^,]+)/
state_regex = /\b[A-Za-z]{2}\b/
city_regex = /\b[a-zA-Z0-9\s]{2,}\b/
if match_data = address_string.match(city_and_state_regex)
address[:city] = match_data[1].strip
address[:state] = match_data[2].strip
address_string.sub!(city_and_state_regex, '')
city_and_state_regex_found = true
end
if zip_match = address_string.match(zip_regex)
address[:zip] = zip_match[0].strip
address_string.sub!(zip_regex, '')
end
if street_match = address_string.match(street_regex)
address[:street] = street_match[0].strip
address[:street] = nil if address[:street].empty?
address_string.sub!(street_regex, '')
end
return address if city_and_state_regex_found
if state_match = address_string.match(state_regex)
address[:state] = state_match[0].strip
address_string.sub!(state_regex, '')
end
if city_match = address_string.match(city_regex)
address[:city] = city_match[0].strip
address_string.sub!(city_regex, '')
end
return address
end
|