From 69782f6b1a25290c24ca83443893ef99d9cf4a13 Mon Sep 17 00:00:00 2001 From: Dave Worth Date: Sun, 5 May 2013 00:01:10 -0400 Subject: [PATCH] extract terrible method to its own class instead --- lib/indirizzo/address.rb | 3 +- lib/indirizzo/address_hash_extractor.rb | 61 +++++++++++++++++++++++++ lib/indirizzo/parser.rb | 57 ----------------------- 3 files changed, 63 insertions(+), 58 deletions(-) create mode 100644 lib/indirizzo/address_hash_extractor.rb diff --git a/lib/indirizzo/address.rb b/lib/indirizzo/address.rb index 2fe5f83..89d7996 100644 --- a/lib/indirizzo/address.rb +++ b/lib/indirizzo/address.rb @@ -1,5 +1,6 @@ require 'indirizzo/constants' require 'indirizzo/parser' +require 'indirizzo/address_hash_extractor' require 'indirizzo/match' require 'indirizzo/city' require 'indirizzo/street' @@ -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) diff --git a/lib/indirizzo/address_hash_extractor.rb b/lib/indirizzo/address_hash_extractor.rb new file mode 100644 index 0000000..3bb3e7a --- /dev/null +++ b/lib/indirizzo/address_hash_extractor.rb @@ -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 diff --git a/lib/indirizzo/parser.rb b/lib/indirizzo/parser.rb index 97970ef..74656ee 100644 --- a/lib/indirizzo/parser.rb +++ b/lib/indirizzo/parser.rb @@ -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)