44require "feedjira"
55
66require_relative "dto"
7- require_relative "function_pipeline"
87
98module Infrastructure
109 module FeedParser
1110 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-
3211 content = if File . exist? ( feed )
3312 File . new ( feed ) . read
3413 else
3514 Net ::HTTP . get ( URI ( feed ) )
3615 end
16+ parsed_content = Feedjira . parse ( content )
3717
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 (
18+ podcast = Infrastructure ::DTO . new (
4819 name : parsed_content . title ,
4920 description : parsed_content . description ,
5021 feed : parsed_content . itunes_new_feed_url ,
5122 website : parsed_content . url
5223 )
53- end
5424
55- InitializeEpisodesDTO = -> ( ctx ) do
56- parsed_content = ctx [ :parsed_content ]
57- ctx [ :episodes ] = parsed_content . entries . map do |e |
25+ episodes = parsed_content . entries . map do |e |
5826 Infrastructure ::DTO . new (
5927 title : e . title ,
6028 release_date : e . published . iso8601 ,
@@ -63,17 +31,11 @@ def self.call(feed)
6331 external_id : e . entry_id
6432 )
6533 end
66- end
6734
68- InitializeFeedDTO = -> ( ctx ) do
69- podcast = ctx [ :podcast ]
70- episodes = ctx [ :episodes ]
7135 # The #reverse is necessary to put the oldest episodes on the top of the feed.
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 )
36+ Infrastructure ::DTO . new ( podcast : podcast , episodes : episodes . reverse )
37+ rescue NoMethodError
38+ Infrastructure ::DTO . new ( podcast : nil , episodes : nil )
7739 end
7840 end
7941end
0 commit comments