lib/chronicle/etl/extractors/csv_extractor.rb in chronicle-etl-0.3.1 vs lib/chronicle/etl/extractors/csv_extractor.rb in chronicle-etl-0.4.0

- old
+ new

@@ -1,46 +1,40 @@ require 'csv' module Chronicle module ETL - class CsvExtractor < Chronicle::ETL::Extractor + 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 + setting :headers, default: true + setting :filename, default: $stdin - 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]) + CSV.read(@config.filename, headers: @config.headers).count unless stdin?(@config.filename) end private def initialize_csv - headers = @options[:headers].is_a?(String) ? @options[:headers].split(',') : @options[:headers] + headers = @config.headers.is_a?(String) ? @config.headers.split(',') : @config.headers csv_options = { headers: headers, converters: :all } - open_from_filesystem(filename: @options[:filename]) do |file| + open_from_filesystem(filename: @config.filename) do |file| return CSV.new(file, **csv_options) end end end end