# File lib/parsers/rss.rb, line 28
28: def self.parse(xml, loose)
29: begin
30: rss = parser.parse(xml)
31: rescue Exception => e
32: #puts "Parser #{parser} failed because #{e.message.gsub("\n",', ')}"
33: return nil
34: end
35:
36: # check for channel to make sure we're only dealing with RSS.
37: rss && rss.respond_to?(:channel) ? package(rss, loose) : nil
38: end
# File lib/parsers/rss.rb, line 47
47: def self.package(rss, loose)
48: feed = Feed.new(self)
49:
50: # channel elements
51: feed_mapping = {
52: :generator => :generator,
53: :title => :title,
54: :urls => :link,
55: :description => :description,
56: :copyright => :copyright,
57: :authors => :managingEditor,
58: :last_updated => [:lastBuildDate, :pubDate, :dc_date],
59: :id => :guid,
60: :ttl => :ttl
61: }
62:
63: # make two passes, to catch all possible root elements
64: map_functions!(feed_mapping, rss, feed)
65: map_functions!(feed_mapping, rss.channel, feed)
66:
67: # custom channel elements
68: feed.image = rss.image ? rss.image.url : nil
69: feed.skip_hours = skip(rss, :skipHours)
70: feed.skip_days = skip(rss, :skipDays)
71:
72: # item elements
73: item_mapping = {
74: :date_published => [:pubDate, :dc_date],
75: :urls => :link,
76: :enclosures => :enclosure,
77: :description => :description,
78: :content => [:content_encoded, :description],
79: :title => :title,
80: :authors => [:author, :dc_creator],
81: :last_updated => [:pubDate, :dc_date] # This is effectively an alias for date_published for this parser.
82: }
83:
84: rss.items.each do |rss_item|
85: feed_entry = Entry.new
86: map_functions!(item_mapping, rss_item, feed_entry)
87:
88: # custom item elements
89: feed_entry.id = rss_item.guid.content if rss_item.respond_to?(:guid) && rss_item.guid
90: feed_entry.copyright = rss.copyright if rss_item.respond_to? :copyright
91: feed_entry.categories = loose ?
92: rss_item.categories.collect{|c|c.content} :
93: [rss_item.categories.first.content] rescue []
94:
95: feed.entries << feed_entry
96: end
97:
98: feed
99: end
# File lib/parsers/rss.rb, line 101
101: def self.skip(parser, attribute)
102: case attribute
103: when :skipHours then attributes = :hours
104: when :skipDays then attributes = :days
105: end
106: channel = parser.channel
107:
108: return nil unless channel.respond_to?(attribute) && a = channel.send(attribute)
109: a.send(attributes).collect{|e| e.content}
110: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.