@@ -10,7 +10,6 @@ module XmlBuilder
1010 Unable to extract content from %<url>s using the %<strategy>s strategy.
1111 The site may rely on JavaScript, block automated requests, or expose a structure that needs a different parser.
1212 DESC
13-
1413 EMPTY_FEED_ITEM_TEMPLATE = <<~DESC
1514 No entries were extracted from %<url>s.
1615 Possible causes:
@@ -21,7 +20,6 @@ module XmlBuilder
2120
2221 Try another strategy or reach out to the site owner.
2322 DESC
24-
2523 class << self
2624 def build_rss_feed ( title :, description :, link : nil , items : [ ] , timestamp : nil )
2725 current_time = timestamp || Time . now
@@ -38,57 +36,61 @@ def build_rss_feed(title:, description:, link: nil, items: [], timestamp: nil)
3836 end
3937
4038 def build_error_feed ( message :, title : 'Error' )
41- now = Time . now
42- build_rss_feed (
39+ build_single_item_feed (
4340 title :,
4441 description : "Failed to generate feed: #{ message } " ,
45- items : [
46- {
47- title :,
48- description : message ,
49- pubDate : now
50- }
51- ] ,
52- timestamp : now
42+ item : {
43+ title :,
44+ description : message
45+ }
5346 )
5447 end
5548
5649 def build_access_denied_feed ( url )
57- now = Time . now
58- build_rss_feed (
50+ build_single_item_feed (
5951 title : 'Access Denied' ,
6052 description : 'This URL is not allowed for public auto source generation.' ,
61- items : [
62- {
63- title : 'Access Denied' ,
64- description : "URL '#{ url } ' is not in the allowed list for public auto source." ,
65- pubDate : now
66- }
67- ] ,
68- timestamp : now
53+ item : {
54+ title : 'Access Denied' ,
55+ description : "URL '#{ url } ' is not in the allowed list for public auto source."
56+ }
6957 )
7058 end
7159
7260 def build_empty_feed_warning ( url :, strategy :, site_title : nil )
73- now = Time . now
74- build_rss_feed (
75- title : site_title ? " #{ site_title } - Content Extraction Issue" : 'Content Extraction Issue' ,
61+ feed_title = site_title ? " #{ site_title } - Content Extraction Issue" : 'Content Extraction Issue'
62+ build_single_item_feed (
63+ title : feed_title ,
7664 description : format ( EMPTY_FEED_DESCRIPTION_TEMPLATE , url :, strategy :) ,
77- link : url ,
78- items : [
79- {
80- title : 'Content Extraction Failed' ,
81- description : format ( EMPTY_FEED_ITEM_TEMPLATE , url :) ,
82- link : url ,
83- pubDate : now
84- }
85- ] ,
86- timestamp : now
65+ item : { title : 'Content Extraction Failed' , description : format ( EMPTY_FEED_ITEM_TEMPLATE , url :) ,
66+ link : url } ,
67+ link : url
8768 )
8869 end
8970
9071 private
9172
73+ def build_single_item_feed ( title :, description :, item :, link : nil )
74+ timestamp = Time . now
75+ build_rss_feed (
76+ title :,
77+ description :,
78+ link :,
79+ items : [ feed_item ( item , timestamp :) ] ,
80+ timestamp :
81+ )
82+ end
83+
84+ def feed_item ( item , timestamp :)
85+ feed_item = {
86+ title : item [ :title ] ,
87+ description : item [ :description ] ,
88+ pubDate : timestamp
89+ }
90+ feed_item [ :link ] = item [ :link ] if item [ :link ]
91+ feed_item
92+ end
93+
9294 def build_channel ( xml , title :, description :, link :, now :)
9395 xml . title ( title . to_s )
9496 xml . description ( description . to_s )
@@ -100,27 +102,18 @@ def build_channel(xml, title:, description:, link:, now:)
100102 def build_items ( xml , items , default_pub_date :)
101103 items . each do |item |
102104 xml . item do
103- if ( title = item [ :title ] )
104- xml . title ( title . to_s )
105- end
106-
107- if ( description = item [ :description ] )
108- xml . description ( description . to_s )
109- end
110-
111- if ( link = item [ :link ] )
112- xml . link ( link . to_s )
113- end
114-
115- if ( pub_date = item [ :pubDate ] )
116- xml . pubDate ( format_pub_date ( pub_date ) )
117- else
118- xml . pubDate ( default_pub_date )
119- end
105+ append_text_node ( xml , :title , item [ :title ] )
106+ append_text_node ( xml , :description , item [ :description ] )
107+ append_text_node ( xml , :link , item [ :link ] )
108+ xml . pubDate ( format_pub_date ( item [ :pubDate ] || default_pub_date ) )
120109 end
121110 end
122111 end
123112
113+ def append_text_node ( xml , node_name , value )
114+ xml . public_send ( node_name , value . to_s ) if value
115+ end
116+
124117 def format_pub_date ( pub_date )
125118 pub_date . is_a? ( Time ) ? pub_date . rfc2822 : pub_date . to_s
126119 end
0 commit comments