Class: RubberBand::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubberband/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file, output_file, options = Options.new) ⇒ Processor

Returns a new instance of Processor.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rubberband/processor.rb', line 8

def initialize(input_file, output_file, options = Options.new)
  @input_file = input_file
  @output_file = output_file
  @errors = []

  if options.is_a?(String) || options.is_a?(Options)
    @raw_options = options
  elsif options.is_a?(Hash)
    @raw_options = Options.new(options)
  else
    raise ArgumentError, "Unknown options format '#{options.class}', should be either EncodingOptions, Hash or String."
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/rubberband/processor.rb', line 6

def command
  @command
end

Instance Method Details

#binaryObject



22
23
24
25
26
27
28
29
# File 'lib/rubberband/processor.rb', line 22

def binary
  binary_path = `which rubberband`
  if $? == 0
    binary_path.strip
  else
    raise "rubberband binary not found!"
  end
end

#convert_succeeded?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/rubberband/processor.rb', line 49

def convert_succeeded?
  unless File.exists?(@output_file)
    @errors << "no output file created"
    return false
  end
  true
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubberband/processor.rb', line 31

def run
  @command = "#{binary} #{@raw_options} '#{Shellwords.escape(@input_file)}' '#{Shellwords.escape(@output_file)}'"
  output  = ""

  Open3.popen3(@command) do |stdin, stdout, stderr|
    stderr.each("r") do |line| 
      output << line
    end
  end

  if convert_succeeded?
    true
  else
    errors = @errors.empty? ? "" : " Errors: #{@errors.join(", ")}. "
    raise "Failed encoding: #{errors}. \nCommand: #{@command}. \nFull output: #{output}"
  end
end