Class: InkscapeCLI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/inkscape_cli/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executable: InkscapeCLI.executable, timeout: InkscapeCLI.timeout) ⇒ Command

Returns a new instance of Command.



18
19
20
21
22
# File 'lib/inkscape_cli/command.rb', line 18

def initialize(executable: InkscapeCLI.executable, timeout: InkscapeCLI.timeout)
  @executable = executable
  @args = []
  @timeout = timeout
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/inkscape_cli/command.rb', line 58

def method_missing(name, *args)
  option = "--#{name.to_s.tr('_', '-').gsub('=', '')}"
  self.args << option
  args.each do |arg|
    self.args << arg.to_s
  end
  self
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/inkscape_cli/command.rb', line 5

def args
  @args
end

#executableObject (readonly)

Returns the value of attribute executable.



5
6
7
# File 'lib/inkscape_cli/command.rb', line 5

def executable
  @executable
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/inkscape_cli/command.rb', line 5

def timeout
  @timeout
end

Class Method Details

.new(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/inkscape_cli/command.rb', line 7

def self.new(*args)
  instance = super(*args)

  if block_given?
    yield instance
    instance.execute
  else
    instance
  end
end

Instance Method Details

#<<(arg) ⇒ Object



53
54
55
56
# File 'lib/inkscape_cli/command.rb', line 53

def <<(arg)
  args << arg.to_s
  self
end

#executeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/inkscape_cli/command.rb', line 24

def execute
  Open3.popen3(executable, *args) do |_stdin, stdout, stderr, thread|
    [stdout, stderr].each(&:binmode)
    stdout_reader = Thread.new { stdout.read }
    stderr_reader = Thread.new { stderr.read }

    unless thread.join(timeout)
      Process.kill("TERM", thread.pid) rescue nil
      Process.waitpid(thread.pid) rescue nil
      raise Timeout::Error, "Inkscape command timed out..."
    end

    exit_status = thread.value.exitstatus
    if exit_status != 0
      raise InkscapeCLI::Error,
            "Inkscape command failed with status: #{exit_status} and error: #{stderr_reader.value}"
    end

    [stdout_reader.value, stderr_reader.value, exit_status]
  end
end

#input_file(file) ⇒ Object Also known as: input_file=



46
47
48
49
# File 'lib/inkscape_cli/command.rb', line 46

def input_file(file)
  self << file
  self
end