Class: Dhalang::Screenshot

Inherits:
Object
  • Object
show all
Defined in:
lib/Screenshot.rb

Overview

Allows consumers of this library to take screenshots with Puppeteer.

Class Method Summary collapse

Class Method Details

.get_from_url(url, image_type, options = {}) ⇒ String

Captures ascreenshot of the webpage under the given url.

Parameters:

  • url (String)

    The url to take a screenshot of.

  • image_type (String)

    The image type (JPEG/PNG/WEBP) to use for storing the screenshot.

  • options (Hash) (defaults to: {})

    User configurable options.

Returns:

  • (String)

    The screenshot that was taken as binary.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/Screenshot.rb', line 40

def self.get_from_url(url, image_type, options = {})
  UrlUtils.validate(url)
  validate_image_type(image_type)
  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

.get_from_url_as_jpeg(url, options = {}) ⇒ String

DEPRECATED: Please use `get_from_url(url, :jpeg)` instead. Captures a full JPEG screenshot of the webpage under the given url.

Parameters:

  • url (String)

    The url to take a screenshot of.

  • options (Hash) (defaults to: {})

    User configurable options.

Returns:

  • (String)

    the screenshot that was taken as binary.



16
17
18
19
# File 'lib/Screenshot.rb', line 16

def self.get_from_url_as_jpeg(url, options = {})
  warn "[DEPRECATION] `get_from_url_as_jpeg` is deprecated.  Use `get_from_url(url, :jpeg)` instead."
  get_from_url(url, :jpeg, options)
end

.get_from_url_as_png(url, options = {}) ⇒ String

DEPRECATED: Please use `get_from_url(url, :png)` instead. Captures a full PNG screenshot of the webpage under the given url.

Parameters:

  • url (String)

    The url to take a screenshot of.

  • options (Hash) (defaults to: {})

    User configurable options.

Returns:

  • (String)

    The screenshot that was taken as binary.



28
29
30
31
# File 'lib/Screenshot.rb', line 28

def self.get_from_url_as_png(url, options = {})
  warn "[DEPRECATION] `get_from_url_as_png` is deprecated.  Use `get_from_url(url, :png)` instead."
  get_from_url(url, :png, options)
end