Skip to content

Commit

Permalink
Bumped to v0.1.0
Browse files Browse the repository at this point in the history
Initial implementation of PDF processor
Added console to expedite development
  • Loading branch information
abrom committed Aug 22, 2018
1 parent 5c23475 commit 7571166
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*.gem
Gemfile.lock
package-lock.json
/node_modules
/coverage
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Layout/DotPosition:
EnforcedStyle: trailing

Metrics/LineLength:
Max: 120
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ before_install:
install:
- bundle install --jobs=3 --retry=3
- gem install rubocop
- npm install

before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
Expand Down
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
[![Travis Build Status](https://img.shields.io/travis/Studiosity/grover.svg?style=flat)](https://travis-ci.org/Studiosity/grover)
[![Maintainability](https://api.codeclimate.com/v1/badges/37609653789bcf2c8d94/maintainability)](https://codeclimate.com/github/Studiosity/grover/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/37609653789bcf2c8d94/test_coverage)](https://codeclimate.com/github/Studiosity/grover/test_coverage)
[![Gem Version](https://img.shields.io/gem/v/grover.svg?style=flat)](#)

# Grover

A Ruby gem to transform HTML into PDFs using Google Puppeteer
A Ruby gem to transform HTML into PDFs using [Google Puppeteer](https://github.com/GoogleChrome/puppeteer)
and [Chromium](https://www.chromium.org/Home).


## Installation

Add this line to your application's Gemfile:

```ruby
gem 'grover'
```

### Google Puppeteer
```bash
npm install puppeteer
```


## Usage
```ruby
# Grover.new accepts a URL and optional parameters for `puppeteer`
grover = Grover.new('https://google.com', page_size: 'A4')

# Get an inline PDF
pdf = grover.to_pdf

```


## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Studiosity/grover.

Note that spec tests are appreciated to minimise regressions. Before submitting a PR, please ensure that:

```bash
$ rspec
```
and

```bash
$ rubocop
```
both succeed


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
7 changes: 7 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'grover'

require 'irb'
IRB.start
6 changes: 4 additions & 2 deletions grover.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ Gem::Specification.new do |spec|
spec.homepage = 'http://github.com/Studiosity/grover'
spec.license = 'MIT'

spec.files = `git ls-files`.split("\n")
spec.files = `git ls-files lib bin`.split("\n")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.test_files = spec.files.grep(%r{^spec/})
spec.require_paths = ['lib']

spec.add_dependency 'schmooze', '~> 0.2'

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake', '~> 12.3'
spec.add_development_dependency 'rspec', '~> 3.7'
Expand Down
5 changes: 5 additions & 0 deletions lib/grover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'grover/version'

require 'grover/utils'
require 'grover/processor'
require 'grover/grover'
42 changes: 42 additions & 0 deletions lib/grover/grover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Grover interface for converting HTML to PDF
#
class Grover
#
# @param [String] url URL of the page to convert
# @param [Hash] options Optional parameters to pass to PDF processor
# see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
#
def initialize(url, options = {})
@url = url
@options = options
end

#
# Request URL with provided options and create PDF
#
# @param [String] path Optional path to write the PDF to
# @return [Array<Integer>] Byte array of the resulting PDF
#
def to_pdf(path = nil)
options = @options.dup
options[:path] = path if path
result = Grover::Processor.new(root_path).convert_pdf(@url, options)
result['data'].pack('c*')
end

def inspect
format(
'#<%<class_name>s:0x%<object_id>p @url="%<url>s">',
class_name: self.class.name,
object_id: object_id,
url: url
)
end

private

def root_path
File.expand_path(__dir__)
end
end
26 changes: 26 additions & 0 deletions lib/grover/processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'schmooze'

class Grover
#
# Processor helper class for calling out to Puppeteer NodeJS library
#
class Processor < Schmooze::Base
dependencies puppeteer: 'puppeteer'

method :convert_pdf, Utils.squish(<<-FUNCTION)
async (url, options) => {
let browser;
try {
browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
return await page.pdf(options);
} finally {
if (browser) {
await browser.close();
}
}
}
FUNCTION
end
end
13 changes: 13 additions & 0 deletions lib/grover/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Grover
#
# Utility class for Grover helper methods
#
class Utils
def self.squish(string)
string.
gsub(/\A[[:space:]]+/, '').
gsub(/[[:space:]]+\z/, '').
gsub(/[[:space:]]+/, ' ')
end
end
end
2 changes: 1 addition & 1 deletion lib/grover/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Grover
VERSION = '0.0.1'.freeze
VERSION = '0.1.0'.freeze
end
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "grover",
"version": "0.1.0",
"description": "A Ruby gem to transform HTML into PDFs using Google Puppeteer/Chromium",
"repository": {
"type": "git",
"url": "git+https://github.com/Studiosity/grover.git"
},
"keywords": [
"PDF",
"puppeteer",
"chromium"
],
"author": "[email protected]",
"license": "MIT",
"bugs": {
"url": "https://github.com/Studiosity/grover/issues"
},
"homepage": "https://github.com/Studiosity/grover#readme",
"devDependencies": {
"puppeteer": "^1.7.0"
},
"dependencies": {
"logging": "^3.2.0"
}
}
24 changes: 24 additions & 0 deletions spec/grover/grover_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe Grover do
describe '.new' do
subject(:new) { Grover.new('http://google.com') }

it { is_expected.to be_a Grover }
it { expect(subject.instance_variable_get('@url')).to eq 'http://google.com' }
it { expect(subject.instance_variable_get('@options')).to eq({}) }

context 'with options passed' do
subject(:new) { Grover.new('http://happyfuntimes.com', page_size: 'A4') }

it { expect(subject.instance_variable_get('@url')).to eq 'http://happyfuntimes.com' }
it { expect(subject.instance_variable_get('@options')).to eq(page_size: 'A4') }
end
end

describe '#to_pdf' do
subject(:to_pdf) { Grover.new('https://www.google.com').to_pdf }

it { is_expected.to start_with "%PDF-1.4\n" }
end
end
37 changes: 37 additions & 0 deletions spec/grover/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

describe Grover::Utils do
describe '.squish' do
subject(:squish) { described_class.squish string }

context 'with an empty string' do
let(:string) { '' }

it { is_expected.to eq '' }
end

context 'with leading spaces' do
let(:string) { ' Foo' }

it { is_expected.to eq 'Foo' }
end

context 'with trailing spaces' do
let(:string) { 'Bar ' }

it { is_expected.to eq 'Bar' }
end

context 'with spaces in the middle' do
let(:string) { 'Foo Bar Baz' }

it { is_expected.to eq 'Foo Bar Baz' }
end

context 'with newlines' do
let(:string) { "\nFoo\nBar Baz\nBoop\n" }

it { is_expected.to eq 'Foo Bar Baz Boop' }
end
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'simplecov'
SimpleCov.start

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'grover'

RSpec.configure do |config|
config.order = 'random'
end

0 comments on commit 7571166

Please sign in to comment.