Class: Plaintext::ExternalCommandHandler

Inherits:
FileHandler show all
Defined in:
lib/plaintext/file_handler/external_command_handler.rb

Constant Summary collapse

FILE_PLACEHOLDER =
'__FILE__'.freeze
DEFAULT_STREAM_ENCODING =
'ASCII-8BIT'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileHandler

#set

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 42

def self.available?
  new.available?
end

Instance Method Details

#accept?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 34

def accept?(content_type)
  super and available?
end

#available?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 38

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

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



18
19
20
21
22
23
24
25
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 18

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

#text(file, options = {}) ⇒ Object



27
28
29
30
31
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 27

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