Class: Media::Transmogrifier

Inherits:
Object
  • Object
show all
Defined in:
lib/media/transmogrifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Transmogrifier

takes a has of options, some of which are required:

  • :input_file or (:input and :input_type)

  • :output_file or :output_type



46
47
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
79
80
81
82
83
84
85
86
87
88
# File 'lib/media/transmogrifier.rb', line 46

def initialize(options=nil)
  self.name = self.class.to_s
  # we keep an instance in the transmogrifier list.
  # They only need to have the name set.
  return if options.nil?

  options = options.dup
  self.input       = options.delete(:input)
  self.input_file  = options.delete(:input_file)
  self.input_type  = options.delete(:input_type)
  self.output_file = options.delete(:output_file)
  self.output_type = options.delete(:output_type)
  self.options = options



  if input and input_type.nil?
    raise ArgumentError.new('input_type required if input specified')
  elsif input and input_file.nil?
    self.input_file = Media::TempFile.new(input, input_type)
  elsif input and input_file
    raise ArgumentError.new('cannot have both input and input_file')
  elsif input_file and input_type.nil?
    self.input_type = Media::MimeType.mime_type_from_extension(input_file)
  elsif input.nil? and input_file.nil?
    raise ArgumentError.new('input or input_file is required')
  end

  if output_file.nil? and output_type.nil?
    raise ArgumentError.new('output_file or output_type is required')
  elsif output_file.nil?
    self.output_file = Media::TempFile.new(nil, output_type)
  elsif output_type.nil?
    self.output_type = Media::MimeType.mime_type_from_extension(output_file)
  end

  debug self.class.name + " converting" +
    " #{input_file } ( #{input_type} ) to" +
    " #{output_file} ( #{output_type} )"

  set_temporary_outfile

end

Instance Attribute Details

#command_outputObject

output of last command run



38
39
40
# File 'lib/media/transmogrifier.rb', line 38

def command_output
  @command_output
end

#inputObject

Returns the value of attribute input.



28
29
30
# File 'lib/media/transmogrifier.rb', line 28

def input
  @input
end

#input_fileObject

Returns the value of attribute input_file.



29
30
31
# File 'lib/media/transmogrifier.rb', line 29

def input_file
  @input_file
end

#input_typeObject

Returns the value of attribute input_type.



30
31
32
# File 'lib/media/transmogrifier.rb', line 30

def input_type
  @input_type
end

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/media/transmogrifier.rb', line 26

def name
  @name
end

#optionsObject

Returns the value of attribute options.



36
37
38
# File 'lib/media/transmogrifier.rb', line 36

def options
  @options
end

#outputObject

maybe some day we return raw output via url?



32
33
34
# File 'lib/media/transmogrifier.rb', line 32

def output
  @output
end

#output_fileObject

desired file location of the output



34
35
36
# File 'lib/media/transmogrifier.rb', line 34

def output_file
  @output_file
end

#output_typeObject

desired mime type of the output



33
34
35
# File 'lib/media/transmogrifier.rb', line 33

def output_type
  @output_type
end

Class Method Details

.add(trans) ⇒ Object



108
109
110
# File 'lib/media/transmogrifier.rb', line 108

def self.add(trans)
  self.list[trans.name] ||= trans
end

.find_class(input_type, output_type) ⇒ Object

returns transmogrifier class, if any, that can tranform input_type into output_type



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/media/transmogrifier.rb', line 116

def self.find_class(input_type, output_type)
  transmog = list.values.
    select{|tm| tm.converts_from?(input_type)}.
    select{|tm| tm.converts_to?(output_type)}.
    select{|tm| tm.available?}.
    first
  return transmog.class if transmog
  logger.error 'could not find a transmogrifier for "%s" -> "%s"' %
    [input_type, output_type]
  return nil
end

.inherited(base) ⇒ Object



90
91
92
# File 'lib/media/transmogrifier.rb', line 90

def self.inherited(base)
  add(base.new)
end

.input_mapObject

maps mine type to an array of transmogrifiers that take that type as an imput



102
# File 'lib/media/transmogrifier.rb', line 102

def self.input_map; @@input_map ||= Hash.new([]); end

.listObject

CLASS METHODS



98
# File 'lib/media/transmogrifier.rb', line 98

def self.list(); @@list ||= {}; end

.output_mapObject

maps mine type to an array of transmogrifiers that produce that type as an output



106
# File 'lib/media/transmogrifier.rb', line 106

def self.output_map; @@output_map ||= Hash.new([]); end

Instance Method Details

#command_available?(command) ⇒ Boolean

def self.command_available?(command)

command.present? and File.file?(command) and File.executable?(command)

end

Returns:

  • (Boolean)


202
203
204
205
206
# File 'lib/media/transmogrifier.rb', line 202

def command_available?(command)
  command and
  File.file?(command) and
  File.executable?(command)
end

#converts_from?(input_type) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/media/transmogrifier.rb', line 128

def converts_from?(input_type)
  input_types.include? input_type
end

#converts_to?(output_type) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/media/transmogrifier.rb', line 132

def converts_to?(output_type)
  output_types.include? output_type
end

#run_command(*args) ⇒ Object

runs a shell command, passing each line that is output, as it is output to the block.

returns the status of the command, as one of the following symbols: :success, :failure, :not_found, :error



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/media/transmogrifier.rb', line 153

def run_command(*args)

  # run the command
  cmdstr = command_string(*args)
  self.command_output = ""
  before = Time.now
  IO.popen(cmdstr + ' 2>&1', 'r') do |pipe|
    while line = pipe.gets
      if block_given?
        yield(line)
      end
      self.command_output << line << "\n"
    end
  end
  took = Time.now - before

  # set the status
  status = case $?.exitstatus
    when 0 then :success
    when 1 then :failure
    when 127 then :not_found
    else :error
  end
  if status == :success
    log_command cmdstr
    debug "took #{took} seconds."
    debug command_output
  else
    msg = ' exited with "%s"' % $?.exitstatus
    error cmdstr
    error msg
    error command_output if command_output.present?
    yield(msg) if block_given?
  end

  # restore the original output_file name
  unless restore_temporary_outfile
    msg = 'could not restore temporary outfile'
    error msg
    yield(msg) if block_given?
    status = :failure
  end
  return status
end