lib/html2rss/feed_builder.rb in html2rss-0.8.1 vs lib/html2rss/feed_builder.rb in html2rss-0.8.2
- old
+ new
@@ -16,57 +16,60 @@
##
# @return [RSS:Rss]
def rss
RSS::Maker.make('2.0') do |maker|
- add_channel(maker)
+ add_channel(maker.channel)
- feed_items.map { |feed_item| add_item(feed_item, maker.items.new_item) }
+ items.each { |item| add_item(item, maker.items.new_item) }
end
end
- private
+ def self.add_categories(categories, item_maker)
+ categories.each { |category| item_maker.categories.new_category.content = category }
+ end
- attr_reader :config
+ def self.add_enclosure_from_url(url, item_maker)
+ return unless url
- def add_channel(maker)
- %i[language author title description link ttl].each do |attribute_name|
- maker.channel.public_send("#{attribute_name}=".to_sym, config.public_send(attribute_name))
- end
+ enclosure = item_maker.enclosure
+ content_type = MIME::Types.type_for(File.extname(url).delete('.'))
- maker.channel.generator = "html2rss V. #{::Html2rss::VERSION}"
- maker.channel.lastBuildDate = Time.now.to_s
+ enclosure.type = content_type.any? ? content_type.first.to_s : 'application/octet-stream'
+ enclosure.length = 0
+ enclosure.url = url
end
- def feed_items
- @feed_items ||= Item.from_url(config.url, config).keep_if(&:valid?)
+ def self.add_guid(item, item_maker)
+ guid = item_maker.guid
+ guid.content = Digest::SHA1.hexdigest(item.title)
+ guid.isPermaLink = false
end
- def add_item(feed_item, rss_item)
- feed_item.available_attributes.each do |attribute_name|
- rss_item.public_send("#{attribute_name}=".to_sym, feed_item.public_send(attribute_name))
- end
+ private
- feed_item.categories.each { |category| rss_item.categories.new_category.content = category }
- add_enclosure_from_url(feed_item.enclosure_url, rss_item) if config.attribute?(:enclosure)
+ attr_reader :config
- add_guid(feed_item, rss_item)
+ def add_channel(channel_maker)
+ %i[language author title description link ttl].each do |attribute_name|
+ channel_maker.public_send("#{attribute_name}=", config.public_send(attribute_name))
+ end
+
+ channel_maker.generator = "html2rss V. #{::Html2rss::VERSION}"
+ channel_maker.lastBuildDate = Time.now
end
- def add_enclosure_from_url(url, rss_item)
- content_type = MIME::Types.type_for(File.extname(url).delete('.'))
-
- rss_item.enclosure.type = if content_type && content_type.first
- content_type.first.to_s
- else
- 'application/octet-stream'
- end
- rss_item.enclosure.length = 0
- rss_item.enclosure.url = url
+ def items
+ @items ||= Item.from_url(config.url, config)
end
- def add_guid(feed_item, rss_item)
- rss_item.guid.content = Digest::SHA1.hexdigest(feed_item.title)
- rss_item.guid.isPermaLink = false
+ def add_item(item, item_maker)
+ item.available_attributes.each do |attribute_name|
+ item_maker.public_send("#{attribute_name}=", item.public_send(attribute_name))
+ end
+
+ self.class.add_categories(item.categories, item_maker)
+ self.class.add_enclosure_from_url(item.enclosure_url, item_maker) if item.enclosure?
+ self.class.add_guid(item, item_maker)
end
end
end