Dhalang 0.6.4 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70f9719f301ba33dc769df19417b0b597adc574a9c7df54ed710f4f23d19e2a0
4
- data.tar.gz: d7d6b46261a0dc227e9fb26cc79a8c7377c9148d464c2ab8e3e383d2f7115c7a
3
+ metadata.gz: 35f7dcd3cc7787c9e13429033b9c177d6f5c82f0b94364a2d4efbe3ffe601a16
4
+ data.tar.gz: b204fe4df6c7b3e0da1e7d59407a711a9bd1d9f41c7828b05eccc478499e2b0b
5
5
  SHA512:
6
- metadata.gz: 69b4cede399f0200064ec2ed56df24e42826f7b112d67d3026ba04f40139d02a72ff62bc407be94d10116adffc701ff8977a253e9ac56783402c3220d5d9989d
7
- data.tar.gz: ea4f6c687f7fa8ab3951883ae8cbec53f0a23a71c118c3f742605e3ca4dad9b398c2d718c776718cf89ab43fb331cebe2442d7db9e1ee0f5cf9e25815567120d
6
+ metadata.gz: 25c582e213b4b26851e59859ab24115f136ea02b4b2889eb99502a9b3d3ddc7688f667ffe656d4f79afb81a65dda0b81a2e482a8d310890a1baf5c11fd17dbc0
7
+ data.tar.gz: 3cd5d4ece1b4e8f87a59abeb781ae241e8b7a515665f6b5c57cc636ccb767a98ebc7a7be71f7746f379bf71d083aba33eb7003c524e9efa1d652d77a940b2656
data/Gemfile.lock CHANGED
@@ -11,32 +11,31 @@ 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
- arm64-darwin-21
39
- x86-mingw32
38
+ x86_64-darwin-20
40
39
 
41
40
  DEPENDENCIES
42
41
  Dhalang!
@@ -47,4 +46,4 @@ DEPENDENCIES
47
46
  rspec (~> 3.0)
48
47
 
49
48
  BUNDLED WITH
50
- 2.3.6
49
+ 2.3.15
data/README.md CHANGED
@@ -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.5"
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
 
@@ -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);
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Dhalang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Niels Steensma
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-18 00:00:00.000000000 Z
11
+ date: 2022-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler