Skip to content
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: 44 additions & 6 deletions lib/pod/infrastructure/feed_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,57 @@
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)

podcast = Infrastructure::DTO.new(
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(
name: parsed_content.title,
description: parsed_content.description,
feed: parsed_content.itunes_new_feed_url,
website: parsed_content.url
)
end

episodes = parsed_content.entries.map do |e|
InitializeEpisodesDTO = ->(ctx) do
parsed_content = ctx[:parsed_content]
ctx[:episodes] = parsed_content.entries.map do |e|
Infrastructure::DTO.new(
title: e.title,
release_date: e.published.iso8601,
Expand All @@ -31,11 +63,17 @@ 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.
Infrastructure::DTO.new(podcast: podcast, episodes: episodes.reverse)
rescue NoMethodError
Infrastructure::DTO.new(podcast: nil, episodes: nil)
ctx[:feed] = Infrastructure::DTO.new(podcast: podcast, episodes: episodes.reverse)
end

InitializeEmptyFeedDTO = ->(ctx) do
ctx[:feed] = Infrastructure::DTO.new(podcast: nil, episodes: nil)
end
end
end
42 changes: 42 additions & 0 deletions lib/pod/infrastructure/function_pipeline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Infrastructure
class FunctionPipeline
def initialize(functions:, on_error:, context: {})
@functions = functions
@on_error = on_error
@context = Context.new(context)
end

def call
@functions.each { |f| f.call(@context) }
rescue => e
@context[:error] = e
@on_error.call(@context)
ensure
return @context.last_entry # standard:disable Lint/EnsureReturn
end

private

class Context
def initialize(context)
@state = context
end

def [](key)
@state[key.to_sym]
end

def []=(key, value)
@state[key.to_sym] = value
@state[:last_entry] = key
end

def last_entry
key = @state[:last_entry]
@state[key]
end
end
end
end