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
Constant Summary collapse
- VERSION =
"1.2.0"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
-
.characterize(*args) {|For| ... } ⇒ String+
Run all of the specified tools against the given content and filename.
- .characterize_with(tool_name, path_to_file, path_to_tool) ⇒ Object
- .characterizer(tool_name) ⇒ Object
- .characterizer_name_from(tool_name) ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.extract_arguments(args) ⇒ String, File
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.
- .run_characterizers(content, filename, tool_names, custom_paths) ⇒ Object
- .run_characterizers_on_file(f, tool_names, custom_paths) ⇒ Object
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
19 20 21 |
# File 'lib/hydra/file_characterization.rb', line 19 def configuration @configuration end |
Class Method Details
.characterize(*args) {|For| ... } ⇒ String+
Run all of the specified tools against the given content and filename.
63 64 65 66 67 68 69 70 71 |
# 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 |
.characterize_with(tool_name, path_to_file, path_to_tool) ⇒ Object
21 22 23 24 25 26 27 28 |
# 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) ⇒ Object
8 9 10 11 12 13 14 15 |
# 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) ⇒ Object
17 18 19 |
# 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 |
.configure {|configuration| ... } ⇒ Object
73 74 75 76 |
# File 'lib/hydra/file_characterization.rb', line 73 def self.configure self.configuration ||= Configuration.new yield(configuration) end |
.extract_arguments(args) ⇒ String, File
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
83 84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
109 110 111 112 113 |
# 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 |