Skip to content

Commit

Permalink
Merge pull request #23 from gustavothecoder/revert-22-refactor-feed-p…
Browse files Browse the repository at this point in the history
…arser

Revert "Refactor `FeedParser` to use lambdas"
  • Loading branch information
gustavothecoder authored Nov 15, 2023
2 parents 89692ed + 844b6e1 commit f59a3cb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 86 deletions.
50 changes: 6 additions & 44 deletions lib/pod/infrastructure/feed_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,25 @@
require "feedjira"

require_relative "dto"
require_relative "function_pipeline"

module Infrastructure
module FeedParser
def self.call(feed)
pipeline = FunctionPipeline.new(
functions: [
FetchContent,
ParseContent,
InitializePodcastDTO,
InitializeEpisodesDTO,
InitializeFeedDTO
],
on_error: InitializeEmptyFeedDTO,
context: {feed: feed}
)

pipeline.call
end

private

FetchContent = ->(ctx) do
feed = ctx[:feed]

content = if File.exist?(feed)
File.new(feed).read
else
Net::HTTP.get(URI(feed))
end
parsed_content = Feedjira.parse(content)

ctx[:content] = content
end

ParseContent = ->(ctx) do
ctx[:parsed_content] = Feedjira.parse(ctx[:content])
end

InitializePodcastDTO = ->(ctx) do
parsed_content = ctx[:parsed_content]
ctx[:podcast] = Infrastructure::DTO.new(
podcast = Infrastructure::DTO.new(
name: parsed_content.title,
description: parsed_content.description,
feed: parsed_content.itunes_new_feed_url,
website: parsed_content.url
)
end

InitializeEpisodesDTO = ->(ctx) do
parsed_content = ctx[:parsed_content]
ctx[:episodes] = parsed_content.entries.map do |e|
episodes = parsed_content.entries.map do |e|
Infrastructure::DTO.new(
title: e.title,
release_date: e.published.iso8601,
Expand All @@ -63,17 +31,11 @@ def self.call(feed)
external_id: e.entry_id
)
end
end

InitializeFeedDTO = ->(ctx) do
podcast = ctx[:podcast]
episodes = ctx[:episodes]
# The #reverse is necessary to put the oldest episodes on the top of the feed.
ctx[:feed] = Infrastructure::DTO.new(podcast: podcast, episodes: episodes.reverse)
end

InitializeEmptyFeedDTO = ->(ctx) do
ctx[:feed] = Infrastructure::DTO.new(podcast: nil, episodes: nil)
Infrastructure::DTO.new(podcast: podcast, episodes: episodes.reverse)
rescue NoMethodError
Infrastructure::DTO.new(podcast: nil, episodes: nil)
end
end
end
42 changes: 0 additions & 42 deletions lib/pod/infrastructure/function_pipeline.rb

This file was deleted.

0 comments on commit f59a3cb

Please sign in to comment.