Class: Diffux::Snapshotter

Inherits:
Object
  • Object
show all
Defined in:
lib/diffux_core/snapshotter.rb

Overview

Snapshotter is responsible for delegating to PhantomJS to take the snapshot for a given URL and viewoprt, and then saving that snapshot to a file and storing any metadata on the Snapshot object.

Constant Summary collapse

SCRIPT_PATH =
File.join(File.dirname(__FILE__),
'script/take-snapshot.js').to_s

Instance Method Summary collapse

Constructor Details

#initialize(url: raise, viewport_width: raise, outfile: raise, user_agent: nil, crop_selector: nil) ⇒ Snapshotter

Returns a new instance of Snapshotter.

Parameters:

  • url (String) (defaults to: raise)

    the URL to snapshot

  • viewport_width (Integer) (defaults to: raise)

    the width of the screen used when snapshotting

  • outfile (File) (defaults to: raise)

    where to store the snapshot PNG.

  • user_agent (String) (defaults to: nil)

    an optional useragent string to used when requesting the page.

  • crop_selector (String) (defaults to: nil)

    an optional string containing a CSS selector. If this is present, and the page contains something matching it, the resulting snapshot image will only contain that element. If the page contains multiple elements mathing the selector, only the first element will be used.



25
26
27
28
29
30
31
32
33
# File 'lib/diffux_core/snapshotter.rb', line 25

def initialize(url:     raise, viewport_width: raise,
               outfile: raise, user_agent: nil,
               crop_selector: nil)
  @viewport_width = viewport_width
  @crop_selector  = crop_selector
  @user_agent     = user_agent
  @outfile        = outfile
  @url            = url
end

Instance Method Details

#take_snapshot!Hash

Takes a snapshot of the URL and saves it in the outfile as a PNG image.

Returns:

  • (Hash)

    a hash containing the following keys: title [String] the of the page being snapshotted log [String] a log of events happened during the snapshotting process</p></div> </li> </ul> </div><table class="source_code"> <tr> <td> <pre class="lines"> 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64</pre> </td> <td> <pre class="code"><span class="info file"># File 'lib/diffux_core/snapshotter.rb', line 40</span> <span class='kw'>def</span> <span class='id identifier rubyid_take_snapshot!'>take_snapshot!</span> <span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span> <span class='id identifier rubyid_opts'>opts</span> <span class='op'>=</span> <span class='lbrace'>{</span> <span class='label'>address:</span> <span class='ivar'>@url</span><span class='comma'>,</span> <span class='label'>outfile:</span> <span class='ivar'>@outfile</span><span class='comma'>,</span> <span class='label'>cropSelector:</span> <span class='ivar'>@crop_selector</span><span class='comma'>,</span> <span class='label'>viewportSize:</span> <span class='lbrace'>{</span> <span class='label'>width:</span> <span class='ivar'>@viewport_width</span><span class='comma'>,</span> <span class='label'>height:</span> <span class='ivar'>@viewport_width</span><span class='comma'>,</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='rbrace'>}</span> <span class='id identifier rubyid_opts'>opts</span><span class='lbracket'>[</span><span class='symbol'>:userAgent</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='ivar'>@user_agent</span> <span class='kw'>if</span> <span class='ivar'>@user_agent</span> <span class='id identifier rubyid_run_phantomjs'>run_phantomjs</span><span class='lparen'>(</span><span class='id identifier rubyid_opts'>opts</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_line'>line</span><span class='op'>|</span> <span class='kw'>begin</span> <span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='id identifier rubyid_line'>line</span><span class='comma'>,</span> <span class='label'>symbolize_names:</span> <span class='kw'>true</span> <span class='kw'>rescue</span> <span class='const'>JSON</span><span class='op'>::</span><span class='const'>ParserError</span> <span class='comment'># We only expect a single line of JSON to be output by our snapshot </span> <span class='comment'># script. If something else is happening, it is likely a JavaScript </span> <span class='comment'># error on the page and we should just forget about it and move on </span> <span class='comment'># with our lives. </span> <span class='kw'>end</span> <span class='kw'>end</span> <span class='id identifier rubyid_result'>result</span> <span class='kw'>end</span></pre> </td> </tr> </table> </div> </div> </div> <div id="footer"> Generated on Mon Apr 13 12:52:59 2026 by <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a> 0.9.40 (ruby-4.0.2). </div> </div> </body> </html> <script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="02a8741d22ea4485e6702aa0-|49" defer></script>