Class: MiniMagick::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_magick/shell.rb

Overview

Sends commands to the shell (more precisely, it sends commands directly to the operating system).

Instance Method Summary collapse

Constructor Details

#initialize(whiny = true) ⇒ Shell

Returns a new instance of Shell.



13
14
15
# File 'lib/mini_magick/shell.rb', line 13

def initialize(whiny = true)
  @whiny = whiny
end

Instance Method Details

#execute(command) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mini_magick/shell.rb', line 32

def execute(command)
  stdout, stderr, status =
    MiniMagick.logger.debug(command.join(" ")) do
      Timeout.timeout(MiniMagick.timeout) do
        send("execute_#{MiniMagick.shell_api.gsub("-", "_")}", *command)
      end
    end

  [stdout, stderr, status.exitstatus]
rescue Errno::ENOENT, IOError
  ["", "executable not found: \"#{command.first}\"", 127]
end

#run(command) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mini_magick/shell.rb', line 17

def run(command)
  stdout, stderr, code = execute(command)

  case code
  when 1
    fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
  when 127
    fail MiniMagick::Error, stderr
  end if @whiny

  $stderr.print(stderr)

  stdout
end