Skip to content

Commit

Permalink
Add support to return the html content (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
paresharma committed Feb 21, 2021
1 parent 4760d8d commit 3f89105
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## Unreleased
- [#106](https://github.com/Studiosity/grover/pull/106) Add support for addStyleTag and addScriptTag ([@paresharma][])
- [#107](https://github.com/Studiosity/grover/pull/107) Add support to return the html content ([@paresharma][])

## [0.14.1](releases/tag/v0.14.1) - 2021-01-16
### Changed
Expand Down
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -34,7 +34,10 @@ pdf = grover.to_pdf

# Get a screenshot
png = grover.to_png
jpeg = grover.to_jpeg
jpeg = grover.to_jpeg

# Get the HTML content (including DOCTYPE)
html = grover.to_html

# Options can be provided through meta tags
Grover.new('<html><head><meta name="grover-page_ranges" content="1-3"')
Expand Down
9 changes: 9 additions & 0 deletions lib/grover.rb
Expand Up @@ -50,6 +50,15 @@ def to_pdf(path = nil)
processor.convert :pdf, @url, normalized_options(path: path)
end

#
# Request URL with provided options and render HTML
#
# @return [String] The resulting HTML string
#
def to_html
processor.convert :content, @url, normalized_options(path: nil)
end

#
# Request URL with provided options and create screenshot
#
Expand Down
1 change: 1 addition & 0 deletions lib/grover/processor.rb
Expand Up @@ -19,6 +19,7 @@ def convert(method, url_or_html, options)
ensure_packages_are_initiated
result = call_js_method method, url_or_html, options
return unless result
return result if result.is_a?(String)

result['data'].pack('C*')
ensure
Expand Down
35 changes: 35 additions & 0 deletions spec/grover/processor_spec.rb
Expand Up @@ -581,5 +581,40 @@ def mean_colour_statistics(image)
colours.map { |colour| image.data.dig('channelStatistics', colour, 'mean').to_s }
end
end

context 'when rendering HTML' do
let(:method) { :content }

let(:url_or_html) do
<<-HTML
<html>
<head></head>
<body>
<h1>Hey there</h1>
<h2>Konnichiwa</h2>
</body>
</html>
HTML
end

let(:options) do
{
'scriptTagOptions' => [{ 'content' => 'document.querySelector("h2").remove()' }]
}
end

it 'returns the rendered HTML' do
expect(Grover::Utils.squish(convert)).to eq(
Grover::Utils.squish(
<<-HTML
<html><head><script type="">document.querySelector("h2").remove()</script></head>
<body>
<h1>Hey there</h1>
</body></html>
HTML
)
)
end
end
end
end
15 changes: 15 additions & 0 deletions spec/grover_spec.rb
Expand Up @@ -545,6 +545,21 @@
end
end

describe '#to_html' do
subject(:to_html) { grover.to_html }

let(:processor) { instance_double 'Grover::Processor' }
let(:expected_html) { '<html><body>Some HTML</body></html>' }

before { allow(Grover::Processor).to receive(:new).with(Dir.pwd).and_return processor }

it 'calls to Grover::Processor' do
allow(processor).to receive(:convert).with(:content, url_or_html, {}).and_return expected_html
expect(processor).to receive(:convert).with(:content, url_or_html, {})
expect(to_html).to eq expected_html
end
end

describe '#front_cover_path' do
subject(:front_cover_path) { grover.front_cover_path }

Expand Down

0 comments on commit 3f89105

Please sign in to comment.