Skip to content

Commit

Permalink
extract terrible method to its own class instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Worth committed May 5, 2013
1 parent 05f8e90 commit 69782f6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 58 deletions.
3 changes: 2 additions & 1 deletion lib/indirizzo/address.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'indirizzo/constants'
require 'indirizzo/parser'
require 'indirizzo/address_hash_extractor'
require 'indirizzo/match'
require 'indirizzo/city'
require 'indirizzo/street'
Expand Down Expand Up @@ -39,7 +40,7 @@ def clean (value)
end

def assign_text_to_address(text)
@text, @city, @street, @number, @prenum, @sufnum, @full_state, @state, @zip, @plus4, @country = Parser.extract_data_from_hash(text, @options)
@text, @city, @street, @number, @prenum, @sufnum, @full_state, @state, @zip, @plus4, @country = AddressHashExtractor.extract(text, @options)
end

def expand_numbers (string)
Expand Down
61 changes: 61 additions & 0 deletions lib/indirizzo/address_hash_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Indirizzo
class AddressHashExtractor
def self.extract(address_hash, options)
text = address_hash
if !text[:address].nil?
@text = Helper.clean text[:address]
return Parser.new(@text, options).parse
else
@street = []
@prenum = text[:prenum]
@sufnum = text[:sufnum]
if !text[:street].nil?
@street = text[:street].scan(Match[:street])
end
@number = ""
if !@street.nil?
if text[:number].nil?
@street.map! { |single_street|
single_street.downcase!
@number = single_street.scan(Match[:number])[0].reject{|n| n.nil? || n.empty?}.first.to_s
single_street.sub! @number, ""
single_street.sub! /^\s*,?\s*/o, ""
}
else
@number = text[:number].to_s
end
@street = Street.expand(@street) if options[:expand_streets]
#Street.parts
end
@city = []
if !text[:city].nil?
@city.push(text[:city])
@text = text[:city].to_s
else
@city.push("")
end
if !text[:region].nil?
# @state = []
@state = text[:region]
if @state.length > 2
# full_state = @state.strip # special case: New York
@state = State[@state]
end
elsif !text[:state].nil?
@state = text[:state]
elsif !text[:country].nil?
@state = text[:country]
end

@zip = text[:postal_code]
@plus4 = text[:plus4]
if !@zip
@zip = @plus4 = ""
end
end

return @text, @city, @street, @number, @prenum, @sufnum, @full_state, @state, @zip, @plus4, @country
end

end
end
57 changes: 0 additions & 57 deletions lib/indirizzo/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,63 +84,6 @@ def parse
return @city, @street, @number, @prenum, @sufnum, @full_state, @state, @zip, @plus4, @country
end

def self.extract_data_from_hash(address_hash, options)
text = address_hash
if !text[:address].nil?
@text = Helper.clean text[:address]
return self.new(@text, options).parse
else
@street = []
@prenum = text[:prenum]
@sufnum = text[:sufnum]
if !text[:street].nil?
@street = text[:street].scan(Match[:street])
end
@number = ""
if !@street.nil?
if text[:number].nil?
@street.map! { |single_street|
single_street.downcase!
@number = single_street.scan(Match[:number])[0].reject{|n| n.nil? || n.empty?}.first.to_s
single_street.sub! @number, ""
single_street.sub! /^\s*,?\s*/o, ""
}
else
@number = text[:number].to_s
end
@street = Street.expand(@street) if options[:expand_streets]
#Street.parts
end
@city = []
if !text[:city].nil?
@city.push(text[:city])
@text = text[:city].to_s
else
@city.push("")
end
if !text[:region].nil?
# @state = []
@state = text[:region]
if @state.length > 2
# full_state = @state.strip # special case: New York
@state = State[@state]
end
elsif !text[:state].nil?
@state = text[:state]
elsif !text[:country].nil?
@state = text[:country]
end

@zip = text[:postal_code]
@plus4 = text[:plus4]
if !@zip
@zip = @plus4 = ""
end
end

return @text, @city, @street, @number, @prenum, @sufnum, @full_state, @state, @zip, @plus4, @country
end

private

def parse_state(regex_match, text)
Expand Down

0 comments on commit 69782f6

Please sign in to comment.