class ChupaText::Decomposers::WebKit::ExternalScreenshoter

Public Class Methods

new() click to toggle source
# File lib/chupa-text/decomposers/webkit.rb, line 92
def initialize
  @screenshoter = File.join(__dir__,
                            "..",
                            "..",
                            "..",
                            "bin",
                            "chupa-text-decomposer-webkit-screenshoter")
  @command = ExternalCommand.new(RbConfig.ruby)
end

Public Instance Methods

run(html_path, uri, output_path, width, height) click to toggle source
# File lib/chupa-text/decomposers/webkit.rb, line 102
def run(html_path, uri, output_path, width, height)
  output_read, output_write = IO.pipe
  error_output = Tempfile.new("chupa-text-decomposer-webkit-error")
  output_reader = Thread.new do
    loop do
      IO.select([output_read])
      line = output_read.gets
      break if line.nil?

      case line.chomp
      when /\Adebug: /
        debug($POSTMATCH)
      when /\Aerror: /
        error($POSTMATCH)
      end
    end
  end
  successed = @command.run(@screenshoter,
                           html_path,
                           uri,
                           output_path,
                           width.to_s,
                           height.to_s,
                           {
                             :spawn_options => {
                               :out => output_write,
                               :err => error_output.path,
                             },
                           })
  output_write.close
  output_reader.join

  unless successed
    error do
      message = "failed to external screenshoter: #{uri}: "
      message << "#{@command.path} #{@screenshoter}"
      "#{log_tag}[external-screenshoter][run][failed] #{message}"
    end
  end
  unless error_output.size.zero?
    error_output.each_line do |line|
      error(line)
    end
  end
end