Sha256: 7d1c5015c5d9307c02e99ef93a65239952051b4e6f174e8188ea7293f95328de

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'csv'

module Chronicle
  module ETL
    class CSVLoader < Chronicle::ETL::Loader
      include Chronicle::ETL::Loaders::Helpers::StdoutHelper

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

      setting :output
      setting :headers, default: true
      setting :header_row, default: true

      def records
        @records ||= []
      end

      def load(record)
        records << record.to_h_flattened
      end

      def finish
        return unless records.any?

        headers = build_headers(records)

        csv_options = {}
        if @config.headers
          csv_options[:write_headers] = @config.header_row
          csv_options[:headers] = headers
        end

        csv_output = CSV.generate(**csv_options) do |csv|
          records.each do |record|
            csv << record
              .transform_keys(&:to_sym)
              .values_at(*headers)
              .map { |value| force_utf8(value) }
          end
        end

        # TODO: just write to io directly
        if output_to_stdout?
          write_to_stdout(csv_output)
        else
          File.write(@config.output, csv_output)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chronicle-etl-0.5.5 lib/chronicle/etl/loaders/csv_loader.rb
chronicle-etl-0.5.4 lib/chronicle/etl/loaders/csv_loader.rb
chronicle-etl-0.5.3 lib/chronicle/etl/loaders/csv_loader.rb
chronicle-etl-0.5.2 lib/chronicle/etl/loaders/csv_loader.rb