Sha256: 747b305953a12681a0dd1ecaaf8c9b86d13914a29597786079092eb6e8fad71c

Contents?: true

Size: 813 Bytes

Versions: 2

Compression:

Stored size: 813 Bytes

Contents

class ODSExtractor::CSVOutput
  def initialize(csv_directory_path)
    require "csv"
    @csv_directory_path = csv_directory_path
  end

  def start_sheet(sheet_name)
    @write_output_file = File.open(File.join(@csv_directory_path, sheet_name_to_csv_file_name(sheet_name)), "wb")
  end

  def write_row(row_values)
    @write_output_file << CSV.generate_line(row_values, force_quotes: true) # => "foo,0\n"
  end

  def end_sheet
    @write_output_file.close
  end

  def sheet_name_to_csv_file_name(sheet_name)
    # This is a subtle spot where there can be a security problem - we take an unsanitized sheet name
    # and we include it in a filesystem path. So some precaution needs to be taken.
    sanitized_sheet_name = File.basename(File.expand_path(sheet_name))
    "#{sanitized_sheet_name}.csv"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ods_extractor-0.1.1 lib/ods_extractor/csv_output.rb
ods_extractor-0.1.0 lib/ods_extractor/csv_output.rb