Module: Hydra::FileCharacterization

Defined in:
lib/hydra/file_characterization.rb,
lib/hydra/file_characterization/version.rb,
lib/hydra/file_characterization/exceptions.rb,
lib/hydra/file_characterization/to_temp_file.rb,
lib/hydra/file_characterization/characterizer.rb,
lib/hydra/file_characterization/characterizers.rb

Defined Under Namespace

Modules: Characterizers Classes: Characterizer, Configuration, FileNotFoundError, ToTempFile, ToolNotFoundError, UnspecifiedToolPathError

Constant Summary collapse

VERSION =
"0.2.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



20
21
22
# File 'lib/hydra/file_characterization.rb', line 20

def configuration
  @configuration
end

Class Method Details

.characterize(content, filename, *tool_names) {|For| ... } ⇒ String+

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

Examples:

xml_string = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits)
xml_string = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits) do |config|
  config[:fits] = './really/custom/path/to/fits'
end
xml_string = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits) do |config|
  config[:fits] = lambda {|filename|  }
end
fits_xml, ffprobe_xml = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits, :ffprobe)

Parameters:

  • content (String)
    • The contents of the original file

  • filename (String)
    • The original file’s filename; Some

    characterization tools take hints from the file names

  • tool_names (Hash/Array)
    • A list of tool names available on the system

    if you provide a Hash

Yield Parameters:

  • For (Hash)

    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

Returns:

  • (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
    

See Also:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hydra/file_characterization.rb', line 58

def self.characterize(content, filename, *tool_names)
  tool_outputs = []
  tool_names = Array(tool_names).flatten.compact
  custom_paths = {}
  yield(custom_paths) if block_given?
  FileCharacterization::ToTempFile.open(content, filename) do |f|
    tool_names.each do |tool_name|
      tool_outputs << FileCharacterization.characterize_with(tool_name, f.path, custom_paths[tool_name])
    end
  end
  tool_names.size == 1 ? tool_outputs.first : tool_outputs
end

.characterize_with(tool_name, path_to_file, path_to_tool) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/hydra/file_characterization/characterizers.rb', line 19

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) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/hydra/file_characterization/characterizers.rb', line 6

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.new(tool_name)
  end
end

.characterizer_name_from(tool_name) ⇒ Object



15
16
17
# File 'lib/hydra/file_characterization/characterizers.rb', line 15

def characterizer_name_from(tool_name)
  tool_name.to_s.gsub(/(?:^|_)([a-z])/) { $1.upcase }
end

.configure {|configuration| ... } ⇒ Object

Yields:



71
72
73
74
# File 'lib/hydra/file_characterization.rb', line 71

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end