diff --git a/app/labor/podcast_feed.rb b/app/labor/podcast_feed.rb index bcc26465c..03a0fcbf0 100644 --- a/app/labor/podcast_feed.rb +++ b/app/labor/podcast_feed.rb @@ -12,10 +12,11 @@ class PodcastFeed rss = HTTParty.get(podcast.feed_url).body feed = RSS::Parser.parse(rss, false) feed.items.first(num).each do |item| - if !existing_episode(item, podcast) - create_new_episode(item, podcast) - elsif (ep = existing_episode(item, podcast).first) + ep = existing_episode(item, podcast).first + if ep update_existing_episode(ep, item, podcast) + else + create_new_episode(item, podcast) end end feed.items.size @@ -23,6 +24,18 @@ class PodcastFeed Rails.logger.error(e) end + private + + # returns empty array if an episode doesn't exist + def existing_episode(item, podcast) + # presence returns nil if the query is an empty array, otherwise returns the array + podcasts = PodcastEpisode.where(media_url: item.enclosure.url).presence || + PodcastEpisode.where(title: item.title).presence || + PodcastEpisode.where(guid: item.guid.to_s).presence || + (podcast.unique_website_url? && PodcastEpisode.where(website_url: item.link).presence) + podcasts.to_a + end + def create_new_episode(item, podcast) ep = PodcastEpisode.new ep.title = item.title @@ -54,14 +67,6 @@ class PodcastFeed update_media_url(episode, item) end - def existing_episode(item, podcast) - # presence returns nil if the query is an empty array, otherwise returns the array - PodcastEpisode.where(media_url: item.enclosure.url).presence || - PodcastEpisode.where(title: item.title).presence || - PodcastEpisode.where(guid: item.guid.to_s).presence || - (podcast.unique_website_url? && PodcastEpisode.where(website_url: item.link).presence) - end - def get_media_url(episode, item, podcast) episode.media_url = if Rails.env.test? || HTTParty.head(item.enclosure.url.gsub(/http:/, "https:")).code == 200 diff --git a/spec/labor/podcast_feed_spec.rb b/spec/labor/podcast_feed_spec.rb index ad13a7ddb..2d128ca26 100644 --- a/spec/labor/podcast_feed_spec.rb +++ b/spec/labor/podcast_feed_spec.rb @@ -13,13 +13,37 @@ RSpec.describe PodcastFeed, vcr: vcr_option do podcast end - it "fetches podcast episodes" do - PodcastFeed.new.get_episodes(podcast, 2) - expect(PodcastEpisode.all.size).to eq(2) + context "when creating" do + it "fetches podcast episodes" do + expect do + PodcastFeed.new.get_episodes(podcast, 2) + end.to change(PodcastEpisode, :count).by(2) + end + + it "fetches correct podcasts" do + PodcastFeed.new.get_episodes(podcast, 2) + episodes = podcast.podcast_episodes + expect(episodes.pluck(:title).sort).to eq(["Analyse Asia with Bernard Leong", "IFTTT Architecture with Nicky Leach"]) + expect(episodes.pluck(:media_url).sort).to eq(%w[https://traffic.libsyn.com/sedaily/AnalyseAsia.mp3 https://traffic.libsyn.com/sedaily/IFTTT.mp3]) + end end - it "does not refetch already fetched episodes" do - 2.times { PodcastFeed.new.get_episodes(podcast, 2) } - expect(PodcastEpisode.all.size).to eq(2) + context "when updating" do + let!(:episode) { create(:podcast_episode, media_url: "http://traffic.libsyn.com/sedaily/AnalyseAsia.mp3", title: "Old Title", published_at: nil) } + let!(:episode2) { create(:podcast_episode, media_url: "http://traffic.libsyn.com/sedaily/IFTTT.mp3", title: "SuperPodcast", published_at: nil) } + + it "does not refetch already fetched episodes" do + expect do + PodcastFeed.new.get_episodes(podcast, 2) + end.not_to change(PodcastEpisode, :count) + end + + it "updates published_at for existing episodes" do + PodcastFeed.new.get_episodes(podcast, 2) + episode.reload + episode2.reload + expect(episode.published_at).to be_truthy + expect(episode2.published_at).to be_truthy + end end end