Class: Plaintext::ExternalCommandHandler

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

Constant Summary collapse

FILE_PLACEHOLDER =
'__FILE__'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileHandler

#set

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 40

def self.available?
  new.available?
end

Instance Method Details

#accept?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 32

def accept?(content_type)
  super and available?
end

#available?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/plaintext/file_handler/external_command_handler.rb', line 36

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/plaintext/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, options = {}) ⇒ Object



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

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