Module: Poleica::Converters::Utils

Included in:
Coercive, GraphicsMagick, 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 = [], timeout = nil, no_stdout = true) ⇒ Object



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

def exec_with_timeout(bin, args = [], timeout = nil, no_stdout = true)
  args              = Array(args).map(&:to_s)
  timeout         ||= Poleica.configuration.timeout
  process           = ChildProcess.build(bin, *args)
  set_process_stdout(process, no_stdout)
  process.start
  timeout ? process.poll_for_exit(timeout) : process.wait
rescue ChildProcess::TimeoutError => e
  process.stop
  raise(Poleica::TimeoutError.new(e.message))
end

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



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

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

.set_process_stdout(process, no_stdout) ⇒ Object



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

def set_process_stdout(process, no_stdout)
  if no_stdout
    null              = IO.new(IO.sysopen('/dev/null', 'w'), 'w')
    process.io.stdout = process.io.stderr = null
  else
    process.io.inherit!
  end
end

Instance Method Details

#bin_pathObject



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

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

#host_osObject



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

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

#linux?Boolean

Returns:

  • (Boolean)


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

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

#osx?Boolean

Returns:

  • (Boolean)


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

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

#underscorize(camel_cased_word) ⇒ Object



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

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)


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

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