Skip to content

Commit 3e8cd0c

Browse files
Steve DaySteve Day
Steve Day
authored and
Steve Day
committed
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 url changes between domains (e.g. testing a migration to a new slug library or a new CMS). Closes #552
1 parent f4859e5 commit 3e8cd0c

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

lib/wraith/gallery.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "fileutils"
44
require "wraith/wraith"
55
require "wraith/helpers/logger"
6+
require "wraith/helpers/capture_options"
67

78
class Wraith::GalleryGenerator
89
include Logging
@@ -60,15 +61,22 @@ def matcher(match, filename, dirname, category)
6061
end
6162

6263
def figure_out_url(group, category)
63-
root = wraith.domains["#{group}"]
64+
group = "#{group}"
65+
root = wraith.domains[group]
66+
base_or_compare = wraith.domains.keys.index(group) == 0 ? :base : :compare
6467
return "" if root.nil?
65-
path = get_path(category)
68+
path = get_path(category, base_or_compare)
6669
url = root + path
6770
url
6871
end
6972

70-
def get_path(category)
71-
wraith.paths[category]["path"] || wraith.paths[category]
73+
def get_path(category, base_or_compare)
74+
path_options = CaptureOptions.new(wraith.paths[category], wraith)
75+
if base_or_compare == :base
76+
path_options.base_path
77+
else
78+
path_options.compare_path
79+
end
7280
end
7381

7482
def get_group_from_match(match)

lib/wraith/helpers/capture_options.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def path
1313
casper?(options)
1414
end
1515

16+
def base_path
17+
options.is_a?(Hash) && options.key?('base_path') ? options['base_path'] : path
18+
end
19+
20+
def compare_path
21+
options.is_a?(Hash) && options.key?('compare_path') ? options['compare_path'] : path
22+
end
23+
1624
def selector
1725
options["selector"] || "body"
1826
end
@@ -31,11 +39,11 @@ def before_capture
3139
end
3240

3341
def base_url
34-
base_urls(path)
42+
base_urls(base_path)
3543
end
3644

3745
def compare_url
38-
compare_urls(path)
46+
compare_urls(compare_path)
3947
end
4048

4149
def base_urls(path)

lib/wraith/save_images.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def define_individual_job(label, settings, width)
5353
compare_file_name = meta.file_names(width, label, meta.compare_label)
5454

5555
jobs = []
56-
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']
57-
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?
56+
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']
57+
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?
5858

5959
jobs
6060
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
imports: 'test_config--phantom.yaml'
2+
3+
paths:
4+
home:
5+
base_path: /old-home
6+
compare_path: /new-home
7+
uk_index:
8+
base_path: /old-uk
9+
compare_path: /new-uk

spec/gallery_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,33 @@
2828
expect(dirs["home"][0][:diff][:thumb]).to eq "thumbnails/home/test_image-diff.png"
2929
end
3030
end
31+
32+
describe "#figure_out_url" do
33+
it "returns an empty string if it can't find the domain" do
34+
expect(gallery.figure_out_url("missing_domain", "home")).to eq ""
35+
end
36+
37+
it "returns a full url" do
38+
expected = "http://www.bbc.com/afrique/"
39+
expect(gallery.figure_out_url("afrique", "home")).to eq expected
40+
end
41+
42+
context "when base_path and compare_path are set" do
43+
let(:config_name) do
44+
get_path_relative_to __FILE__,
45+
"./configs/test_config--base_compare_paths.yaml"
46+
end
47+
let(:gallery) { Wraith::GalleryGenerator.new(config_name, false) }
48+
49+
it "uses the base path for the first domain" do
50+
expected = "http://www.bbc.com/afrique/old-home"
51+
expect(gallery.figure_out_url("afrique", "home")).to eq expected
52+
end
53+
54+
it "uses the compare path for the second domain" do
55+
expected = "http://www.bbc.com/russian/new-home"
56+
expect(gallery.figure_out_url("russian", "home")).to eq expected
57+
end
58+
end
59+
end
3160
end

0 commit comments

Comments
 (0)