class Dhalang::Puppeteer

Contains common logic for interacting with Puppeteer.

Constants

DEFAULT_JPEG_OPTIONS
DEFAULT_PDF_OPTIONS
DEFAULT_PNG_OPTIONS
NODE_MODULES_PATH
USER_OPTIONS

Public Class Methods

visit(page_url, script_path, temp_file_path, temp_file_extension, options) click to toggle source

Launches a new Node process, executing the (Puppeteer) script under the given script_path.

@param [String] page_url The url to pass to the goTo method of Puppeteer. @param [String] script_path The absolute path of the JS script to execute. @param [String] temp_file_path The absolute path of the temp file to use to write any actions from Puppeteer. @param [String] temp_file_extension The extension of the temp file. @param [Object] options Set of options to use, configurable by the user.

# File lib/Dhalang/puppeteer.rb, line 60
def self.visit(page_url, script_path, temp_file_path, temp_file_extension, options)
    configuration = create_configuration(page_url, script_path, temp_file_path, temp_file_extension, options)

    command = "node #{script_path} #{Shellwords.escape(configuration)}"

    Open3.popen2e(command) do |_stdin, stdouterr, wait|
        return nil if wait.value.success?

        output = stdouterr.read.strip
        output = nil if output == ''
        message = output || "Exited with status #{wait.value.exitstatus}"
        raise DhalangError, message
    end
end

Private Class Methods

create_configuration(page_url, script_path, temp_file_path, temp_file_extension, options) click to toggle source

Returns a JSON string with the configuration to use within the Puppeteer script.

@param [String] page_url The url to pass to the goTo method of Puppeteer. @param [String] script_path The absolute path of the JS script to execute. @param [String] temp_file_path The absolute path of the temp file to use to write any actions from Puppeteer. @param [String] temp_file_extension The extension of the temp file. @param [Hash] options Set of options to use, configurable by the user.

# File lib/Dhalang/puppeteer.rb, line 83
                     def self.create_configuration(page_url, script_path, temp_file_path, temp_file_extension, options)
    {
        webPageUrl: page_url,
        tempFilePath: temp_file_path,
        puppeteerPath: NODE_MODULES_PATH,
        imageType: temp_file_extension,
        userOptions: USER_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value]}.to_h,
        pdfOptions: DEFAULT_PDF_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h,
        pngOptions: DEFAULT_PNG_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h,
        jpegOptions: DEFAULT_JPEG_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h
    }.to_json
end