Module: Poleica::Converters::Utils

Included in:
Coercive, GraphicsMagick, GraphicsMagick::ThumbnailOptionsGenerator, LibreOffice
Defined in:
lib/poleica/converters/utils.rb

Overview

An Utility module for the converters needs to be include

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exec_with_timeout(bin, args = [], options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/poleica/converters/utils.rb', line 52

def exec_with_timeout(bin, args = [], options = {})
  timeout           = options[:timeout] || Poleica.configuration.timeout
  process           = ChildProcess.build(bin, *Array(args).map(&:to_s))
  map_std(process) do
    process.start
    timeout ? process.poll_for_exit(timeout) : process.wait
  end
rescue ChildProcess::TimeoutError => e
  process.stop
  raise Poleica::TimeoutError, e.message
end

.extract_extension_and_options(method, args = []) ⇒ Object



45
46
47
48
49
50
# File 'lib/poleica/converters/utils.rb', line 45

def extract_extension_and_options(method, args = [])
  extension  = method.to_s.split(/^to_(.*)/)[1]
  options    = args.last if args.last.is_a?(Hash)
  options    ||= {}
  [extension, options]
end

.fail_if_error(process, stderr) ⇒ Object



84
85
86
87
88
# File 'lib/poleica/converters/utils.rb', line 84

def fail_if_error(process, stderr)
  return if process.exit_code == 0
  message = "Code: #{process.exit_code} #{stderr.read}"
  fail Poleica::ProcessError, message
end

.init_process_std(process) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/poleica/converters/utils.rb', line 74

def init_process_std(process)
  stdout, stdout_w = IO.pipe
  stderr, stderr_w = IO.pipe

  process.io.stdout = stdout_w
  process.io.stderr = stderr_w

  [stdout, stdout_w, stderr, stderr_w]
end

.map_std(process, &_block) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/poleica/converters/utils.rb', line 64

def map_std(process, &_block)
  stdout, stdout_w, stderr, stderr_w = init_process_std(process)
  yield
  stderr_w.close.nil? && fail_if_error(process, stderr)
  stdout_w.close.nil? && stdout.read
ensure
  stderr_w.close unless stderr_w.closed?
  stdout_w.close unless stdout_w.closed?
end

Instance Method Details

#bin_path(given_class = nil) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/poleica/converters/utils.rb', line 27

def bin_path(given_class = nil)
  converter     = :"#{underscorize(given_class || self.class)}"
  configuration = Poleica.configuration.send(converter)
  bin_paths     = configuration[:bin_paths]
  path          = bin_paths[host_os] || bin_paths[:linux]
  fail "#{converter} not found @ #{path}" unless File.exist?(path)
  path
end

#host_osObject



23
24
25
# File 'lib/poleica/converters/utils.rb', line 23

def host_os
  [:windows, :osx, :linux].find { |os| send(:"#{os}?") }
end

#linux?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/poleica/converters/utils.rb', line 19

def linux?
  !HOST_OS.match(/linux/i).nil?
end

#osx?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/poleica/converters/utils.rb', line 15

def osx?
  !HOST_OS.match(/darwin/i).nil?
end

#underscorize(camel_cased_word) ⇒ Object



36
37
38
39
40
41
# File 'lib/poleica/converters/utils.rb', line 36

def underscorize(camel_cased_word)
  word = camel_cased_word.to_s
  word = word[((word.rindex('::') + 2) || 0)..-1]
  word.gsub!(/([A-Z]+)/, '_\1').gsub!(/^_/, '').downcase!
  word
end

#windows?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/poleica/converters/utils.rb', line 11

def windows?
  !HOST_OS.match(/mswin|windows|cygwin/i).nil?
end