44require "feedjira"
55
66require_relative "dto"
7+ require_relative "function_pipeline"
78
89module Infrastructure
910 module FeedParser
1011 def self . call ( feed )
12+ pipeline = FunctionPipeline . new (
13+ functions : [
14+ FetchContent ,
15+ ParseContent ,
16+ InitializePodcastDTO ,
17+ InitializeEpisodesDTO ,
18+ InitializeFeedDTO
19+ ] ,
20+ on_error : InitializeEmptyFeedDTO ,
21+ context : { feed : feed }
22+ )
23+
24+ pipeline . call
25+ end
26+
27+ private
28+
29+ FetchContent = -> ( ctx ) do
30+ feed = ctx [ :feed ]
31+
1132 content = if File . exist? ( feed )
1233 File . new ( feed ) . read
1334 else
1435 Net ::HTTP . get ( URI ( feed ) )
1536 end
16- parsed_content = Feedjira . parse ( content )
1737
18- podcast = Infrastructure ::DTO . new (
38+ ctx [ :content ] = content
39+ end
40+
41+ ParseContent = -> ( ctx ) do
42+ ctx [ :parsed_content ] = Feedjira . parse ( ctx [ :content ] )
43+ end
44+
45+ InitializePodcastDTO = -> ( ctx ) do
46+ parsed_content = ctx [ :parsed_content ]
47+ ctx [ :podcast ] = Infrastructure ::DTO . new (
1948 name : parsed_content . title ,
2049 description : parsed_content . description ,
2150 feed : parsed_content . itunes_new_feed_url ,
2251 website : parsed_content . url
2352 )
53+ end
2454
25- episodes = parsed_content . entries . map do |e |
55+ InitializeEpisodesDTO = -> ( ctx ) do
56+ parsed_content = ctx [ :parsed_content ]
57+ ctx [ :episodes ] = parsed_content . entries . map do |e |
2658 Infrastructure ::DTO . new (
2759 title : e . title ,
2860 release_date : e . published . iso8601 ,
@@ -31,11 +63,17 @@ def self.call(feed)
3163 external_id : e . entry_id
3264 )
3365 end
66+ end
3467
68+ InitializeFeedDTO = -> ( ctx ) do
69+ podcast = ctx [ :podcast ]
70+ episodes = ctx [ :episodes ]
3571 # The #reverse is necessary to put the oldest episodes on the top of the feed.
36- Infrastructure ::DTO . new ( podcast : podcast , episodes : episodes . reverse )
37- rescue NoMethodError
38- Infrastructure ::DTO . new ( podcast : nil , episodes : nil )
72+ ctx [ :feed ] = Infrastructure ::DTO . new ( podcast : podcast , episodes : episodes . reverse )
73+ end
74+
75+ InitializeEmptyFeedDTO = -> ( ctx ) do
76+ ctx [ :feed ] = Infrastructure ::DTO . new ( podcast : nil , episodes : nil )
3977 end
4078 end
4179end
0 commit comments