lib/sysmodb/extractor.rb in simple-spreadsheet-extractor-0.16.1 vs lib/sysmodb/extractor.rb in simple-spreadsheet-extractor-0.17.0
- old
+ new
@@ -1,71 +1,64 @@
-require 'open4'
+require 'terrapin'
module SysMODB
- #Exception that is thrown when a problem occurs during the extraction
+ # Exception that is thrown when a problem occurs during the extraction
class SpreadsheetExtractionException < Exception; end
- #handles the delegation to java, and executes the extraction passing the
- #input file through STDIN, and reading the results through STDOUT.
+ # handles the delegation to java
class Extractor
- JAR_VERSION="0.16.0".freeze
+ JAR_VERSION = '0.16.0'.freeze
DEFAULT_PATH = File.dirname(__FILE__) + "/../../jars/simple-spreadsheet-extractor-#{JAR_VERSION}.jar"
- BUFFER_SIZE=250000 # 1/4 a megabyte
def initialize(memory_allocation)
@memory_allocation = memory_allocation
- if is_windows?
- raise Exception.new("Windows is not currently supported")
- end
+ raise Exception, 'Windows is not currently supported' if is_windows?
end
+ # spreadsheet_data can be an IO like object or the path to a file
def spreadsheet_to_xml(spreadsheet_data)
- read_with_open4 spreadsheet_data,"xml"
+ spreadsheet_to_requested_format(spreadsheet_data, 'xml')
end
- def spreadsheet_to_csv(spreadsheet_data,sheet=1,trim=false)
- read_with_open4 spreadsheet_data,"csv",sheet,trim
+ # spreadsheet_data can be an IO like object or the path to a file
+ def spreadsheet_to_csv(spreadsheet_data, sheet = 1, trim = false)
+ spreadsheet_to_requested_format(spreadsheet_data, 'csv', sheet, trim)
end
private
- def spreadsheet_extractor_command(format="xml",sheet=nil,trim=false)
+ def spreadsheet_to_requested_format(spreadsheet_data, format, sheet = nil, trim = nil)
+ if spreadsheet_data.is_a?(IO) || spreadsheet_data.is_a?(StringIO)
+ Tempfile.create('spreadsheet-extraction') do |f|
+ f.write(spreadsheet_data.read)
+ f.flush
+ execute_command_line f.path, format, sheet, trim
+ end
+ elsif spreadsheet_data.is_a?(String)
+ execute_command_line spreadsheet_data, format, sheet, trim
+ end
+ end
+
+ def spreadsheet_extractor_command(filepath, format = 'xml', sheet = nil, trim = false)
command = "java -Xmx#{@memory_allocation} -jar #{(defined? SPREADSHEET_EXTRACTOR_JAR_PATH) ? SPREADSHEET_EXTRACTOR_JAR_PATH : DEFAULT_PATH}"
- command += " -o #{format}"
+ command += " -o #{format}"
command += " -s #{sheet}" if sheet
- command += " -t" if trim
+ command += ' -t' if trim
+ command += " < #{filepath}"
command
end
def is_windows?
!(RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /mingw32/).nil?
end
- def read_with_open4(spreadsheet_data,format="xml",sheet=nil,trim=false)
- output = ""
- err_message = ""
- command = spreadsheet_extractor_command format,sheet,trim
- status = Open4.popen4(command) do |_pid, stdin, stdout, stderr|
- while ((line = spreadsheet_data.gets(BUFFER_SIZE)) != nil) do
- stdin << line
- end
- stdin.close
-
- while ((line = stdout.gets(BUFFER_SIZE)) != nil) do
- output << line
- end
- stdout.close
-
- until ((line=stderr.gets((BUFFER_SIZE))).nil?) do
- err_message << line
- end
- stderr.close
+ def execute_command_line(filepath, format = 'xml', sheet = nil, trim = false)
+ command = spreadsheet_extractor_command filepath, format, sheet, trim
+ begin
+ Terrapin::CommandLine.new(command).run.strip
+ rescue Terrapin::ExitStatusError, Terrapin::CommandNotFoundError => e
+ raise SpreadsheetExtractionException, e.message
end
-
- if status.to_i != 0
- raise SpreadsheetExtractionException.new(err_message)
- end
-
- output.strip
end
+
end
end