Module: ChocolateDisco
- Defined in:
- lib/chocolate_disco.rb,
lib/chocolate_disco/java.rb,
lib/chocolate_disco/version.rb,
lib/chocolate_disco/chocolate_disco.rb
Defined Under Namespace
Constant Summary collapse
- @@supported_mime_types =
nil
Class Method Summary collapse
-
.extract(path, opts = {}) ⇒ Object
(also: lets_party!)
extracts text from path, returns string, empty string or nil.
-
.supported?(opts = {}) ⇒ Boolean
returns whether or not the file is supported.
-
.supported_mime_types ⇒ Object
returns an array of supported mime types.
Class Method Details
.extract(path, opts = {}) ⇒ Object Also known as: lets_party!
extracts text from path, returns string, empty string or nil.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chocolate_disco/chocolate_disco.rb', line 41 def extract(path, opts = {}) if opts[:force_mime].is_a?(String) mimes = [opts[:force_mime]] else mimes = MIME::Types.of(path).map { |mime| mime.simplified } end raise(NotImplementedError.new("MIME (#{mimes.join(', ')}) for '#{path}' is not supported by chocolate_disco.")) unless supported?(:mimes => mimes) extractor = mimes.map { |mime| Java::TextExtractionFactory.getExtractor(mime) }[0] raise(RuntimeError.new('TextExtractionFactory failed to get an instance of TextExtractor, which is impossible.')) if extractor.nil? extractor.extractText(java.io.File.new(path)) end |
.supported?(opts = {}) ⇒ Boolean
returns whether or not the file is supported. :path to specify the file. :mime to specify the mime. :mimes to give an array of mimes.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chocolate_disco/chocolate_disco.rb', line 18 def supported?(opts = {}) mimes = [] mimes << opts[:mime] if opts[:mime].is_a?(String) mimes += opts[:mimes] if opts[:mimes].is_a?(Array) if opts[:path].is_a?(String) && File.exists?(opts[:path]) && !File.directory?(opts[:path]) && File.file?(opts[:path]) begin mimes << MIME::Types.of(opts[:path]).map { |mime| mime.simplified } rescue NoMethodError # begin raise(NotImplementedError.new("MIME types of a file '#{opts[:path]}' could not be inferred.")) end # rescue end # if opts[:path].is_a?(String) && File.exists?(opts[:path]) && !File.directory?(opts[:path]) && File.file?(opts[:path]) mimes.flatten! mimes.compact! mimes.uniq! mimes.map { |mime| return true if supported_mime_types.include?(mime) } false end |
.supported_mime_types ⇒ Object
returns an array of supported mime types.
11 12 13 |
# File 'lib/chocolate_disco/chocolate_disco.rb', line 11 def supported_mime_types @@supported_mime_types ||= Java::SupportedMimeTypes.constants.map { |const| eval("Java::SupportedMimeTypes::#{const}") } end |