Class: Grover

Inherits:
Object show all
Defined in:
lib/grover.rb,
lib/grover/utils.rb,
lib/grover/errors.rb,
lib/grover/version.rb,
lib/grover/processor.rb,
lib/grover/middleware.rb,
lib/grover/configuration.rb,
lib/grover/options_fixer.rb,
lib/grover/options_builder.rb,
lib/grover/html_preprocessor.rb

Overview

Grover interface for converting HTML to PDF

Defined Under Namespace

Modules: HTMLPreprocessor, JavaScript Classes: Configuration, Middleware, OptionsBuilder, OptionsFixer, Processor, Utils

Constant Summary collapse

DEFAULT_HEADER_TEMPLATE =
"<div class='date text left'></div><div class='title text center'></div>"
<<~HTML
  <div class='url text left grow'></div>
  <div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
HTML
Error =

Error classes for calling out to Puppeteer NodeJS library

Heavily based on the Schmooze library github.com/Shopify/schmooze

Class.new(StandardError)
DependencyError =
Class.new(Error)
VERSION =
'0.12.3'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Grover

Returns a new instance of Grover.

Parameters:



35
36
37
38
39
40
41
# File 'lib/grover.rb', line 35

def initialize(url, options = {})
  @url = url
  @options = OptionsBuilder.new(options, url)
  @root_path = @options.delete 'root_path'
  @front_cover_path = @options.delete 'front_cover_path'
  @back_cover_path = @options.delete 'back_cover_path'
end

Instance Attribute Details

#back_cover_pathObject (readonly)

Returns the value of attribute back_cover_path.



28
29
30
# File 'lib/grover.rb', line 28

def back_cover_path
  @back_cover_path
end

#front_cover_pathObject (readonly)

Returns the value of attribute front_cover_path.



28
29
30
# File 'lib/grover.rb', line 28

def front_cover_path
  @front_cover_path
end

Class Method Details

.configurationObject

Configuration for the conversion



119
120
121
# File 'lib/grover.rb', line 119

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



123
124
125
# File 'lib/grover.rb', line 123

def self.configure
  yield(configuration)
end

Instance Method Details

#inspectObject

Instance inspection



107
108
109
110
111
112
113
114
# File 'lib/grover.rb', line 107

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

#screenshot(path: nil, format: nil) ⇒ String

Request URL with provided options and create screenshot

Parameters:

  • path (String) (defaults to: nil)

    Optional path to write the screenshot to

  • format (String) (defaults to: nil)

    Optional format of the screenshot

Returns:

  • (String)

    The resulting image data



60
61
62
63
64
# File 'lib/grover.rb', line 60

def screenshot(path: nil, format: nil)
  options = normalized_options(path: path)
  options['type'] = format if %w[png jpeg].include? format
  processor.convert :screenshot, @url, options
end

#show_back_cover?Boolean

Returns whether a back cover (request) path has been specified in the options

Returns:

  • (Boolean)

    Back cover path is configured



100
101
102
# File 'lib/grover.rb', line 100

def show_back_cover?
  back_cover_path.is_a?(::String) && back_cover_path.start_with?('/')
end

#show_front_cover?Boolean

Returns whether a front cover (request) path has been specified in the options

Returns:

  • (Boolean)

    Front cover path is configured



91
92
93
# File 'lib/grover.rb', line 91

def show_front_cover?
  front_cover_path.is_a?(::String) && front_cover_path.start_with?('/')
end

#to_jpeg(path = nil) ⇒ String

Request URL with provided options and create JPEG

Parameters:

  • path (String) (defaults to: nil)

    Optional path to write the screenshot to

Returns:

  • (String)

    The resulting JPEG data



82
83
84
# File 'lib/grover.rb', line 82

def to_jpeg(path = nil)
  screenshot path: path, format: 'jpeg'
end

#to_pdf(path = nil) ⇒ String

Request URL with provided options and create PDF

Parameters:

  • path (String) (defaults to: nil)

    Optional path to write the PDF to

Returns:

  • (String)

    The resulting PDF data



49
50
51
# File 'lib/grover.rb', line 49

def to_pdf(path = nil)
  processor.convert :pdf, @url, normalized_options(path: path)
end

#to_png(path = nil) ⇒ String

Request URL with provided options and create PNG

Parameters:

  • path (String) (defaults to: nil)

    Optional path to write the screenshot to

Returns:

  • (String)

    The resulting PNG data



72
73
74
# File 'lib/grover.rb', line 72

def to_png(path = nil)
  screenshot path: path, format: 'png'
end