lib/html2rss/feed_builder.rb in html2rss-0.6.0 vs lib/html2rss/feed_builder.rb in html2rss-0.7.0
- old
+ new
@@ -1,7 +1,7 @@
require 'rss'
-require_relative 'item'
+require 'mime/types'
module Html2rss
##
# The purpose is to build the feed, consisting of
#
@@ -18,13 +18,11 @@
# @return [RSS:Rss]
def rss
RSS::Maker.make('2.0') do |maker|
add_channel(maker)
- feed_items.map do |feed_item|
- add_item(feed_item, maker.items.new_item)
- end
+ feed_items.map { |feed_item| add_item(feed_item, maker.items.new_item) }
end
end
private
@@ -46,13 +44,28 @@
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
- feed_item.categories.each do |category|
- rss_item.categories.new_category.content = category
- end
+ 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)
+ add_guid(feed_item, rss_item)
+ 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
+ end
+
+ def add_guid(feed_item, rss_item)
rss_item.guid.content = Digest::SHA1.hexdigest(feed_item.title)
rss_item.guid.isPermaLink = false
end
end
end