From b4ae4c1d43c9f0bd597e76dcb3324ac6f89e8359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Po=C3=A7a?= Date: Fri, 1 Dec 2017 09:01:28 +0000 Subject: [PATCH] Use enclosure_url when url is missing Some feeds, especially the podcasts, don't have an URL for their stories. That's fine, the url is not mandatory but we decided to link to the media object when it is present and the url is not. --- app/repositories/story_repository.rb | 10 +++++++--- spec/repositories/story_repository_spec.rb | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/repositories/story_repository.rb b/app/repositories/story_repository.rb index e89b8ab0d..4967d77ac 100644 --- a/app/repositories/story_repository.rb +++ b/app/repositories/story_repository.rb @@ -6,11 +6,9 @@ class StoryRepository extend UrlHelpers def self.add(entry, feed) - entry.url = normalize_url(entry.url, feed.url) unless entry.url.nil? - Story.create(feed: feed, title: extract_title(entry), - permalink: entry.url, + permalink: extract_url(entry, feed), body: extract_content(entry), is_read: false, is_starred: false, @@ -83,6 +81,12 @@ def self.read_count Story.where(is_read: true).count end + def self.extract_url(entry, feed) + return entry.enclosure_url if entry.url.nil? && entry.enclosure_url.present? + + normalize_url(entry.url, feed.url) unless entry.url.nil? + end + def self.extract_content(entry) sanitized_content = "" diff --git a/spec/repositories/story_repository_spec.rb b/spec/repositories/story_repository_spec.rb index 4a1e1d1d6..b7eed53bf 100644 --- a/spec/repositories/story_repository_spec.rb +++ b/spec/repositories/story_repository_spec.rb @@ -26,6 +26,22 @@ end end + describe ".extract_url" do + it "returns the url" do + feed = double(url: "http://github.com") + entry = double(url: "https://github.com/swanson/stringer") + + expect(StoryRepository.extract_url(entry, feed)).to eq "https://github.com/swanson/stringer" + end + + it "returns the enclosure_url when the url is nil" do + feed = double(url: "http://github.com") + entry = double(url: nil, enclosure_url: "https://github.com/swanson/stringer") + + expect(StoryRepository.extract_url(entry, feed)).to eq "https://github.com/swanson/stringer" + end + end + describe ".extract_title" do let(:entry) do end