<commit_before>require 'phantomjs'
require 'json'

class Snapshotter
  SCRIPT_PATH = Rails.root.join('script', 'take-snapshot.js').to_s

  def initialize(url, viewport)
    @url      = url
    @viewport = viewport
  end

  # @return [Hash]
  def take_snapshot!
    result = {}

    FileUtil.with_tempfile do |snapshot_file|
      opts = {
        address: @url.address,
        outfile: snapshot_file,
        viewportSize: {
          width:  @viewport.width,
          height: @viewport.height
        }
      }

      Rails.logger.info "Taking snapshot of #{@url} @ #{@viewport}"
      Phantomjs.run(SCRIPT_PATH, opts.to_json) do |line|
        begin
          result = JSON.parse line, symbolize_names: true
        rescue JSON::ParserError
          # We only expect a single line of JSON to be output by our snapshot
          # script. If something else is happening, we want to know about it.
          raise line
        end
      end

      Rails.logger.info "Uploading snapshot of #{@url} @ #{@viewport}"
      result[:external_image_id] = FileUtil.upload_to_cloudinary(snapshot_file)
    end

    result
  end
end
<commit_msg>Ignore SSL errors when taking snapshot<commit_after>require 'phantomjs'
require 'json'

class Snapshotter
  SCRIPT_PATH = Rails.root.join('script', 'take-snapshot.js').to_s

  def initialize(url, viewport)
    @url      = url
    @viewport = viewport
  end

  # @return [Hash]
  def take_snapshot!
    result = {}

    FileUtil.with_tempfile do |snapshot_file|
      opts = {
        address: @url.address,
        outfile: snapshot_file,
        viewportSize: {
          width:  @viewport.width,
          height: @viewport.height
        }
      }

      Rails.logger.info "Taking snapshot of #{@url} @ #{@viewport}"
      Phantomjs.run('--ignore-ssl-errors=true', SCRIPT_PATH, opts.to_json) do |line|
        begin
          result = JSON.parse line, symbolize_names: true
        rescue JSON::ParserError
          # We only expect a single line of JSON to be output by our snapshot
          # script. If something else is happening, we want to know about it.
          raise line
        end
      end

      Rails.logger.info "Uploading snapshot of #{@url} @ #{@viewport}"
      result[:external_image_id] = FileUtil.upload_to_cloudinary(snapshot_file)
    end

    result
  end
end

{'fraction_non_alphanumeric': 0.09617861743237441, 'fraction_numerical': 0.0, 'mean_word_length': 2.39155749636099, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3682468', 'n_tokens_mistral': 728, 'n_tokens_neox': 710, 'n_words': 196}