Sha256: 2f6e0297054c3ba71733ea6373f767479cb6b007ec038c284970e27f0bc15b26

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

require 'csv'
require_relative 'quantity_exploder'

module Squib::Import
  class CsvImporter
    include Squib::Import::QuantityExploder
    def import_to_dataframe(import, csv_opts, &block)
      data = import.data.nil? ? File.read(import.file) : import.data
      table = CSV.parse(data, **csv_opts.to_hash)
      check_duplicate_csv_headers(table)
      hash = Squib::DataFrame.new
      table.headers.each do |header|
        new_header = header.to_s
        new_header = new_header.strip if import.strip?
        hash[new_header] ||= table[header]
      end
      if import.strip?
        new_hash = Squib::DataFrame.new
        hash.each do |header, col|
          new_hash[header] = col.map do |str|
            str = str.strip if str.respond_to?(:strip)
            str
          end
        end
        hash = new_hash
      end
      unless block.nil?
        hash.each do |header, col|
          col.map! do |val|
            yield(header, val)
          end
        end
      end
      return explode_quantities(hash, import.explode)
    end

    def check_duplicate_csv_headers(table)
      if table.headers.size != table.headers.uniq.size
        dups = table.headers.select{|e| table.headers.count(e) > 1 }
        Squib.logger.warn "CSV duplicated the following column keys: #{dups.join(',')}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
squib-0.19.0 lib/squib/import/csv_importer.rb
squib-0.19.0b lib/squib/import/csv_importer.rb
squib-0.19.0a lib/squib/import/csv_importer.rb