module Hydra::FileCharacterization

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

characterize(*args) { |custom_paths| ... } click to toggle source

Run all of the specified tools against the given content and filename.

@example

xml_string = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits)

@example

xml_string = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits) do |config|
  config[:fits] = './really/custom/path/to/fits'
end

@example

xml_string = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits) do |config|
  config[:fits] = lambda {|filename|  }
end

@example

fits_xml, ffprobe_xml = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits, :ffprobe)

@example With an open file

fits_xml, ffprobe_xml = Hydra::FileCharacterization.characterize(File.open('foo.mkv'), :fits, :ffprobe)

@example With an open file and a filename

fits_xml, ffprobe_xml = Hydra::FileCharacterization.characterize(File.open('foo.mkv'), 'my_movie.mkv', :fits, :ffprobe)

@param [String] content - The contents of the original file @param [String] filename - The original file’s filename; Some

characterization tools take hints from the file names

@param [Hash/Array] tool_names - A list of tool names available on the system

if you provide a Hash

@return [String, Array<String>] -

String - When a single tool_name is given, returns the raw XML as a
  string
Array<String> - When multiple tool_names are given, returns an equal
  length Array of XML strings

@yieldparam [Hash] For any of the specified tool_names, if you add a

key to the yieldparam with a value, that value will be used as the path

@see Hydra::FileCharacterization.configure

# File lib/hydra/file_characterization.rb, line 63
def self.characterize(*args)
  content, filename, tool_names = extract_arguments(args)
  tool_names = Array(tool_names).flatten.compact
  custom_paths = {}
  yield(custom_paths) if block_given?

  tool_outputs = run_characterizers(content, filename, tool_names, custom_paths)
  tool_names.size == 1 ? tool_outputs.first : tool_outputs
end
configure() { |configuration| ... } click to toggle source
# File lib/hydra/file_characterization.rb, line 73
def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end
extract_arguments(args) click to toggle source

Break up a list of arguments into two possible lists:

option1:  [String] content, [String] filename, [Array] tool_names
option2:  [File] content, [Array] tool_names

In the case of option2, derive the filename from the file’s path @return [String, File], [String], [Array]

# File lib/hydra/file_characterization.rb, line 83
def self.extract_arguments(args)
  content = args.shift
  filename = if content.is_a?(File) && !args[0].is_a?(String)
               File.basename(content.path)
             else
               args.shift
  end
  tool_names = args
  [content, filename, tool_names]
end
run_characterizers(content, filename, tool_names, custom_paths) click to toggle source

@param [File, String] content Either an open file or a string. If a string is passed

a temp file will be created

@param [String] filename Used in creating a temp file name @param [Array<Symbol>] tool_names A list of symbols referencing the characerization tools to run @param [Hash] custom_paths The paths to the executables of the tool.

# File lib/hydra/file_characterization.rb, line 99
def self.run_characterizers(content, filename, tool_names, custom_paths)
  if content.is_a? File
    run_characterizers_on_file(content, tool_names, custom_paths)
  else
    FileCharacterization::ToTempFile.open(filename, content) do |f|
      run_characterizers_on_file(f, tool_names, custom_paths)
    end
  end
end
run_characterizers_on_file(f, tool_names, custom_paths) click to toggle source
# File lib/hydra/file_characterization.rb, line 109
def self.run_characterizers_on_file(f, tool_names, custom_paths)
  tool_names.map do |tool_name|
    FileCharacterization.characterize_with(tool_name, f.path, custom_paths[tool_name])
  end
end

Public Instance Methods

characterize_with(tool_name, path_to_file, path_to_tool) click to toggle source
# File lib/hydra/file_characterization/characterizers.rb, line 21
def characterize_with(tool_name, path_to_file, path_to_tool)
  if path_to_tool.respond_to?(:call)
    path_to_tool.call(path_to_file)
  else
    tool_obj = characterizer(tool_name).new(path_to_file, path_to_tool)
    tool_obj.call
  end
end
characterizer(tool_name) click to toggle source
# File lib/hydra/file_characterization/characterizers.rb, line 8
def characterizer(tool_name)
  characterizer_name = characterizer_name_from(tool_name)
  if Characterizers.const_defined?(characterizer_name)
    Characterizers.const_get(characterizer_name)
  else
    raise ToolNotFoundError, tool_name
  end
end
characterizer_name_from(tool_name) click to toggle source
# File lib/hydra/file_characterization/characterizers.rb, line 17
def characterizer_name_from(tool_name)
  tool_name.to_s.gsub(/(?:^|_)([a-z])/) { Regexp.last_match(1).upcase }
end