Sha256: 018b5feeb01a222e316a03c941fb32be7bc20fd20e785b305c5e266c9d7a3369

Contents?: true

Size: 983 Bytes

Versions: 1

Compression:

Stored size: 983 Bytes

Contents

require 'csv'

module Chronicle
  module ETL
    class CSVExtractor < Chronicle::ETL::Extractor
      include Extractors::Helpers::FilesystemReader

      register_connector do |r|
        r.description = 'input as CSV'
      end

      setting :headers, default: true
      setting :filename, default: $stdin

      def extract
        csv = initialize_csv
        csv.each do |row|
          yield Chronicle::ETL::Extraction.new(data: row.to_h)
        end
      end

      def results_count
        CSV.read(@config.filename, headers: @config.headers).count unless stdin?(@config.filename)
      end

      private

      def initialize_csv
        headers = @config.headers.is_a?(String) ? @config.headers.split(',') : @config.headers

        csv_options = {
          headers: headers,
          converters: :all
        }

        open_from_filesystem(filename: @config.filename) do |file|
          return CSV.new(file, **csv_options)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-etl-0.4.0 lib/chronicle/etl/extractors/csv_extractor.rb