使用Ruby的RSS类来解析Atom和RSS

时间:2013-02-06 09:55:14

标签: ruby rss atom-feed

我想使用Ruby的RSS class来解析Atom和RSS提要,所以我可以从中提取链接。如何区分代码中的两种类型?

我已经准备好了解析器响应。

response = RSS::Parser.parse(rss_url, false)

1 个答案:

答案 0 :(得分:5)

我找到了Feed对象的.feed_type方法,并按如下方式使用它:

if response.feed_type == "rss"
  puts "hey rss"
  response.channel.items.each{ |item| links += "'#{item.link}'," }
elsif response.feed_type == "atom"
  puts "hey atom"
  response.entries.each{ |entry| links += "'#{entry.link.href}'," }
else
  puts "something went wrong"
end