Class: VideoConverter::Command

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, params = {}, safe_keys = []) ⇒ Command

Returns a new instance of Command.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/video_converter/command.rb', line 13

def initialize command, params = {}, safe_keys = []
  self.command = command.dup
  if params.any?
    params = params.deep_shellescape_values(safe_keys)
    self.command.gsub!(/%\{(\w+?)\}/) do
      value = params[$1.to_sym]
      if value.is_a?(Hash)
        value.deep_join(' ')
      else
        value.to_s
      end
    end
  end
  raise ArgumentError.new("Command is not parsed '#{self.command}'") if self.command.match(/%{[\w\-.]+}/)
  self.command = "nice -n #{self.class.nice} #{self.command}" if self.class.nice
  self.command = "ionice -c 2 -n #{self.class.ionice} #{self.command}" if self.class.ionice
end

Class Attribute Details

.dry_runObject

Returns the value of attribute dry_run.



6
7
8
# File 'lib/video_converter/command.rb', line 6

def dry_run
  @dry_run
end

.ioniceObject

Returns the value of attribute ionice.



6
7
8
# File 'lib/video_converter/command.rb', line 6

def ionice
  @ionice
end

.niceObject

Returns the value of attribute nice.



6
7
8
# File 'lib/video_converter/command.rb', line 6

def nice
  @nice
end

.verboseObject

Returns the value of attribute verbose.



6
7
8
# File 'lib/video_converter/command.rb', line 6

def verbose
  @verbose
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



11
12
13
# File 'lib/video_converter/command.rb', line 11

def command
  @command
end

Instance Method Details

#append(*commands) ⇒ Object



49
50
51
52
# File 'lib/video_converter/command.rb', line 49

def append(*commands)
  self.command = commands.unshift(command).map { |c| "(#{c})" }.join(' && ')
  self
end

#capture(params = {}) ⇒ Object



40
41
42
43
# File 'lib/video_converter/command.rb', line 40

def capture params = {}
  puts command if params[:verbose] || self.class.verbose
  `#{command}`.encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')
end

#execute(params = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/video_converter/command.rb', line 31

def execute params = {}
  puts command if params[:verbose] || self.class.verbose
  if params[:dry_run] || self.class.dry_run
    true
  else
    system command
  end
end

#to_sObject



45
46
47
# File 'lib/video_converter/command.rb', line 45

def to_s
  command
end