Sha256: b81df014f611747ae12eac288d3e05a106d2a8ff2c7a65b0bb2dd4030acf8cb9

Contents?: true

Size: 900 Bytes

Versions: 2

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

2 entries across 2 versions & 1 rubygems

Version Path
chronicle-etl-0.1.4 lib/chronicle/etl/extractors/csv_extractor.rb
chronicle-etl-0.1.3 lib/chronicle/etl/extractors/csv_extractor.rb