<commit_before>require 'csv'

module Peoplefinder
  class CsvPeopleImporter
    COLUMNS = [:given_name, :surname, :email]

    attr_reader :errors

    def initialize(csv)
      @csv = csv
    end

    def valid?
      @errors = []

      if missing_columns.empty?
        csv_rows.each do |row|
          person = Person.new(row.to_h.slice(*COLUMNS))
          unless person.valid?
            @errors << %(row "#{row.to_csv.strip}": #{person.errors.full_messages.join(', ')})
          end
        end
      else
        @errors = missing_columns.map { |column| "#{column} column is missing" }
      end

      @errors.empty?
    end

  private

    def csv_rows
      CSV.new(@csv, headers: true, skip_blanks: true, header_converters: :symbol)
    end

    def missing_columns
      header_row = CSV.new(@csv,
        headers: true, return_headers: true, header_converters: :symbol).first

      COLUMNS.reject { |column| header_row.include?(column) }
    end
  end
end
<commit_msg>Refactor csv user imported to work with csv file handle<commit_after>require 'csv'

module Peoplefinder
  class CsvPeopleImporter
    COLUMNS = %w[given_name surname email]

    attr_reader :errors

    def initialize(csv)
      @csv = CSV.new(csv, headers: true, return_headers: true)
      @header_row = @csv.first
    end

    def valid?
      @errors = []

      if missing_columns.empty?
        @csv.each do |row|
          person = Person.new(row.to_h.slice(*COLUMNS))
          unless person.valid?
            @errors << %(row "#{row.to_csv.strip}": #{person.errors.full_messages.join(', ')})
          end
        end
      else
        @errors = missing_columns.map { |column| "#{column} column is missing" }
      end

      @errors.empty?
    end

  private

    def missing_columns
      COLUMNS.reject { |column| @header_row.include?(column) }
    end
  end
end

{'fraction_non_alphanumeric': 0.11224489795918367, 'fraction_numerical': 0.0, 'mean_word_length': 2.3934426229508197, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6953086', 'n_tokens_mistral': 614, 'n_tokens_neox': 588, 'n_words': 143}