-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from alphagov/fix-body-content-parsing
Refactor parsing of body content
- Loading branch information
Showing
6 changed files
with
114 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module PublishingApi | ||
class BodyContent | ||
def initialize(raw_content) | ||
@content = case raw_content | ||
when String | ||
raw_content | ||
when Array | ||
raw_content.find { _1[:content_type] == "text/html" }&.dig(:content) | ||
end | ||
end | ||
|
||
def html_content | ||
content | ||
end | ||
|
||
def text_content | ||
return nil unless html_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 | ||
|
||
private | ||
|
||
attr_reader :content | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
RSpec.describe PublishingApi::BodyContent do | ||
subject(:body_content) { described_class.new(content) } | ||
|
||
context "when the content is a plain string" do | ||
let(:content) { "Hello, world!" } | ||
|
||
describe "#html_content" do | ||
subject(:html_content) { body_content.html_content } | ||
|
||
it { is_expected.to eq("Hello, world!") } | ||
end | ||
|
||
describe "#text_content" do | ||
subject(:text_content) { body_content.text_content } | ||
|
||
it { is_expected.to eq("Hello, world!") } | ||
end | ||
|
||
describe "#summarized_text_content" do | ||
subject(:summarized_text_content) { body_content.summarized_text_content(length: 6) } | ||
|
||
it { is_expected.to eq("Hello…") } | ||
end | ||
end | ||
|
||
context "when the content is an array of typed content that includes text/html content" do | ||
let(:content) do | ||
[ | ||
{ content_type: "application/json", content: '{"foo": "bar"}' }, | ||
{ content_type: "text/html", content: "<blink>Hello, world!</blink>" }, | ||
] | ||
end | ||
|
||
describe "#html_content" do | ||
subject(:html_content) { body_content.html_content } | ||
|
||
it { is_expected.to eq("<blink>Hello, world!</blink>") } | ||
end | ||
|
||
describe "#text_content" do | ||
subject(:text_content) { body_content.text_content } | ||
|
||
it { is_expected.to eq("Hello, world!") } | ||
end | ||
|
||
describe "#summarized_text_content" do | ||
subject(:summarized_text_content) { body_content.summarized_text_content(length: 6) } | ||
|
||
it { is_expected.to eq("Hello…") } | ||
end | ||
end | ||
|
||
context "when the content is an array of typed content that doesn't include text/html content" do | ||
let(:content) do | ||
[ | ||
{ content_type: "application/json", content: '{"foo": "bar"}' }, | ||
] | ||
end | ||
|
||
describe "#html_content" do | ||
subject(:html_content) { body_content.html_content } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
describe "#text_content" do | ||
subject(:text_content) { body_content.text_content } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
describe "#summarized_text_content" do | ||
subject(:summarized_text_content) { body_content.summarized_text_content } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
end | ||
end |