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/dev_tools_parser.rb,
lib/grover/html_preprocessor.rb

Overview

Grover interface for converting HTML to PDF

Defined Under Namespace

Modules: HTMLPreprocessor, JavaScript Classes: Configuration, DependencyError, DevToolsParser, Error, Middleware, OptionsBuilder, OptionsFixer, Processor, UnsafeConfigurationError, Utils

Constant Summary collapse

DEFAULT_HEADER_TEMPLATE =
"<div class='date text left'></div><div class='title text center'></div>"
"<div class='url text left grow'></div>\n<div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>\n"
VERSION =
'1.2.7'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, **options) ⇒ Grover

Returns a new instance of Grover.



37
38
39
40
41
42
43
# File 'lib/grover.rb', line 37

def initialize(uri, **options)
  @uri = uri.to_s
  @options = OptionsBuilder.new(options, @uri)
  @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.



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

def back_cover_path
  @back_cover_path
end

#front_cover_pathObject (readonly)

Returns the value of attribute front_cover_path.



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

def front_cover_path
  @front_cover_path
end

Class Method Details

.configurationObject

Configuration for the conversion



142
143
144
# File 'lib/grover.rb', line 142

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

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

Yields:



146
147
148
# File 'lib/grover.rb', line 146

def self.configure
  yield(configuration)
end

Instance Method Details

#debug_outputArray<String>?

Returns the debug output from the conversion process.

Returns:

  • An array of DevTools log output, or nil if DEBUG env var is not enabled



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

def debug_output
  processor.debug_output
end

#inspectObject

Instance inspection



130
131
132
133
134
135
136
137
# File 'lib/grover.rb', line 130

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

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

Request URI with provided options and create screenshot

Parameters:

  • (defaults to: nil)

    Optional path to write the screenshot to

  • (defaults to: nil)

    Optional format of the screenshot

Returns:

  • The resulting image data



73
74
75
76
77
78
# File 'lib/grover.rb', line 73

def screenshot(path: nil, format: nil)
  @processor = nil # Flush out the processor from any previous use
  options = normalized_options(path: path)
  options['type'] = format if %w[png jpeg].include? format
  processor.convert :screenshot, @uri, options
end

#show_back_cover?Boolean

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

Returns:

  • Back cover path is configured



114
115
116
# File 'lib/grover.rb', line 114

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:

  • Front cover path is configured



105
106
107
# File 'lib/grover.rb', line 105

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

#to_htmlString

Request URI with provided options and render HTML

Returns:

  • The resulting HTML string



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

def to_html
  @processor = nil # Flush out the processor from any previous use
  processor.convert :content, @uri, normalized_options(path: nil)
end

#to_jpeg(path = nil) ⇒ String

Request URI with provided options and create JPEG

Parameters:

  • (defaults to: nil)

    Optional path to write the screenshot to

Returns:

  • The resulting JPEG data



96
97
98
# File 'lib/grover.rb', line 96

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

#to_pdf(path = nil) ⇒ String

Request URI with provided options and create PDF

Parameters:

  • (defaults to: nil)

    Optional path to write the PDF to

Returns:

  • The resulting PDF data



51
52
53
54
# File 'lib/grover.rb', line 51

def to_pdf(path = nil)
  @processor = nil # Flush out the processor from any previous use
  processor.convert :pdf, @uri, normalized_options(path: path)
end

#to_png(path = nil) ⇒ String

Request URI with provided options and create PNG

Parameters:

  • (defaults to: nil)

    Optional path to write the screenshot to

Returns:

  • The resulting PNG data



86
87
88
# File 'lib/grover.rb', line 86

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