Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to return the html content #107

Merged
merged 3 commits into from Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions spec/grover/processor_spec.rb
Expand Up @@ -581,5 +581,22 @@ 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>
</body>
</html>
HTML
end

it { expect(convert.gsub(/[[:space:]]+/, '')).to eq url_or_html.gsub(/[[:space:]]+/, '') }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things here.. I'd suggest using Grover::Utils.squish rather than removing all spaces. Would still give a consistent result whilst being true to the rendered output. Also, it feels like this would be a good opportunity to test that the rendered HTML is returned. ie something like:

    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>
              <script>document.querySelector('h1').remove();</script>
            </body>
          </html>
        HTML
      end

      it 'returns the rendered HTML' do
        expect(Grover::Utils.squish(convert)).to eq Grover::Utils.squish(<<~HTML)
          <html><head></head>
            <body>
              <h2>Konnichiwa</h2>
              <script>document.querySelector('h1').remove();</script>
            </body></html>
        HTML
      end
    end

Ie not just what you gave it, but rather the result of it being rendered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch and point, thanks. Updated 馃檶

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