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 hash of options, some of which are required:

  • :input_file or (:input and :input_type)

  • :output_file or :output_type



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
# File 'lib/media/transmogrifier.rb', line 47

def initialize(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



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

def command_output
  @command_output
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#input_fileObject

Returns the value of attribute input_file.



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

def input_file
  @input_file
end

#input_typeObject

Returns the value of attribute input_type.



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

def input_type
  @input_type
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#outputObject

maybe some day we return raw output via url?



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

def output
  @output
end

#output_fileObject

desired file location of the output



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

def output_file
  @output_file
end

#output_typeObject

desired mime type of the output



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

def output_type
  @output_type
end

Class Method Details

.add(trans) ⇒ Object



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

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

.command_available?(command) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.converts_from?(input_type) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/media/transmogrifier.rb', line 124

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

.converts_to?(output_type) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.find_class(input_type, output_type) ⇒ Object

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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/media/transmogrifier.rb', line 112

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 if transmog
  logger.error 'could not find a transmogrifier for "%s" -> "%s"' %
    [input_type, output_type]
  return nil
end

.inherited(base) ⇒ Object



86
87
88
# File 'lib/media/transmogrifier.rb', line 86

def self.inherited(base)
  add base
end

.input_mapObject

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



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

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

.listObject

CLASS METHODS



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

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

.output_mapObject

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



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

def self.output_map; @@output_map ||= Hash.new([]); 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
* the output to STDOUT and STDERR of the command


151
152
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
# File 'lib/media/transmogrifier.rb', line 151

def self.run_command(*args)

  # run the command
  cmdstr = command_string(*args)
  output = ""
  before = Time.now
  IO.popen(cmdstr + ' 2>&1', 'r') do |pipe|
    while line = pipe.gets
      if block_given?
        yield(line)
      end
      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 output
  else
    msg = ' exited with "%s"' % $?.exitstatus
    error cmdstr
    error msg
    error output if !output.empty?
    yield(msg) if block_given?
  end

  return status, output
end

Instance Method Details

#run_command(*args) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/media/transmogrifier.rb', line 189

def run_command(*args)
  status, self.command_output = self.class.run_command(*args)

  # 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