Class: Percy::Capybara::Loaders::FilesystemLoader

Inherits:
BaseLoader
  • Object
show all
Defined in:
lib/percy/capybara/loaders/filesystem_loader.rb

Overview

Resource loader that looks for resources in the specified folder.

Constant Summary collapse

SKIP_RESOURCE_EXTENSIONS =
[
  '.map', # Ignore source maps.
  '.gz', # Ignore gzipped files.
].freeze
MAX_FILESIZE_BYTES =

15 MB.

15 * 1024**2

Constants inherited from BaseLoader

BaseLoader::URL_REGEX

Instance Attribute Summary

Attributes inherited from BaseLoader

#page

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FilesystemLoader

Returns a new instance of FilesystemLoader.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/percy/capybara/loaders/filesystem_loader.rb', line 17

def initialize(options = {})
  # @assets_dir should point to a _compiled_ static assets directory, not source assets.
  @assets_dir = options[:assets_dir]
  @base_url = options[:base_url] || ''

  raise ArgumentError, 'assets_dir is required' if @assets_dir.nil? || @assets_dir == ''
  unless Pathname.new(@assets_dir).absolute?
    raise ArgumentError, "assets_dir needs to be an absolute path. Received: #{@assets_dir}"
  end
  unless Dir.exist?(@assets_dir)
    raise ArgumentError, "assets_dir provided was not found. Received: #{@assets_dir}"
  end

  super
end

Instance Method Details

#build_resourcesObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/percy/capybara/loaders/filesystem_loader.rb', line 37

def build_resources
  resources = []
  Find.find(@assets_dir).each do |path|
    # Skip directories.
    next unless FileTest.file?(path)
    # Skip certain extensions.
    next if SKIP_RESOURCE_EXTENSIONS.include?(File.extname(path))
    # Skip large files, these are hopefully downloads and not used in page rendering.
    next if File.size(path) > MAX_FILESIZE_BYTES

    # Replace the assets_dir with the base_url to generate the resource_url
    resource_url = path.sub(@assets_dir, @base_url)

    sha = Digest::SHA256.hexdigest(File.read(path))
    resources << Percy::Client::Resource.new(resource_url, sha: sha, path: path)
  end
  resources
end

#snapshot_resourcesObject



33
34
35
# File 'lib/percy/capybara/loaders/filesystem_loader.rb', line 33

def snapshot_resources
  [root_html_resource]
end