class ChupaText::Decomposers::AbiWord

Constants

EXTENSIONS
MIME_TYPES

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/chupa-text/decomposers/abiword.rb, line 42
def initialize(options)
  super
  @command = find_command
  debug do
    if @command
      "#{log_tag}[command][found] #{@command.path}"
    else
      "#{log_tag}[command][not-found]"
    end
  end
end

Public Instance Methods

decompose(data) { |text_data(read, source_data: data)| ... } click to toggle source
# File lib/chupa-text/decomposers/abiword.rb, line 68
def decompose(data)
  create_tempfiles(data) do |text, stdout, stderr|
    succeeded = @command.run("--to", "text",
                             "--to-name", text.path,
                             data.path.to_s,
                             {
                               data: data,
                               spawn_options: {
                                 out: stdout.path,
                                 err: stderr.path,
                               },
                             })
    unless succeeded
      error do
        tag = "#{log_tag}[convert][exited][abnormally]"
        [
          tag,
          "output: <#{stdout.read}>",
          "error: <#{stderr.read}>",
        ].join("\n")
      end
      return
    end
    File.open(text.path) do |text_input|
      yield(TextData.new(text_input.read, source_data: data))
    end
  end
end
target?(data) click to toggle source
# File lib/chupa-text/decomposers/abiword.rb, line 54
def target?(data)
  return false if @command.nil?
  EXTENSIONS.include?(data.extension) or
    MIME_TYPES.include?(data.mime_type)
end
target_score(data) click to toggle source
# File lib/chupa-text/decomposers/abiword.rb, line 60
def target_score(data)
  if target?(data)
    10
  else
    nil
  end
end

Private Instance Methods

create_tempfiles(data) { |text, stdout, stderr| ... } click to toggle source
# File lib/chupa-text/decomposers/abiword.rb, line 112
def create_tempfiles(data)
  basename = File.basename(data.path)
  text = Tempfile.new([basename, ".txt"])
  stdout = Tempfile.new([basename, ".stdout.log"])
  stderr = Tempfile.new([basename, ".stderr.log"])
  begin
    yield(text, stdout, stderr)
  ensure
    text.close!
    stdout.close!
    stderr.close!
  end
end
find_command() click to toggle source
# File lib/chupa-text/decomposers/abiword.rb, line 98
def find_command
  candidates = [
    @options[:abiword],
    ENV["ABIWORD"],
    "abiword",
  ]
  candidates.each do |candidate|
    next if candidate.nil?
    command = ExternalCommand.new(candidate)
    return command if command.exist?
  end
  nil
end
log_tag() click to toggle source
# File lib/chupa-text/decomposers/abiword.rb, line 126
def log_tag
  "[decomposer][abiword]"
end