grover 0.5.5 → 0.6.1

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/grover.rb +22 -23
  3. data/lib/grover/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1b134a553f30ceb3fa72cc1699eaa46457dc055
4
- data.tar.gz: d927e1b38d32982d7578657c67fb4ccfcd664e8a
3
+ metadata.gz: 6c26dbbef85a97e2a0efaaac225c73ef4621c3a2
4
+ data.tar.gz: 644ba163ea5e742ae0f9b245ed1d5a7381968865
5
5
  SHA512:
6
- metadata.gz: 80630d162e38555c0eec975009ade34104fcaa21addb3b2215ff57dc9f46625d5335254827161aa3d0654413e237d7cb775999709a5f1051ecf691f31f2bd912
7
- data.tar.gz: 32bf0d1016b22a24877404052165226c104c0f90bad13298fb69feeb18cfcb295f93ad80b02328ff137fd0448d1b1fac26d2c659511cc5cf120d877e3b1fcaf7
6
+ metadata.gz: 6eb0f8320d0fe7d53c13b9474a014cc9ab1fab300d70ef15c8dae1d6dfd45569005a3073f5339882b28898d6d297b26329c28a152dd9ae6a709bfc9e6de31d5d
7
+ data.tar.gz: 8135ebc0aae3ba810577be644f2efd184d799f79c943dd8c07d431eb9d1e8fb8eff8fa5a2b3277b17c01cd7554de907abd4b0570304588c99ac55c6782f87eb5
@@ -24,37 +24,52 @@ class Grover
24
24
  ENV['CI'] == 'true' ? "{args: ['--no-sandbox', '--disable-setuid-sandbox']}" : ''
25
25
  end
26
26
 
27
- method :convert_pdf, Utils.squish(<<-FUNCTION)
28
- async (url, options) => {
27
+ method :convert_pdf, <<-FUNCTION
28
+ async (url_or_html, options) => {
29
29
  let browser;
30
30
  try {
31
+ // Launch the browser and create a page
31
32
  browser = await puppeteer.launch(#{launch_params});
32
33
  const page = await browser.newPage();
33
34
 
35
+ // Set caching flag (if provided)
34
36
  const cache = options.cache; delete options.cache;
35
- if (cache != undefined) {
37
+ if (cache != undefined) {
36
38
  await page.setCacheEnabled(cache);
37
39
  }
38
40
 
41
+ // Setup timeout option (if provided)
39
42
  let request_options = {};
40
43
  const timeout = options.timeout; delete options.timeout;
41
44
  if (timeout != undefined) {
42
45
  request_options.timeout = timeout;
43
46
  }
44
47
 
45
- if (url.match(/^http/i)) {
48
+ if (url_or_html.match(/^http/i)) {
49
+ // Request is for a URL, so request it
46
50
  request_options.waitUntil = 'networkidle2';
47
- await page.goto(url, request_options);
51
+ await page.goto(url_or_html, request_options);
48
52
  } else {
53
+ // Request is some HTML content. Use request interception to assign the body
49
54
  request_options.waitUntil = 'networkidle0';
50
- await page.goto(`data:text/html,${url}`, request_options);
55
+ await page.setRequestInterception(true);
56
+ page.once('request', request => {
57
+ request.respond({ body: url_or_html });
58
+ // Reset the request interception
59
+ // (we only want to intercept the first request - ie our HTML)
60
+ page.on('request', request => request.continue());
61
+ });
62
+ const displayUrl = options.displayUrl; delete options.displayUrl;
63
+ await page.goto(displayUrl || 'http://example.com', request_options);
51
64
  }
52
65
 
66
+ // If specified, emulate the media type
53
67
  const emulateMedia = options.emulateMedia; delete options.emulateMedia;
54
68
  if (emulateMedia != undefined) {
55
69
  await page.emulateMedia(emulateMedia);
56
70
  }
57
71
 
72
+ // Return the converted PDF
58
73
  return await page.pdf(options);
59
74
  } finally {
60
75
  if (browser) {
@@ -66,11 +81,9 @@ class Grover
66
81
  end
67
82
  private_constant :Processor
68
83
 
69
- DISPLAY_URL_PLACEHOLDER = '{{display_url}}'.freeze
70
-
71
84
  DEFAULT_HEADER_TEMPLATE = "<div class='date text left'></div><div class='title text center'></div>".freeze
72
85
  DEFAULT_FOOTER_TEMPLATE = Utils.strip_heredoc(<<-HTML).freeze
73
- <div class='text left grow'>#{DISPLAY_URL_PLACEHOLDER}</div>
86
+ <div class='url text left grow'></div>
74
87
  <div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
75
88
  HTML
76
89
 
@@ -159,7 +172,6 @@ class Grover
159
172
  Utils.deep_merge! combined, Utils.deep_stringify_keys(options)
160
173
  Utils.deep_merge! combined, meta_options unless url_source?
161
174
 
162
- fix_templates! combined
163
175
  fix_boolean_options! combined
164
176
  fix_numeric_options! combined
165
177
 
@@ -190,19 +202,6 @@ class Grover
190
202
  @url.match(/^http/i)
191
203
  end
192
204
 
193
- def fix_templates!(options)
194
- display_url = options.delete 'display_url'
195
- return unless display_url
196
-
197
- options['footer_template'] ||= DEFAULT_FOOTER_TEMPLATE
198
-
199
- %w[header_template footer_template].each do |key|
200
- next unless options[key].is_a? ::String
201
-
202
- options[key] = options[key].gsub(DISPLAY_URL_PLACEHOLDER, display_url)
203
- end
204
- end
205
-
206
205
  def fix_boolean_options!(options)
207
206
  %w[display_header_footer print_background landscape prefer_css_page_size].each do |opt|
208
207
  next unless options.key? opt
@@ -1,3 +1,3 @@
1
1
  class Grover
2
- VERSION = '0.5.5'.freeze
2
+ VERSION = '0.6.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grover
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich