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

      def parse_date(date_string)
        return nil if date_string.nil?
        date_string = date_string.strip
        return nil if date_string.empty?
        Date.parse(date_string).strftime('%Y-%m-%d') rescue nil
      end

      def split_name(full_name)
        words = full_name.split(' ')
  
        if words.length == 2
          return { firstname: words[0], lastname: words[1] }
        end
  
        name = words[0..-2].join(' ')
        lastname = words[-1]
        return { firstname: name, lastname: lastname }
      end
    end
  end
end