Skip to content

Commit

Permalink
extract state fetching into method
Browse files Browse the repository at this point in the history
* inline parse_state to avoid side-effects that were weird
  • Loading branch information
Dave Worth committed May 5, 2013
1 parent 969a884 commit c2c7a59
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/indirizzo/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ def parse
@country = @text[zip_end_index+1..-1].sub(/^\s*,\s*/, '').strip
@country = nil if @country == text

@state = text.scan(Match[:state]).last
if @state
last_match = $&
state_index = text.rindex(last_match)
text = parse_state(last_match, text)
else
@full_state = ""
@state = ""
end
@state, @full_state, @city, state_index = extract_state_from_text(text)

@number = text.scan(Match[:number]).first
# FIXME: 230 Fish And Game Rd, Hudson NY 12534
Expand Down Expand Up @@ -76,14 +68,6 @@ def parse

private

def parse_state(regex_match, text)
idx = text.rindex(regex_match)
@full_state = @state[0].strip # special case: New York
@state = State[@full_state]
@city = "Washington" if @state == "DC" && text[idx...idx+regex_match.length] =~ /washington\s+d\.?c\.?/i
text
end

def extract_zip_from_text(text)
zip = text.scan(Match[:zip]).last
if zip
Expand All @@ -98,5 +82,21 @@ def extract_zip_from_text(text)
end
return zip, plus4, zip_index, zip_end_index
end

def extract_state_from_text(text)
state = text.scan(Match[:state]).last
if state
last_match = $&
state_index = text.rindex(last_match)
idx = text.rindex(last_match)
full_state = state[0].strip # special case: New York
state = State[full_state]
city = "Washington" if state == "DC" && text[idx...idx+last_match.length] =~ /washington\s+d\.?c\.?/i
else
full_state = ""
state = ""
end
return state, full_state, city, state_index
end
end
end

0 comments on commit c2c7a59

Please sign in to comment.