Dhalang 0.6.4 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70f9719f301ba33dc769df19417b0b597adc574a9c7df54ed710f4f23d19e2a0
4
- data.tar.gz: d7d6b46261a0dc227e9fb26cc79a8c7377c9148d464c2ab8e3e383d2f7115c7a
3
+ metadata.gz: 580f62daf557818f3de686d07fc10f3fa07705025ea562e9e29130a7d0387660
4
+ data.tar.gz: e237333306eac8cfc4c5a9fd0d1cd6d59a1f99dcee0a632cc71e0e5ae4b44b8a
5
5
  SHA512:
6
- metadata.gz: 69b4cede399f0200064ec2ed56df24e42826f7b112d67d3026ba04f40139d02a72ff62bc407be94d10116adffc701ff8977a253e9ac56783402c3220d5d9989d
7
- data.tar.gz: ea4f6c687f7fa8ab3951883ae8cbec53f0a23a71c118c3f742605e3ca4dad9b398c2d718c776718cf89ab43fb331cebe2442d7db9e1ee0f5cf9e25815567120d
6
+ metadata.gz: 8054c0e43bd1ff71d6072ccf2cc4f3c98cf2a0a5f073c5e4b8cd75c2fc00c63cca60ab505d461533fae147e4da4b6786109ab0f00e70eaa4cfca520c19f1874e
7
+ data.tar.gz: 7e5a64263595fec4f5e3c47a13f4a68507b94ee8f3b79e1fa2c4445b13e756deeb374944f1d406f65c2fae6e1ccf76de20a5a8b0f7ce5b8691592265f1523fa2
@@ -0,0 +1,20 @@
1
+ name: Build
2
+ on:
3
+ - push
4
+ - pull_request
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v4
10
+ - uses: ruby/setup-ruby@v1
11
+ with:
12
+ ruby-version: 3.1.3
13
+ bundler-cache: true
14
+ - uses: actions/setup-node@v4
15
+ with:
16
+ node-version: 18
17
+ cache: 'npm'
18
+ cache-dependency-path: '**/package-lock.json'
19
+ - run: npm ci
20
+ - run: bundle exec rake spec
@@ -0,0 +1,23 @@
1
+ name: Publish
2
+ on:
3
+ release:
4
+ types:
5
+ - published
6
+ jobs:
7
+ publish:
8
+ runs-on: ubuntu-latest
9
+ env:
10
+ GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
11
+ steps:
12
+ - uses: actions/checkout@v4
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 3.1.3
16
+ bundler-cache: true
17
+ - uses: actions/setup-node@v4
18
+ with:
19
+ node-version: 18
20
+ - run: npm ci
21
+ - run: bundle exec rake spec
22
+ - run: gem build -o Dhalang.gem Dhalang.gemspec
23
+ - run: gem push Dhalang.gem
data/.gitignore CHANGED
@@ -50,3 +50,4 @@ build-iPhoneSimulator/
50
50
 
51
51
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
52
52
  .rvmrc
53
+ .npmrc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- Dhalang (0.6.4)
4
+ Dhalang (0.6.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -11,32 +11,34 @@ GEM
11
11
  diff-lcs (1.5.0)
12
12
  fastimage (2.2.6)
13
13
  hashery (2.1.2)
14
- pdf-reader (2.9.0)
14
+ pdf-reader (2.9.2)
15
15
  Ascii85 (~> 1.0)
16
16
  afm (~> 0.2.1)
17
17
  hashery (~> 2.0)
18
18
  ruby-rc4
19
19
  ttfunk
20
20
  rake (13.0.6)
21
- rspec (3.10.0)
22
- rspec-core (~> 3.10.0)
23
- rspec-expectations (~> 3.10.0)
24
- rspec-mocks (~> 3.10.0)
25
- rspec-core (3.10.2)
26
- rspec-support (~> 3.10.0)
27
- rspec-expectations (3.10.2)
21
+ rspec (3.11.0)
22
+ rspec-core (~> 3.11.0)
23
+ rspec-expectations (~> 3.11.0)
24
+ rspec-mocks (~> 3.11.0)
25
+ rspec-core (3.11.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-expectations (3.11.0)
28
28
  diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.10.0)
30
- rspec-mocks (3.10.3)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-mocks (3.11.1)
31
31
  diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.10.0)
33
- rspec-support (3.10.3)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-support (3.11.0)
34
34
  ruby-rc4 (0.1.5)
35
35
  ttfunk (1.7.0)
36
36
 
37
37
  PLATFORMS
38
38
  arm64-darwin-21
39
39
  x86-mingw32
40
+ x86_64-darwin-20
41
+ x86_64-linux
40
42
 
41
43
  DEPENDENCIES
42
44
  Dhalang!
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Dhalang [![Build Status](https://travis-ci.com/NielsSteensma/Dhalang.svg?token=XZgKAByw2KZjcrsCh8gW&branch=master)](https://travis-ci.com/NielsSteensma/Dhalang)
1
+ # Dhalang [![Build](https://github.com/NielsSteensma/Dhalang/actions/workflows/build.yml/badge.svg)](https://github.com/NielsSteensma/Dhalang/actions/workflows/build.yml)
2
2
 
3
3
  > Dhalang is a Ruby wrapper for Google's Puppeteer.
4
4
 
@@ -24,7 +24,7 @@ Install puppeteer in your application's root directory:
24
24
 
25
25
  $ npm install puppeteer
26
26
 
27
- <sub>NodeJS v10.18.1 or greater is required</sub>
27
+ <sub>Dhalang and Puppeteer require Node ≥ 18 and Puppeteer 22</sub>
28
28
  ## Usage
29
29
  __Get a PDF of a website url__
30
30
  ```ruby
@@ -39,14 +39,20 @@ Dhalang::PDF.get_from_html("<html><head></head><body><h1>examplestring</h1></bod
39
39
 
40
40
  __Get a PNG screenshot of a website__
41
41
  ```ruby
42
- Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
42
+ Dhalang::Screenshot.get_from_url("https://www.google.com", :png)
43
43
  ```
44
44
 
45
45
  __Get a JPEG screenshot of a website__
46
46
  ```ruby
47
- Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
47
+ Dhalang::Screenshot.get_from_url("https://www.google.com", :jpeg)
48
48
  ```
49
- All methods return a string containing the PDF or JPEG/PNG in binary.
49
+
50
+ __Get a WEBP screenshot of a website__
51
+ ```ruby
52
+ Dhalang::Screenshot.get_from_url("https://www.google.com", :webp)
53
+ ```
54
+
55
+ All methods return a string containing the PDF or JPEG/PNG/WEBP in binary.
50
56
 
51
57
 
52
58
 
@@ -60,7 +66,7 @@ Dhalang::PDF.get_from_url("https://www.google.com", {margin: { top: 100, right:
60
66
 
61
67
  For example to only take a screenshot of the visible part of the page:
62
68
  ```ruby
63
- Dhalang::Screenshot.get_from_url_as_png("https://www.google.com", {fullPage: false})
69
+ Dhalang::Screenshot.get_from_url("https://www.google.com", :webp, {fullPage: false})
64
70
  ```
65
71
 
66
72
  A list of all possible PDF options that can be set, can be found at: https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions
@@ -80,7 +86,7 @@ You may want to change the way Dhalang interacts with Puppeteer in general. User
80
86
 
81
87
  For example to set a custom navigation timeout:
82
88
  ```ruby
83
- Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com", {navigationTimeout: 20000})
89
+ Dhalang::Screenshot.get_from_url("https://www.google.com", :jpeg, {navigationTimeout: 20000})
84
90
  ```
85
91
 
86
92
  Below table lists all possible configuration parameters that can be set:
@@ -107,18 +113,10 @@ def example_controller_method
107
113
  end
108
114
  ```
109
115
 
110
- To return a PNG from a Rails controller you can do the following:
116
+ To return a screenshot from a Rails controller you can do the following:
111
117
  ```ruby
112
118
  def example_controller_method
113
- binary_png = Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
119
+ binary_png = Dhalang::Screenshot.get_from_url("https://www.google.com", :png)
114
120
  send_data(binary_png, filename: 'screenshotofgoogle.png', type: 'image/png')
115
121
  end
116
- ```
117
-
118
- To return a JPEG from a Rails controller you can do the following:
119
- ```ruby
120
- def example_controller_method
121
- binary_jpeg = Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
122
- send_data(binary_jpeg, filename: 'screenshotofgoogle.jpeg', type: 'image/jpeg')
123
- end
124
- ```
122
+ ```
@@ -38,18 +38,15 @@ module Dhalang
38
38
  }
39
39
  private_constant :DEFAULT_PDF_OPTIONS
40
40
 
41
- DEFAULT_PNG_OPTIONS = {
41
+ DEFAULT_SCREENSHOT_OPTIONS = {
42
42
  fullPage: true,
43
43
  clip: nil,
44
44
  omitBackground: false
45
45
  }
46
- private_constant :DEFAULT_PNG_OPTIONS
46
+ private_constant :DEFAULT_SCREENSHOT_OPTIONS
47
47
 
48
48
  DEFAULT_JPEG_OPTIONS = {
49
- quality: 100,
50
- fullPage: true,
51
- clip: nil,
52
- omitBackground: false
49
+ quality: 100
53
50
  }
54
51
  private_constant :DEFAULT_JPEG_OPTIONS
55
52
 
@@ -92,7 +89,7 @@ module Dhalang
92
89
  imageType: temp_file_extension,
93
90
  userOptions: USER_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value]}.to_h,
94
91
  pdfOptions: DEFAULT_PDF_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h,
95
- pngOptions: DEFAULT_PNG_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h,
92
+ screenshotOptions: DEFAULT_SCREENSHOT_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h,
96
93
  jpegOptions: DEFAULT_JPEG_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h
97
94
  }.to_json
98
95
  end
@@ -1,3 +1,3 @@
1
1
  module Dhalang
2
- VERSION = "0.6.4"
2
+ VERSION = "0.6.6"
3
3
  end
data/lib/Screenshot.rb CHANGED
@@ -2,8 +2,11 @@ module Dhalang
2
2
  # Allows consumers of this library to take screenshots with Puppeteer.
3
3
  class Screenshot
4
4
  PUPPETEER_SCRIPT_PATH = File.expand_path('../js/screenshot-generator.js', __FILE__).freeze
5
+ IMAGE_TYPES = [:jpeg, :png, :webp].freeze
5
6
  private_constant :PUPPETEER_SCRIPT_PATH
7
+ private_constant :IMAGE_TYPES
6
8
 
9
+ # <b>DEPRECATED:</b> Please use `get_from_url(url, :jpeg)` instead.
7
10
  # Captures a full JPEG screenshot of the webpage under the given url.
8
11
  #
9
12
  # @param [String] url The url to take a screenshot of.
@@ -11,9 +14,11 @@ module Dhalang
11
14
  #
12
15
  # @return [String] the screenshot that was taken as binary.
13
16
  def self.get_from_url_as_jpeg(url, options = {})
14
- get(url, "jpeg", options)
17
+ warn "[DEPRECATION] `get_from_url_as_jpeg` is deprecated. Use `get_from_url(url, :jpeg)` instead."
18
+ get_from_url(url, :jpeg, options)
15
19
  end
16
20
 
21
+ # <b>DEPRECATED:</b> Please use `get_from_url(url, :png)` instead.
17
22
  # Captures a full PNG screenshot of the webpage under the given url.
18
23
  #
19
24
  # @param [String] url The url to take a screenshot of.
@@ -21,19 +26,22 @@ module Dhalang
21
26
  #
22
27
  # @return [String] The screenshot that was taken as binary.
23
28
  def self.get_from_url_as_png(url, options = {})
24
- get(url, "png", options)
29
+ warn "[DEPRECATION] `get_from_url_as_png` is deprecated. Use `get_from_url(url, :png)` instead."
30
+ get_from_url(url, :png, options)
25
31
  end
26
-
27
- # Groups and executes the logic for taking a screenhot of a webpage.
32
+
33
+ # Captures ascreenshot of the webpage under the given url.
28
34
  #
29
35
  # @param [String] url The url to take a screenshot of.
30
- # @param [String] image_type The image type to use for storing the screenshot.
31
- # @param [Hash] options Set of options to use, passed by the user of this library.
36
+ # @param [String] image_type The image type (JPEG/PNG/WEBP) to use for storing the screenshot.
37
+ # @param [Hash] options User configurable options.
32
38
  #
33
39
  # @return [String] The screenshot that was taken as binary.
34
- private_class_method def self.get(url, image_type, options)
40
+ def self.get_from_url(url, image_type, options = {})
35
41
  UrlUtils.validate(url)
42
+ validate_image_type(image_type)
36
43
  validate_options(options)
44
+
37
45
  temp_file = FileUtils.create_temp_file(image_type)
38
46
  begin
39
47
  Puppeteer.visit(url, PUPPETEER_SCRIPT_PATH, temp_file.path, image_type, options)
@@ -44,6 +52,15 @@ module Dhalang
44
52
  return binary_image_content
45
53
  end
46
54
 
55
+ # Raises an error if the given image type is not supported.
56
+ #
57
+ # @param [String] image_type The image_type to validate
58
+ private_class_method def self.validate_image_type(image_type)
59
+ if !IMAGE_TYPES.include? image_type.downcase
60
+ raise DhalangError, 'Unsupported image type'
61
+ end
62
+ end
63
+
47
64
  # Raises an error if the given options might conflict with the Puppeteer configuration.
48
65
  #
49
66
  # @param [Hash] options The options to validate
data/lib/js/dhalang.js CHANGED
@@ -8,7 +8,7 @@ const fs = require('fs')
8
8
  * @property {string} imageType - The type of image to save ( undefined for pdfgenerator ).
9
9
  * @property {UserOptions} userOptions - User defined and default parameters to use when navigating to pages.
10
10
  * @property {Object} pdfOptions - User defined and default parameters to use when creating PDFs. Note: Do not use directly, rather use {@link getConfiguredPdfOptions}.
11
- * @property {Object} pngOptions - User defined and default parameters to use when creating PNGs.
11
+ * @property {Object} screenshotOptions - User defined and default parameters to use when creating screenshots.
12
12
  * @property {Object} jpegOptions - User defined and default parameters to use when creating JPEGs.
13
13
  */
14
14
 
@@ -96,7 +96,7 @@ exports.navigate = async function (page, configuration) {
96
96
  } else if (navigationWaitForXPath !== "") {
97
97
  await page.waitForXPath(navigationWaitForXPath, this.getWaitingParameters(configuration));
98
98
  } else {
99
- await page.waitForTimeout(250);
99
+ await new Promise(r => setTimeout(r, 250));
100
100
  }
101
101
  }
102
102
 
@@ -11,13 +11,13 @@ const createScreenshot = async () => {
11
11
  await dhalang.configure(page, configuration.userOptions);
12
12
  await dhalang.navigate(page, configuration);
13
13
 
14
- const screenshotOptions = configuration.imageType === "png" ? configuration.pngOptions : configuration.jpegOptions
15
14
  await page.screenshot({
16
15
  ...{
17
16
  path: configuration.tempFilePath,
18
17
  type: configuration.imageType,
19
18
  },
20
- ...screenshotOptions
19
+ ...(configuration.imageType === "jpeg") && configuration.jpegOptions,
20
+ ...configuration.screenshotOptions
21
21
  });
22
22
  } catch (error) {
23
23
  console.error(error.message);