class Dhalang::Screenshot

Allows consumers of this library to take screenshots with Puppeteer.

Constants

PUPPETEER_SCRIPT_PATH

Public Class Methods

get_from_url_as_jpeg(url, options = {}) click to toggle source

Captures a full JPEG screenshot of the webpage under the given url.

@param [String] url The url to take a screenshot of. @param [Hash] options User configurable options.

@return [String] the screenshot that was taken as binary.

# File lib/Screenshot.rb, line 13
def self.get_from_url_as_jpeg(url, options = {})
  get(url, "jpeg", options)
end
get_from_url_as_png(url, options = {}) click to toggle source

Captures a full PNG screenshot of the webpage under the given url.

@param [String] url The url to take a screenshot of. @param [Hash] options User configurable options.

@return [String] The screenshot that was taken as binary.

# File lib/Screenshot.rb, line 23
def self.get_from_url_as_png(url, options = {})
  get(url, "png", options)
end

Private Class Methods

get(url, image_type, options) click to toggle source

Groups and executes the logic for taking a screenhot of a webpage.

@param [String] url The url to take a screenshot of. @param [String] image_type The image type to use for storing the screenshot. @param [Hash] options Set of options to use, passed by the user of this library.

@return [String] The screenshot that was taken as binary.

# File lib/Screenshot.rb, line 34
                     def self.get(url, image_type, options)
  UrlUtils.validate(url)
  validate_options(options)
  temp_file = FileUtils.create_temp_file(image_type)
  begin
    Puppeteer.visit(url, PUPPETEER_SCRIPT_PATH, temp_file.path, image_type, options)
    binary_image_content = FileUtils.read_binary(temp_file.path)
  ensure
    FileUtils.delete(temp_file)
  end
  return binary_image_content
end
validate_options(options) click to toggle source

Raises an error if the given options might conflict with the Puppeteer configuration.

@param [Hash] options The options to validate

# File lib/Screenshot.rb, line 50
                     def self.validate_options(options)
  symbolized_options = options.transform_keys(&:to_sym)
  if symbolized_options.has_key?(:type)
    raise DhalangError, 'Invalid option set: "type"'
  end
end