diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 00000000..956b00aa --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,21 @@ +We're always happy to receive Pull Requests from the Wraith community. + +Guidelines: + +* Make sure your PR is documented (What does it do? Why is it needed?) +* New features and bug fixes should have tests written alongside them +* Appreciate that contributors maintain Wraith in their spare time, so a response may take several weeks + +A PR is more likely to be merged if it fixes one of [Wraith's open issues](https://github.com/BBC-News/wraith/issues). + +How to contribute: + +* Fork a branch based off BBC-News/wraith:master and do all of your changes within it. +* Make commits of logical units and describe them properly. +* Check for unnecessary whitespace with git diff --check before committing. +* If possible, submit tests to your patch / new feature so it can be tested easily. +* Assure nothing is broken by running all the tests (`bundle exec rspec`). +* Please ensure that it complies with coding standards. +* When writing the title of your Pull Request, if you have to pause to add an 'and' anywhere in the title - it should be two pull requests. + +**Please raise any issues with this project as a GitHub issue.** diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000..4945c1d8 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,21 @@ + + + +------ +Reporting a problem? Please describe the issue above, and complete the following checklist so that we can help you more quickly. + +#### Issue checklist: + +- [ ] I have validated my config file against [YAML Validator](http://codebeautify.org/yaml-validator) to make sure it is valid YAML. + +- [ ] I have run the command in verbose mode (by adding `verbose: true` to my config) and pasted the output below: + +``` +paste results here +``` + +- [ ] I have pasted the contents of my config file below: + +``` +paste config here +``` diff --git a/Dockerfile b/Dockerfile index 7c5204b5..bc5a4ee5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,11 +3,11 @@ FROM ruby:2.1.2 # some of ruby's build scripts are written in ruby # we purge this later to make sure our final image uses what we just built RUN apt-get update -RUN curl -o phantomjs.tar.gz -L https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.8-linux-x86_64.tar.bz2 -RUN tar -xvf phantomjs.tar.gz -RUN mv phantomjs-1.9.8-linux-x86_64/bin/phantomjs /usr/bin RUN echo "export phantomjs=/usr/bin/phantomjs" > .bashrc -RUN apt-get install -y libfreetype6 libfontconfig1 +RUN apt-get install -y libfreetype6 libfontconfig1 nodejs npm +RUN ln -s /usr/bin/nodejs /usr/bin/node +RUN npm install npm +RUN npm install -g phantomjs@2.1.7 casperjs@1.1.1 RUN gem install wraith --no-rdoc --no-ri RUN gem install aws-sdk --no-rdoc --no-ri @@ -17,4 +17,7 @@ RUN echo "deb http://security.debian.org/ jessie/updates contrib non-free" | tee RUN apt-get update RUN apt-get install -y ttf-freefont ttf-mscorefonts-installer ttf-bitstream-vera ttf-dejavu ttf-liberation +# Make sure a recent (>6.7.7-10) version of ImageMagick is installed. +RUN apt-get install -y imagemagick + ENTRYPOINT [ "wraith" ] diff --git a/README.md b/README.md index b5a3dd19..5a732356 100644 --- a/README.md +++ b/README.md @@ -56,17 +56,7 @@ Wraith also requires at least one of these headless browsers: ## Contributing -If you want to add functionality to this project, pull requests are welcome. - - * Fork a branch based off BBC-News/wraith:master and do all of your changes within it. - * Make commits of logical units and describe them properly. - * Check for unnecessary whitespace with git diff --check before committing. - * If possible, submit tests to your patch / new feature so it can be tested easily. - * Assure nothing is broken by running all the tests (`bundle exec rspec`). - * Please ensure that it complies with coding standards. - * When writing the title of your Pull Request, if you have to pause to add an 'and' anywhere in the title - it should be two pull requests. - -**Please raise any issues with this project as a GitHub issue.** +Please read [how to contribute to Wraith](https://github.com/BBC-News/wraith/blob/master/.github/CONTRIBUTING.md). ## License diff --git a/lib/wraith/cli.rb b/lib/wraith/cli.rb index 0133049b..3d9333ed 100644 --- a/lib/wraith/cli.rb +++ b/lib/wraith/cli.rb @@ -42,7 +42,7 @@ def copy_old_shots(config_name) end end - desc "validate", "checks your configuration and validates that all required properties exist" + desc "validate [config_name]", "checks your configuration and validates that all required properties exist" def validate(config_name) within_acceptable_limits do logger.info Wraith::Validate.new(config_name).validate @@ -176,4 +176,10 @@ def latest(config) generate_gallery(config) end end + + desc "version", "Show the version of Wraith" + map ["--version", "-version", "-v"] => "version" + def version + logger.info Wraith::VERSION + end end diff --git a/lib/wraith/folder.rb b/lib/wraith/folder.rb index 41f335c3..8951c209 100644 --- a/lib/wraith/folder.rb +++ b/lib/wraith/folder.rb @@ -86,11 +86,8 @@ def threshold_rate(dirs) dirs.each do |_folder_name, shot_info| shot_info.each do |_k, v| begin - if !v.include?(:diff) - return false - elsif v[:data] > wraith.threshold - return false - end + return false unless v.include?(:diff) + return false if v[:data] > wraith.threshold rescue return true end diff --git a/lib/wraith/gallery.rb b/lib/wraith/gallery.rb index 4e4b893f..b764dc9a 100755 --- a/lib/wraith/gallery.rb +++ b/lib/wraith/gallery.rb @@ -111,13 +111,32 @@ def data_check(size_dict, dirname, filepath) def sorting_dirs(dirs) if %w(diffs_only diffs_first).include?(wraith.mode) - @sorted = dirs.sort_by { |_category, sizes| -1 * sizes.max_by { |_size, dict| dict[:data] }[1][:data] } + @sorted = sort_by_diffs dirs else - @sorted = dirs.sort_by { |category, _sizes| category } + @sorted = sort_alphabetically dirs end Hash[@sorted] end + def sort_by_diffs(dirs) + dirs.sort_by do |_category, sizes| + size = select_size_with_biggest_diff sizes + -1 * size[1][:data] + end + end + + def select_size_with_biggest_diff(sizes) + begin + sizes.max_by { |_size, dict| dict[:data] } + rescue + fail MissingImageError + end + end + + def sort_alphabetically(dirs) + dirs.sort_by { |category, _sizes| category } + end + def generate_gallery(with_path = "") dest = "#{@location}/gallery.html" directories = parse_directories(@location) diff --git a/lib/wraith/gallery_template/slideshow_template.erb b/lib/wraith/gallery_template/slideshow_template.erb index 824a9b54..38beb28f 100644 --- a/lib/wraith/gallery_template/slideshow_template.erb +++ b/lib/wraith/gallery_template/slideshow_template.erb @@ -207,7 +207,8 @@ // http://jquery.malsup.com/cycle/options.html $('.slideshow').cycle({ - fx: 'cover', + fx: 'scrollHorz', + speed: 300, prev: '.prev', next: '.next', speed: 300, @@ -244,6 +245,20 @@ var slide = $(this).closest(".slide") fullScreenSlide(slide); }) + + window.addEventListener('keydown', function(e){ + var leftArrowKeyCode = 37; + var rightArrowKeyCode = 39; + + e = e || window.event; + + if (e.keyCode === leftArrowKeyCode) { + $('.slideshow').cycle('prev'); + } + else if (e.keyCode === rightArrowKeyCode) { + $('.slideshow').cycle('next'); + } + }); }) diff --git a/lib/wraith/helpers/custom_exceptions.rb b/lib/wraith/helpers/custom_exceptions.rb index 9431dc10..52b6d5db 100644 --- a/lib/wraith/helpers/custom_exceptions.rb +++ b/lib/wraith/helpers/custom_exceptions.rb @@ -6,3 +6,14 @@ class InvalidDomainsError < CustomError class MissingRequiredPropertyError < CustomError end + +class ConfigFileDoesNotExistError < CustomError +end + +class MissingImageError < CustomError + def initialize(msg = false) + default_msg = "Something went wrong! It looks like you're missing some images. Check your output directory and make sure that each path has four files for every screen size (data.txt, diff, base, latest). If in doubt, delete your output directory and run Wraith again." + msg = default_msg unless msg + super(msg) + end +end diff --git a/lib/wraith/spider.rb b/lib/wraith/spider.rb index 3ed65f10..76317c3f 100644 --- a/lib/wraith/spider.rb +++ b/lib/wraith/spider.rb @@ -27,6 +27,8 @@ def check_for_paths end class Wraith::Spider + attr_reader :wraith + def initialize(wraith) @wraith = wraith @paths = {} diff --git a/lib/wraith/validate.rb b/lib/wraith/validate.rb index bee74906..e448deda 100644 --- a/lib/wraith/validate.rb +++ b/lib/wraith/validate.rb @@ -19,12 +19,12 @@ def validate(mode = false) end def validate_basic_properties - if wraith.engine.nil? - fail MissingRequiredPropertyError, "You must specify a browser engine! #{docs_prompt}" - end - unless wraith.domains - fail MissingRequiredPropertyError, "You must specify at least one domain for Wraith to do anything! #{docs_prompt}" - end + fail MissingRequiredPropertyError, "You must specify a browser engine! #{docs_prompt}" if wraith.engine.nil? + + fail MissingRequiredPropertyError, "You must specify at least one domain for Wraith to do anything! #{docs_prompt}" unless wraith.domains + + fail MissingRequiredPropertyError, "You must specify a directory for capture! #{docs_prompt}" if wraith.directory.nil? + # @TODO validate fuzz is not nil, etc end @@ -68,12 +68,14 @@ def docs_prompt def list_debug_information wraith_version = Wraith::VERSION + command_run = ARGV.join ' ' ruby_version = run_command_safely("ruby -v") || "Ruby not installed" phantomjs_version = run_command_safely("phantomjs --version") || "PhantomJS not installed" casperjs_version = run_command_safely("casperjs --version") || "CasperJS not installed" imagemagick_version = run_command_safely("convert -version") || "ImageMagick not installed" logger.debug "#################################################" + logger.debug " Command run: #{command_run}" logger.debug " Wraith version: #{wraith_version}" logger.debug " Ruby version: #{ruby_version}" logger.debug " ImageMagick: #{imagemagick_version}" diff --git a/lib/wraith/version.rb b/lib/wraith/version.rb index 3e585e42..4e606a07 100644 --- a/lib/wraith/version.rb +++ b/lib/wraith/version.rb @@ -1,3 +1,3 @@ module Wraith - VERSION = "3.1.0" + VERSION = "3.2.1" end diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index 746cf98f..a689dbf3 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -22,12 +22,11 @@ def open_config_file(config_name) possible_filenames.each do |filepath| if File.exist?(filepath) - config = File.open config_name + config = File.open filepath return YAML.load config end end - rescue - logger.error "unable to find config \"#{config}\"" + fail ConfigFileDoesNotExistError, "unable to find config \"#{config_name}\"" end def directory diff --git a/spec/before_capture_spec.rb b/spec/before_capture_spec.rb index 19cc9ea8..58240fc9 100644 --- a/spec/before_capture_spec.rb +++ b/spec/before_capture_spec.rb @@ -45,7 +45,7 @@ def run_js_then_capture(config) # @TODO - we need tests determining the path to "path-level before_capture hooks" - describe "When hooking into beforeCapture (CasperJS)" do + describe "When hooking into before_capture (CasperJS)" do it "Executes the global JS before capturing" do run_js_then_capture( :global_js => before_suite_js, @@ -75,7 +75,7 @@ def run_js_then_capture(config) end #  @TODO - uncomment and figure out why broken - # describe "When hooking into beforeCapture (PhantomJS)" do + # describe "When hooking into before_capture (PhantomJS)" do # let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } # let(:saving) { Wraith::SaveImages.new(config_name) } # let(:wraith) { Wraith::Wraith.new(config_name) } diff --git a/spec/validate_spec.rb b/spec/validate_spec.rb index 9aad5a42..150cf6fd 100644 --- a/spec/validate_spec.rb +++ b/spec/validate_spec.rb @@ -7,6 +7,8 @@ test: http://www.bbc.com browser: "casperjs" + + directory: some/dir ') end @@ -24,6 +26,10 @@ config["browser"] = nil expect { Wraith::Validate.new(config, true).validate }.to raise_error MissingRequiredPropertyError end + + it "should complain if the config file doesn't exist" do + expect { Wraith::Wraith.new('configs/some_made_up_config.yml') }.to raise_error ConfigFileDoesNotExistError + end end describe "validation specific to capture mode" do @@ -47,6 +53,15 @@ ') Wraith::Validate.new(config, true).validate("capture") end + + it "should fail if no directory is specified" do + config["domains"] = YAML.load(' + test: http://something.bbc.com + live: http://www.bbc.com + ') + config["directory"] = nil + expect { Wraith::Validate.new(config, true).validate("capture") }.to raise_error MissingRequiredPropertyError + end end describe "validations specific to history mode" do diff --git a/templates/configs/history.yaml b/templates/configs/history.yaml index a49a2afe..aaa81eaf 100644 --- a/templates/configs/history.yaml +++ b/templates/configs/history.yaml @@ -29,7 +29,7 @@ paths: clickable_guide__after_click: path: /news/entertainment-arts-27221191 selector: '.idt__news' - before_capture: 'javascript/beforeCapture--casper_example.js' # (optional) JavaScript file to execute before taking the screenshot of this path. + before_capture: 'javascript/interact--casper.js' # (optional) JavaScript file to execute before taking the screenshot of this path. # (optional) JavaScript file to execute before taking screenshot of every path. Default: nil before_capture: 'javascript/wait--casper.js' diff --git a/wraith.gemspec b/wraith.gemspec index e5231c90..072c302c 100644 --- a/wraith.gemspec +++ b/wraith.gemspec @@ -20,14 +20,14 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'pry' spec.add_development_dependency 'rspec' + spec.add_development_dependency 'casperjs' spec.add_runtime_dependency 'rake' spec.add_runtime_dependency 'image_size' spec.add_runtime_dependency 'anemone' spec.add_runtime_dependency 'robotex' - spec.add_runtime_dependency 'nokogiri', '1.6.7' + spec.add_runtime_dependency 'nokogiri', '~> 1.6.7' spec.add_runtime_dependency 'log4r' spec.add_runtime_dependency 'thor' spec.add_runtime_dependency 'parallel' - spec.add_runtime_dependency 'casperjs' end