Class: Percy::Capybara::Bedrock::Loader
- Inherits:
-
Loaders::BaseLoader
- Object
- Loaders::BaseLoader
- Percy::Capybara::Bedrock::Loader
- Defined in:
- lib/percy/capybara/bedrock/loader.rb
Constant Summary collapse
- RESOURCE_PATHS =
[ '/app/mu-plugins', '/app/plugins', '/app/themes', '/app/uploads', '/wp', ].freeze
- SKIP_RESOURCE_EXTENSIONS =
[ '.gz', '.htm', '.html', '.json', '.lock', '.log', '.map', '.md', '.php', '.phtml', '.rar', '.rb', '.sql', '.txt', '.xml', '.zip', '.gemspec', ].freeze
- SKIP_RESOURCE_BASENAMES =
[ '.DS_Store', '.editorconfig', '.eslintrc.js', '.gitignore', 'Gemfile', 'LICENSE', 'Rakefile', ].freeze
- SKIP_RESOURCE_PATHS =
[ '/.cache-loader/', '/.cache/', '/.caches/', '/.circleci/', '/.git/', '/.github/', '/build/', '/cache/', '/caches/', '/doc/', '/docs/', '/log/', '/logs/', '/node_modules/', '/tmp/', '/vendor/', '/wp/wp-content/themes/', 'resources/assets/', ].freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#web_root ⇒ Object
readonly
Returns the value of attribute web_root.
Instance Method Summary collapse
- #_resources_from_dir(root_dir, base_url: '/') ⇒ Object
- #build_resources ⇒ Object
-
#initialize(options = {}) ⇒ Loader
constructor
A new instance of Loader.
- #skip?(path:) ⇒ Boolean
- #skip_basename?(path:) ⇒ Boolean
- #skip_extension?(path:) ⇒ Boolean
- #skip_file_size?(path:) ⇒ Boolean
- #skip_path?(path:) ⇒ Boolean
- #snapshot_resources ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Loader
Returns a new instance of Loader.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 70 def initialize( = {}) # @web_root should point to <bedrock>/web @web_root = [:web_root] @base_url = [:base_url] || '/' raise ArgumentError, '@web_root is required' if @web_root.nil? || @web_root == '' unless Pathname.new(@web_root).absolute? raise ArgumentError, "@web_root needs to be an absolute path. Received: #{@web_root}" end unless Dir.exist?(@web_root) raise ArgumentError, "@web_root provided was not found. Received: #{@web_root}" end super end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
68 69 70 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 68 def base_url @base_url end |
#web_root ⇒ Object (readonly)
Returns the value of attribute web_root.
68 69 70 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 68 def web_root @web_root end |
Instance Method Details
#_resources_from_dir(root_dir, base_url: '/') ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 95 def _resources_from_dir(root_dir, base_url: '/') resources = [] resource_dirs = RESOURCE_PATHS.map do |resource_path| root_dir + resource_path end _find_files(resource_dirs).each do |path| next if skip?(path: path) # Replace the @web_root with the base_url to generate the resource_url resource_url = _uri_join(base_url, path.sub(root_dir.to_s, '')) sha = Digest::SHA256.hexdigest(File.read(path)) resources << Percy::Client::Resource.new(resource_url, sha: sha, path: path) end resources end |
#build_resources ⇒ Object
90 91 92 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 90 def build_resources _resources_from_dir(@web_root, base_url: @base_url) end |
#skip?(path:) ⇒ Boolean
114 115 116 117 118 119 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 114 def skip?(path:) skip_extension?(path: path) || skip_basename?(path: path) || skip_path?(path: path) || skip_file_size?(path: path) end |
#skip_basename?(path:) ⇒ Boolean
125 126 127 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 125 def skip_basename?(path:) SKIP_RESOURCE_BASENAMES.include?(File.basename(path)) end |
#skip_extension?(path:) ⇒ Boolean
121 122 123 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 121 def skip_extension?(path:) SKIP_RESOURCE_EXTENSIONS.include?(File.extname(path)) end |
#skip_file_size?(path:) ⇒ Boolean
133 134 135 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 133 def skip_file_size?(path:) File.size(path) > MAX_FILESIZE_BYTES end |
#skip_path?(path:) ⇒ Boolean
129 130 131 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 129 def skip_path?(path:) SKIP_RESOURCE_PATHS.any? { |skip| path.include?(skip) } end |
#snapshot_resources ⇒ Object
86 87 88 |
# File 'lib/percy/capybara/bedrock/loader.rb', line 86 def snapshot_resources [root_html_resource] end |