Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Refactor FeedParser to use lambdas" #23

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.