Class: Attached::Processor::Audio

Inherits:
Base
  • Object
show all
Defined in:
lib/attached/processor/audio.rb

Instance Attribute Summary (collapse)

Attributes inherited from Base

#file, #options

Instance Method Summary (collapse)

Methods inherited from Base

process

Constructor Details

- (Audio) initialize(file, options = {}, attachment = nil)

Create a processor.

Parameters:

  • file - The file to be processed.

  • options - The options to be applied to the processing.

  • attachment - The attachment the processor is being run for.



23
24
25
26
27
28
29
30
31
32
# File 'lib/attached/processor/audio.rb', line 23

def initialize(file, options = {}, attachment = nil)
  super
  
  @path      = self.file.path
  
  @preset    = options[:preset]
  @extension = options[:extension]
  
  @extension ||= self.attachment.extension
end

Instance Attribute Details

- (Object) attachment (readonly)

Returns the value of attribute attachment



12
13
14
# File 'lib/attached/processor/audio.rb', line 12

def attachment
  @attachment
end

- (Object) extension (readonly)

Returns the value of attribute extension



10
11
12
# File 'lib/attached/processor/audio.rb', line 10

def extension
  @extension
end

- (Object) path (readonly)

Returns the value of attribute path



9
10
11
# File 'lib/attached/processor/audio.rb', line 9

def path
  @path
end

- (Object) preset (readonly)

Returns the value of attribute preset



11
12
13
# File 'lib/attached/processor/audio.rb', line 11

def preset
  @preset
end

Instance Method Details

- (Object) process

Helper function for calling processors.

Usage:

self.process


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/attached/processor/audio.rb', line 48

def process
  
  result = Tempfile.new(["", self.extension])
  result.binmode
  
  begin
  
    parameters = []
    
    parameters << "--preset #{self.preset}" if self.preset
    
    parameters << self.path
    parameters << result.path
    
    parameters = parameters.join(" ").squeeze(" ")
    
    `lame #{parameters} #{redirect}`
    
    raise Errno::ENOENT if $?.exitstatus == 127
    
  rescue Errno::ENOENT  
    raise "command 'lame' not found: ensure LAME is installed"
  end
  
  unless $?.exitstatus == 0
    raise Attached::Processor::Error, "must be an audio file"
  end
  
  return result
  
end

- (Object) redirect

Redirect output path.



37
38
39
# File 'lib/attached/processor/audio.rb', line 37

def redirect
  ">/dev/null 2>&1" if File.exist?("/dev/null")
end