Class: VideoConverter::Command

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Command.

Raises:

  • (ArgumentError)


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

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\-.]+}/)
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

.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.



15
16
17
# File 'lib/video_converter/command.rb', line 15

def command
  @command
end

Class Method Details

.chain(*commands) ⇒ Object



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

def self.chain(*commands)
  commands.map { |c| "(#{c})" }.join(' && ')
end

Instance Method Details

#capture(params = {}) ⇒ Object



42
43
44
45
# File 'lib/video_converter/command.rb', line 42

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



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

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



47
48
49
# File 'lib/video_converter/command.rb', line 47

def to_s
  command
end