Sha256: 87c7428e11f276d56163171e9eff9987c8b4722fd5b0fe779bf81f17818ca9cc

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

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

      DEFAULT_OPTIONS = {
        headers: true,
        filename: $stdin
      }.freeze

      def initialize(options = {})
        super(DEFAULT_OPTIONS.merge(options))
      end

      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(@options[:filename], headers: @options[:headers]).count unless stdin?(@options[:filename])
      end

      private

      def initialize_csv
        headers = @options[:headers].is_a?(String) ? @options[:headers].split(',') : @options[:headers]

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chronicle-etl-0.3.1 lib/chronicle/etl/extractors/csv_extractor.rb
chronicle-etl-0.3.0 lib/chronicle/etl/extractors/csv_extractor.rb