Sha256: ef8ff51e5b92a60b02bcce5d3f05df434f49859beef3b159e58219aade515fe3

Contents?: true

Size: 900 Bytes

Versions: 4

Compression:

Stored size: 900 Bytes

Contents

require 'csv'
class Chronicle::ETL::CsvExtractor < Chronicle::ETL::Extractor
  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|
      result = row.to_h
      yield result
    end
  end

  def results_count
    CSV.read(@options[:filename], headers: @options[:headers]).count if read_from_file?
  end

  private

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

    csv_options = {
      headers: headers,
      header_converters: :symbol,
      converters: [:all]
    }

    stream = read_from_file? ? File.open(@options[:filename]) : @options[:filename]
    CSV.new(stream, **csv_options)
  end

  def read_from_file?
    @options[:filename] != $stdin
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chronicle-etl-0.2.3 lib/chronicle/etl/extractors/csv_extractor.rb
chronicle-etl-0.2.2 lib/chronicle/etl/extractors/csv_extractor.rb
chronicle-etl-0.2.1 lib/chronicle/etl/extractors/csv_extractor.rb
chronicle-etl-0.2.0 lib/chronicle/etl/extractors/csv_extractor.rb