Skip to content

Commit

Permalink
Merge pull request #59 from alphagov/parts-body
Browse files Browse the repository at this point in the history
Handle body content in `parts`
  • Loading branch information
csutter authored Oct 25, 2023
2 parents 692c61e + 53e7288 commit 3204a63
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem "railties", RAILS_GEMS_VERSION

gem "bootsnap", require: false
gem "govuk_app_config"
gem "loofah"
gem "oj"

# Gems specific to the document sync worker that aren't required for the main Rails API app
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ DEPENDENCIES
govuk_test
json_schemer
jsonpath
loofah
oj
plek
railties (= 7.0.7.2)
Expand Down
22 changes: 22 additions & 0 deletions lib/document_sync_worker/document/content_with_multiple_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module DocumentSyncWorker::Document
class ContentWithMultipleTypes
def initialize(content_with_multiple_types)
@content_with_multiple_types = content_with_multiple_types
end

def html_content
@content_with_multiple_types.find { _1["content_type"] == "text/html" }&.dig("content")
end

def text_content
Loofah
.document(html_content)
.to_text(encode_special_chars: false)
.squish
end

def summarized_text_content(length: 75, omission: "…", separator: " ")
text_content.truncate(length, omission:, separator:)
end
end
end
8 changes: 7 additions & 1 deletion lib/document_sync_worker/document/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ def historic?
def parts
document_hash
.dig("details", "parts")
&.map { { slug: _1["slug"], title: _1["title"] } } || []
&.map do
{
slug: _1["slug"],
title: _1["title"],
body: ContentWithMultipleTypes.new(_1["body"]).summarized_text_content,
}
end
end
end
end
Expand Down
35 changes: 31 additions & 4 deletions spec/lib/document_sync_worker/document/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
context "when the document has no parts" do
let(:parts) { nil }

it { is_expected.to be_empty }
it { is_expected.to be_nil }
end

context "when the document has parts" do
Expand All @@ -336,17 +336,44 @@
{
"title" => "Part 1",
"slug" => "/part-1",
"body" => "Part 1 body",
"body" => [
{
"content" => "Part 1 body",
"content_type" => "text/simples",
},
{
"content" => "<div class=\"lipsum\">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur <blink>tincidunt sem erat</blink>, eget blandit urna porta ac. Mauris lobortis tincidunt dui at pharetra.</div>",
"content_type" => "text/html",
},
],
},
{
"title" => "Part 2",
"slug" => "/part-2",
"body" => "Part 2 body",
"body" => [
{
"content" => "I have no HTML content :(",
"content_type" => "text/simples",
},
],
},
]
end

it { is_expected.to eq([{ title: "Part 1", slug: "/part-1" }, { title: "Part 2", slug: "/part-2" }]) }
it "contains the expected titles" do
expect(extracted_parts.map { _1[:title] }).to eq(["Part 1", "Part 2"])
end

it "contains the expected slugs" do
expect(extracted_parts.map { _1[:slug] }).to eq(%w[/part-1 /part-2])
end

it "contains the expected body with HTML stripped and truncated" do
expect(extracted_parts.map { _1[:body] }).to eq([
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur…",
"",
])
end
end
end
end
Expand Down
36 changes: 28 additions & 8 deletions spec/lib/document_sync_worker_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
668cd623-c7a8-4159-9575-90caac36d4b4 c31256e8-f328-462b-993f-dce50b7892e9
],
locale: "en",
parts: [],
parts: nil,
)

expect(result[:content]).to start_with("<div class=\"govspeak\"><p>The government has")
Expand Down Expand Up @@ -64,11 +64,31 @@
],
locale: "en",
parts: [
{ slug: "warnings-and-insurance", title: "Warnings and insurance" },
{ slug: "entry-requirements", title: "Entry requirements" },
{ slug: "safety-and-security", title: "Safety and security" },
{ slug: "health", title: "Health" },
{ slug: "getting-help", title: "Getting help" },
{
slug: "warnings-and-insurance",
title: "Warnings and insurance",
body: "The Foreign, Commonwealth & Development Office (FCDO) provides advice…",
},
{
slug: "entry-requirements",
title: "Entry requirements",
body: "This advice reflects the UK government’s understanding of current rules…",
},
{
slug: "safety-and-security",
title: "Safety and security",
body: "Terrorism There is a high threat of terrorist attack globally affecting UK…",
},
{
slug: "health",
title: "Health",
body: "Before you travel check that: your destination can provide the healthcare…",
},
{
slug: "getting-help",
title: "Getting help",
body: "The Foreign, Commonwealth & Development Office (FCDO) cannot provide…",
},
],
)

Expand Down Expand Up @@ -101,7 +121,7 @@
3dbeb4a3-33c0-4bda-bd21-b721b0f8736f
],
locale: "en",
parts: [],
parts: nil,
)

expect(result[:content]).to start_with("<div class=\"govspeak\"><p>In the UEFA Champions")
Expand Down Expand Up @@ -131,7 +151,7 @@
content_purpose_supergroup: "other",
part_of_taxonomy_tree: [],
locale: "en",
parts: [],
parts: nil,
)

expect(result[:content]).to be_blank
Expand Down

0 comments on commit 3204a63

Please sign in to comment.