From 494208f7877a2660d44e6bf8cdab063e7bda3a8f Mon Sep 17 00:00:00 2001 From: Steve Day Date: Mon, 10 Sep 2018 10:13:46 +0100 Subject: [PATCH] Add base_path and compare_path options for paths This adds the option to supply two different paths for particular pages, for instances where the path changes between domains (e.g. testing a migration to a new slug library or a new CMS). Closes #552 --- lib/wraith/gallery.rb | 16 +++++++--- lib/wraith/helpers/capture_options.rb | 12 ++++++-- lib/wraith/save_images.rb | 4 +-- .../test_config--base_compare_paths.yaml | 9 ++++++ spec/gallery_spec.rb | 29 +++++++++++++++++++ 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 spec/configs/test_config--base_compare_paths.yaml diff --git a/lib/wraith/gallery.rb b/lib/wraith/gallery.rb index b764dc9a..ddc7dc93 100755 --- a/lib/wraith/gallery.rb +++ b/lib/wraith/gallery.rb @@ -3,6 +3,7 @@ require "fileutils" require "wraith/wraith" require "wraith/helpers/logger" +require "wraith/helpers/capture_options" class Wraith::GalleryGenerator include Logging @@ -60,15 +61,22 @@ def matcher(match, filename, dirname, category) end def figure_out_url(group, category) - root = wraith.domains["#{group}"] + group = "#{group}" + root = wraith.domains[group] + base_or_compare = wraith.domains.keys.index(group) == 0 ? :base : :compare return "" if root.nil? - path = get_path(category) + path = get_path(category, base_or_compare) url = root + path url end - def get_path(category) - wraith.paths[category]["path"] || wraith.paths[category] + def get_path(category, base_or_compare) + path_options = CaptureOptions.new(wraith.paths[category], wraith) + if base_or_compare == :base + path_options.base_path + else + path_options.compare_path + end end def get_group_from_match(match) diff --git a/lib/wraith/helpers/capture_options.rb b/lib/wraith/helpers/capture_options.rb index 9673b0d4..78d93f44 100644 --- a/lib/wraith/helpers/capture_options.rb +++ b/lib/wraith/helpers/capture_options.rb @@ -13,6 +13,14 @@ def path casper?(options) end + def base_path + options.is_a?(Hash) && options.key?('base_path') ? options['base_path'] : path + end + + def compare_path + options.is_a?(Hash) && options.key?('compare_path') ? options['compare_path'] : path + end + def selector options["selector"] || "body" end @@ -31,11 +39,11 @@ def before_capture end def base_url - base_urls(path) + base_urls(base_path) end def compare_url - compare_urls(path) + compare_urls(compare_path) end def base_urls(path) diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 151e3c6f..fd4790c3 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -53,8 +53,8 @@ def define_individual_job(label, settings, width) compare_file_name = meta.file_names(width, label, meta.compare_label) jobs = [] - jobs << [label, settings.path, prepare_widths_for_cli(width), settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture, 'invalid1.jpg'] - jobs << [label, settings.path, prepare_widths_for_cli(width), settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture, 'invalid2.jpg'] unless settings.compare_url.nil? + jobs << [label, settings.base_path, prepare_widths_for_cli(width), settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture, 'invalid1.jpg'] + jobs << [label, settings.compare_path, prepare_widths_for_cli(width), settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture, 'invalid2.jpg'] unless settings.compare_url.nil? jobs end diff --git a/spec/configs/test_config--base_compare_paths.yaml b/spec/configs/test_config--base_compare_paths.yaml new file mode 100644 index 00000000..44449539 --- /dev/null +++ b/spec/configs/test_config--base_compare_paths.yaml @@ -0,0 +1,9 @@ +imports: 'test_config--phantom.yaml' + +paths: + home: + base_path: /old-home + compare_path: /new-home + uk_index: + base_path: /old-uk + compare_path: /new-uk diff --git a/spec/gallery_spec.rb b/spec/gallery_spec.rb index ed51d810..cdae8f3a 100644 --- a/spec/gallery_spec.rb +++ b/spec/gallery_spec.rb @@ -28,4 +28,33 @@ expect(dirs["home"][0][:diff][:thumb]).to eq "thumbnails/home/test_image-diff.png" end end + + describe "#figure_out_url" do + it "returns an empty string if it can't find the domain" do + expect(gallery.figure_out_url("missing_domain", "home")).to eq "" + end + + it "returns a full url" do + expected = "http://www.bbc.com/afrique/" + expect(gallery.figure_out_url("afrique", "home")).to eq expected + end + + context "when base_path and compare_path are set" do + let(:config_name) do + get_path_relative_to __FILE__, + "./configs/test_config--base_compare_paths.yaml" + end + let(:gallery) { Wraith::GalleryGenerator.new(config_name, false) } + + it "uses the base path for the first domain" do + expected = "http://www.bbc.com/afrique/old-home" + expect(gallery.figure_out_url("afrique", "home")).to eq expected + end + + it "uses the compare path for the second domain" do + expected = "http://www.bbc.com/russian/new-home" + expect(gallery.figure_out_url("russian", "home")).to eq expected + end + end + end end