lib/wolf_core/application/barton/parsing.rb



module WolfCore
  module Barton
    module Parsing
      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
    end
  end
end