Class: TextExtractor::ExternalCommandHandler

Inherits:
FileHandler
  • Object
show all
Defined in:
lib/text_extractor/file_handler/external_command_handler.rb

Constant Summary collapse

FILE_PLACEHOLDER =
'__FILE__'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/text_extractor/file_handler/external_command_handler.rb', line 39

def self.available?
  new.available?
end

Instance Method Details

#accept?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/text_extractor/file_handler/external_command_handler.rb', line 31

def accept?(content_type)
  super and available?
end

#available?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/text_extractor/file_handler/external_command_handler.rb', line 35

def available?
  @command.present? and File.executable?(@command[0])
end

#shellout(cmd, options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/text_extractor/file_handler/external_command_handler.rb', line 14

def shellout(cmd, options = {}, &block)
  mode = "r+"
  IO.popen(cmd, mode) do |io|
    io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
    io.close_write unless options[:write_stdin]
    block.call(io) if block_given?
  end
end

#text(file) ⇒ Object



25
26
27
28
29
# File 'lib/text_extractor/file_handler/external_command_handler.rb', line 25

def text(file)
  cmd = @command.dup
  cmd[cmd.index(FILE_PLACEHOLDER)] = Pathname(file).to_s
  shellout(cmd){ |io| io.read }.to_s
end