grover 0.1.2 → 0.2.0

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
  SHA1:
3
- metadata.gz: 3a06e4797cf17f2c9d7e1e4d85188aa12c7a2eef
4
- data.tar.gz: 9ce156b3e3d80b6e14d98878264f124c4877b816
3
+ metadata.gz: cf861057bca3ab2b9c59bf0c52b329bb70cd1e5c
4
+ data.tar.gz: 910e24710cfd955ca779b600680ce7b50bc8458b
5
5
  SHA512:
6
- metadata.gz: e0f96d8f404409c1c6980bdc692ee97955ab9dd8bbd685ae945cc319b945231a6539b6bd07f48deb2b10f42d4ffed0eac4e5817b3d37cf81bdfe06e9fcb7103c
7
- data.tar.gz: e002838a7f7b42256545a4590fb4624a75b3425af3764eff7a78b5cae8f973e62930ea1db9db47977fcc6bf527df86489389d055957aea80b0ec24435f20b9ab
6
+ metadata.gz: 6c101f3d8bf4b9cdefa35ed26994e24498c9eede363ed798450b1cc849b288547b34e28ded856387bda3e3d20335b76aed0547527baaa5632a6e3164f5bbc1a8
7
+ data.tar.gz: 3fbc34457ebb9a151c85e01217ddc884baaac14c36532c0eec77856557188b7351389f6ec73cdc400ad2379589fccf158fd0b20c5f2626f27de5e850ffa7ff31
data/lib/grover.rb CHANGED
@@ -2,4 +2,52 @@ require 'grover/version'
2
2
 
3
3
  require 'grover/utils'
4
4
  require 'grover/processor'
5
- require 'grover/grover'
5
+ require 'grover/middleware'
6
+
7
+ #
8
+ # Grover interface for converting HTML to PDF
9
+ #
10
+ class Grover
11
+ #
12
+ # @param [String] url URL of the page to convert
13
+ # @param [Hash] options Optional parameters to pass to PDF processor
14
+ # see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
15
+ #
16
+ def initialize(url, options = {})
17
+ @url = url
18
+ @root_path = options.delete :root_path
19
+ @options = options
20
+ end
21
+
22
+ #
23
+ # Request URL with provided options and create PDF
24
+ #
25
+ # @param [String] path Optional path to write the PDF to
26
+ # @return [Array<Integer>] Byte array of the resulting PDF
27
+ #
28
+ def to_pdf(path = nil)
29
+ options = @options.dup
30
+ options[:path] = path if path
31
+ result = processor.convert_pdf @url, options
32
+ result['data'].pack('c*')
33
+ end
34
+
35
+ def inspect
36
+ format(
37
+ '#<%<class_name>s:0x%<object_id>p @url="%<url>s">',
38
+ class_name: self.class.name,
39
+ object_id: object_id,
40
+ url: url
41
+ )
42
+ end
43
+
44
+ private
45
+
46
+ def root_path
47
+ @root_path ||= Dir.pwd
48
+ end
49
+
50
+ def processor
51
+ Grover::Processor.new(root_path)
52
+ end
53
+ end
@@ -0,0 +1,55 @@
1
+ class Grover
2
+ #
3
+ # Rack middleware for catching PDF requests and returning the upstream HTML as a PDF
4
+ #
5
+ class Middleware
6
+ def initialize(app)
7
+ @app = app
8
+ @render_pdf = false
9
+ end
10
+
11
+ def call(env)
12
+ @request = Rack::Request.new(env)
13
+ @render_pdf = false
14
+
15
+ set_request_to_render_as_pdf(env) if render_as_pdf?
16
+ status, headers, response = @app.call(env)
17
+
18
+ if rendering_pdf? && headers['Content-Type'] =~ %r{text/html|application/xhtml\+xml}
19
+ body = response.respond_to?(:body) ? response.body : response.join
20
+ body = body.join if body.is_a?(Array)
21
+
22
+ body = Grover.new(body).to_pdf
23
+ response = [body]
24
+
25
+ # Do not cache PDFs
26
+ headers.delete 'ETag'
27
+ headers.delete 'Cache-Control'
28
+
29
+ headers['Content-Length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
30
+ headers['Content-Type'] = 'application/pdf'
31
+ end
32
+
33
+ [status, headers, response]
34
+ end
35
+
36
+ private
37
+
38
+ def rendering_pdf?
39
+ @render_pdf
40
+ end
41
+
42
+ def render_as_pdf?
43
+ @request.path.end_with?('.pdf')
44
+ end
45
+
46
+ def set_request_to_render_as_pdf(env)
47
+ @render_pdf = true
48
+ env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
49
+ end
50
+
51
+ def concat(accepts, type)
52
+ (accepts || '').split(',').unshift(type).compact.join(',')
53
+ end
54
+ end
55
+ end
@@ -18,7 +18,11 @@ class Grover
18
18
  try {
19
19
  browser = await puppeteer.launch(#{launch_params});
20
20
  const page = await browser.newPage();
21
- await page.goto(url, { waitUntil: 'networkidle2' });
21
+ if (url.match(/^http/i)) {
22
+ await page.goto(url, { waitUntil: 'networkidle2' });
23
+ } else {
24
+ await page.setContent(url);
25
+ }
22
26
  return await page.pdf(options);
23
27
  } finally {
24
28
  if (browser) {
@@ -1,3 +1,3 @@
1
1
  class Grover
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.2.0'.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.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich
@@ -97,14 +97,12 @@ dependencies:
97
97
  description: A Ruby gem to transform HTML into PDFs using Google Puppeteer/Chromium
98
98
  email:
99
99
  - abromwich@studiosity.com
100
- executables:
101
- - console
100
+ executables: []
102
101
  extensions: []
103
102
  extra_rdoc_files: []
104
103
  files:
105
- - bin/console
106
104
  - lib/grover.rb
107
- - lib/grover/grover.rb
105
+ - lib/grover/middleware.rb
108
106
  - lib/grover/processor.rb
109
107
  - lib/grover/utils.rb
110
108
  - lib/grover/version.rb
@@ -132,5 +130,5 @@ rubygems_version: 2.6.14.1
132
130
  signing_key:
133
131
  specification_version: 4
134
132
  summary: A Ruby gem to transform HTML into PDFs wrapper the NodeJS Google Puppeteer
135
- project using Chromium
133
+ driver for Chromium
136
134
  test_files: []
data/bin/console DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'grover'
5
-
6
- require 'irb'
7
- IRB.start
data/lib/grover/grover.rb DELETED
@@ -1,43 +0,0 @@
1
- #
2
- # Grover interface for converting HTML to PDF
3
- #
4
- class Grover
5
- #
6
- # @param [String] url URL of the page to convert
7
- # @param [Hash] options Optional parameters to pass to PDF processor
8
- # see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
9
- #
10
- def initialize(url, options = {})
11
- @url = url
12
- @root_path = options.delete :root_path
13
- @options = options
14
- end
15
-
16
- #
17
- # Request URL with provided options and create PDF
18
- #
19
- # @param [String] path Optional path to write the PDF to
20
- # @return [Array<Integer>] Byte array of the resulting PDF
21
- #
22
- def to_pdf(path = nil)
23
- options = @options.dup
24
- options[:path] = path if path
25
- result = Grover::Processor.new(root_path).convert_pdf(@url, options)
26
- result['data'].pack('c*')
27
- end
28
-
29
- def inspect
30
- format(
31
- '#<%<class_name>s:0x%<object_id>p @url="%<url>s">',
32
- class_name: self.class.name,
33
- object_id: object_id,
34
- url: url
35
- )
36
- end
37
-
38
- private
39
-
40
- def root_path
41
- @root_path ||= File.expand_path(__dir__)
42
- end
43
- end