Module: ExtractTtc

Extended by:
FFI::Library
Defined in:
lib/extract_ttc.rb,
lib/extract_ttc/version.rb

Defined Under Namespace

Classes: Error, InvalidFileError, ReadFileError, UnknownResultError, WriteFileError

Constant Summary collapse

VERSION =
"0.3.6".freeze

Class Method Summary collapse

Class Method Details

.capture3Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/extract_ttc.rb', line 51

def self.capture3
  stderr = status = nil

  stdout = capture_stream($stdout) do
    stderr = capture_stream($stderr) do
      status = yield
    end
  end

  [stdout, stderr, status]
end

.capture_stream(stream_io) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/extract_ttc.rb', line 63

def self.capture_stream(stream_io)
  origin_stream = stream_io.dup

  Tempfile.open("captured_stream") do |captured_stream|
    stream_io.reopen(captured_stream)
    yield
    captured_stream.rewind
    return captured_stream.read
  end
ensure
  stream_io.reopen(origin_stream)
end

.extract(path) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/extract_ttc.rb', line 28

def self.extract(path)
  stdout, stderr, code = capture3 do
    handlefile(path)
  end

  return handle_error(code, stderr) unless code.zero?

  fetch_filenames(stdout)
end

.fetch_filenames(stdout) ⇒ Object



76
77
78
# File 'lib/extract_ttc.rb', line 76

def self.fetch_filenames(stdout)
  stdout.split("=>").last.split
end

.handle_error(code, stderr) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/extract_ttc.rb', line 38

def self.handle_error(code, stderr)
  case code
  when -1
    raise ReadFileError, stderr
  when -2
    raise InvalidFileError, stderr
  when -3
    raise WriteFileError, stderr
  else
    raise UnknownResultError, "Return code: #{code}"
  end
end