Module: Thumblemonks::Indico::IOHelpers

Included in:
PDFDocument, RTFDocument, WordDocument
Defined in:
lib/thumblemonks/indico/io_helpers.rb

Instance Method Summary collapse

Instance Method Details

#open_or_raise(command, exception = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/thumblemonks/indico/io_helpers.rb', line 8

def open_or_raise(command, exception = nil)
  exception ||= RuntimeError
  return_val = nil
  status = Open4.popen4(command) do |pid,stdin,stdout,stderr|
    return_val = yield(pid, stdin, stdout, stderr)
  end
  raise(exception) unless status.exitstatus.zero?
  return_val
end

#open_with_tempfile_or_raise(content, exception = RuntimeError) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/thumblemonks/indico/io_helpers.rb', line 18

def open_with_tempfile_or_raise(content, exception = RuntimeError)
  return_val = nil
  Tempfile.open(self.class.name.gsub(/[^a-z]/i, '_').downcase) do |tf|
    tf << content
    tf.flush
    cmd = yield(tf)
    open_or_raise(cmd, exception) { |pid,stdin,stdout,stderr| return_val = stdout.read }
  end
  return_val
end