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
- .exec_with_timeout(bin, args = [], timeout = nil, no_stdout = true) ⇒ Object
- .extract_extension_and_options(method, args = []) ⇒ Object
- .set_process_stdout(process, no_stdout) ⇒ Object
Instance Method Summary collapse
- #bin_path ⇒ Object
- #host_os ⇒ Object
- #linux? ⇒ Boolean
- #osx? ⇒ Boolean
- #underscorize(camel_cased_word) ⇒ Object
- #windows? ⇒ Boolean
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.)) end |
.extract_extension_and_options(method, args = []) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/poleica/converters/utils.rb', line 46 def (method, args = []) extension = method.to_s.split(/^to_(.*)/)[1] = args.last if args.last.is_a?(Hash) ||= {} [extension, ] 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_path ⇒ Object
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_os ⇒ Object
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
20 21 22 |
# File 'lib/poleica/converters/utils.rb', line 20 def linux? !!HOST_OS.match(/linux/i) end |
#osx? ⇒ 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
12 13 14 |
# File 'lib/poleica/converters/utils.rb', line 12 def windows? !!HOST_OS.match(/mswin|windows|cygwin/i) end |