lib/io_streams/xlsx/reader.rb in iostreams-0.20.3 vs lib/io_streams/xlsx/reader.rb in iostreams-1.0.0.beta
- old
+ new
@@ -1,27 +1,15 @@
require 'csv'
module IOStreams
module Xlsx
- class Reader
- # Convert a xlsx, or xlsm file or stream into CSV format.
- def self.open(file_name_or_io, _ = nil, &block)
- return extract_csv(file_name_or_io, &block) if file_name_or_io.is_a?(String)
-
- # Creek gem can only work against a file, not a stream, so create temp file.
- IOStreams::File::Path.temp_file_name('iostreams_xlsx') do |temp_file_name|
- IOStreams.copy(file_name_or_io, temp_file_name, target_options: {streams: []})
- extract_csv(temp_file_name, &block)
- end
- end
-
- # Convert the spreadsheet to csv in a tempfile
- def self.extract_csv(file_name, &block)
- IOStreams::File::Path.temp_file_name('iostreams_csv') do |temp_file_name|
- IOStreams::File::Writer.open(temp_file_name) do |io|
- new(file_name).each { |lines| io << lines.to_csv }
- end
- IOStreams::File::Reader.open(temp_file_name, &block)
+ class Reader < IOStreams::Reader
+ # Convert a xlsx, or xlsm file into CSV format.
+ def self.file(file_name, original_file_name: file_name, &block)
+ # Stream into a temp file as csv
+ Utils.temp_file_name('iostreams_csv') do |temp_file_name|
+ ::File.open(temp_file_name, 'wb') { |io| new(file_name).each { |lines| io << lines.to_csv } }
+ ::File.open(temp_file_name, 'rb', &block)
end
end
def initialize(file_name)
begin