lib/percy/capybara/loaders/native_loader.rb in percy-capybara-2.4.0 vs lib/percy/capybara/loaders/native_loader.rb in percy-capybara-2.4.1

- old
+ new

@@ -3,11 +3,10 @@ require 'uri' module Percy module Capybara module Loaders - # Resource loader that uses the native Capybara browser interface to discover resources. # This loader uses JavaScript to discover page resources, so specs must be tagged with # "js: true" because the default Rack::Test driver does not support executing JavaScript. class NativeLoader < BaseLoader PATH_REGEX = /\A\/[^\\s\"']*/ @@ -71,16 +70,17 @@ return css_urls; JS resource_urls = _evaluate_script(page, script) resource_urls.each do |url| - next if !_should_include_url?(url) + next unless _should_include_url?(url) response = _fetch_resource_url(url) _absolute_url_to_relative!(url, _current_host_port) - next if !response + next unless response resources << Percy::Client::Resource.new( - url, mimetype: 'text/css', content: response.body) + url, mimetype: 'text/css', content: response.body + ) end resources end private :_get_css_resources @@ -90,11 +90,11 @@ image_urls = Set.new # Find all image tags on the page. page.all('img').each do |image_element| srcs = [] - srcs << image_element[:src] if !image_element[:src].nil? + srcs << image_element[:src] unless image_element[:src].nil? srcset_raw_urls = image_element[:srcset] || '' temp_urls = srcset_raw_urls.split(',') temp_urls.each do |temp_url| srcs << temp_url.split(' ').first @@ -150,33 +150,35 @@ resource_url = URI.join(page.current_url, image_url).to_s # Skip duplicates. next if resources.find { |r| r.resource_url == resource_url } - next if !_should_include_url?(resource_url) + next unless _should_include_url?(resource_url) # Fetch the images. # TODO(fotinakis): this can be pretty inefficient for image-heavy pages because the # browser has already loaded them once and this fetch cannot easily leverage the # browser's cache. However, often these images are probably local resources served by a # development server, so it may not be so bad. Re-evaluate if this becomes an issue. response = _fetch_resource_url(resource_url) _absolute_url_to_relative!(resource_url, _current_host_port) - next if !response + next unless response resources << Percy::Client::Resource.new( - resource_url, mimetype: response.content_type, content: response.body) + resource_url, mimetype: response.content_type, content: response.body + ) end resources end private :_get_image_resources # @private def _fetch_resource_url(url) response = Percy::Capybara::HttpFetcher.fetch(url) - if !response - STDERR.puts "[percy] Warning: failed to fetch page resource, this might be a bug: #{url}" + unless response + STDERR.puts '[percy] Warning: failed to fetch page resource, ' \ + "this might be a bug: #{url}" return nil end response end private :_fetch_resource_url @@ -214,18 +216,17 @@ url_match[1] + url_match[2] + (url_match[3] || '') end # @private def _same_server?(url, host_port) - url.start_with?(host_port + "/") || url == host_port + url.start_with?(host_port + '/') || url == host_port end # @private def _absolute_url_to_relative!(url, host_port) - url.gsub!(host_port + '/','/') if url.start_with?(host_port + "/") + url.gsub!(host_port + '/', '/') if url.start_with?(host_port + '/') end private :_absolute_url_to_relative! - end end end end