Skip to content

Commit

Permalink
Use summary when there isn't a title
Browse files Browse the repository at this point in the history
Follwing the
[spec](https://validator.w3.org/feed/docs/rss2.html#hrelementsOfLtitemgt)
for RSS, if the title property isn't present for a story, we should
display the description, which is the [summary in
feedjira](https://github.com/feedjira/feedjira/blob/d0a28c3ba6437d70e4ca886ae5c05287bf1b6821/lib/feedjira/parser/itunes_rss_item.rb#L13).
  • Loading branch information
gabrielpoca committed Dec 1, 2017
1 parent 0a5cbb5 commit 3543597
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/repositories/story_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.add(entry, feed)
entry.url = normalize_url(entry.url, feed.url) unless entry.url.nil?

Story.create(feed: feed,
title: sanitize(entry.title),
title: extract_title(entry),
permalink: entry.url,
body: extract_content(entry),
is_read: false,
Expand Down Expand Up @@ -95,6 +95,12 @@ def self.extract_content(entry)
expand_absolute_urls(sanitized_content, entry.url)
end

def self.extract_title(entry)
return sanitize(entry.title) if entry.title.present?
return sanitize(entry.summary) if entry.summary.present?
"There isn't a title for this story"
end

def self.sanitize(content)
Loofah.fragment(content.gsub(/<wbr\s*>/i, ""))
.scrub!(:prune)
Expand Down
17 changes: 17 additions & 0 deletions spec/repositories/story_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
end
end

describe ".extract_title" do
let(:entry) do
end

it "returns the title if there is a title" do
entry = double(title: "title", summary: "summary")

expect(StoryRepository.extract_title(entry)).to eq "title"
end

it "returns the summary if there isn't a title" do
entry = double(title: "", summary: "summary")

expect(StoryRepository.extract_title(entry)).to eq "summary"
end
end

describe ".extract_content" do
let(:entry) do
double(url: "http://mdswanson.com",
Expand Down

0 comments on commit 3543597

Please sign in to comment.