Skip to content

Commit

Permalink
TRG-128: Make /api/pages.json endpoint return relative links when the…
Browse files Browse the repository at this point in the history
… sites is configured to use relative links.
  • Loading branch information
eddgrant committed Jan 20, 2022
1 parent 2995ee3 commit 16396e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/govuk_tech_docs/pages.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module GovukTechDocs
class Pages
include GovukTechDocs::PathHelpers
attr_reader :sitemap

def initialize(sitemap, config)
def initialize(sitemap, config, current_page)
@sitemap = sitemap
@config = config
@current_page = current_page
end

def to_json(*_args)
Expand All @@ -18,7 +20,7 @@ def as_json
review = PageReview.new(page, @config)
{
title: page.data.title,
url: "#{@config[:tech_docs][:host]}#{page.url}",
url: "#{get_path_to_resource(@config, page, @current_page)}",
review_by: review.review_by,
owner_slack: review.owner_slack,
}
Expand Down
2 changes: 1 addition & 1 deletion lib/source/api/pages.json.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= GovukTechDocs::Pages.new(sitemap, config).to_json %>
<%= GovukTechDocs::Pages.new(sitemap, config, current_page).to_json %>
20 changes: 18 additions & 2 deletions spec/govuk_tech_docs/pages_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
RSpec.describe GovukTechDocs::Pages do
describe "#to_json" do
it "returns the pages as JSON" do
it "returns the pages as JSON when using absolute links" do
current_page = double(path: "/api/pages.json")
sitemap = double(resources: [
double(url: "/a.html", data: double(title: "A thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "0 days")),
double(url: "/b.html", data: double(title: "B thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "2 days")),
])

json = described_class.new(sitemap, tech_docs: {}).to_json
json = described_class.new(sitemap, {}, current_page).to_json

expect(JSON.parse(json)).to eql([
{ "title" => "A thing", "url" => "/a.html", "review_by" => Date.yesterday.to_s, "owner_slack" => "#2ndline" },
{ "title" => "B thing", "url" => "/b.html", "review_by" => Date.tomorrow.to_s, "owner_slack" => "#2ndline" },
])
end

it "returns the pages as JSON when using relative links" do
current_page = double(path: "/api/pages.json")
sitemap = double(resources: [
double(url: "/a.html", path: "/a.html", data: double(title: "A thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "0 days")),
double(url: "/b/c.html", path: "/b/c.html", data: double(title: "B thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "2 days")),
])

json = described_class.new(sitemap, {:relative_links => true}, current_page).to_json

expect(JSON.parse(json)).to eql([
{ "title" => "A thing", "url" => "../a.html", "review_by" => Date.yesterday.to_s, "owner_slack" => "#2ndline" },
{ "title" => "B thing", "url" => "../b/c.html", "review_by" => Date.tomorrow.to_s, "owner_slack" => "#2ndline" },
])
end
end
end

0 comments on commit 16396e7

Please sign in to comment.