饲料在铁路中进行解析

时间:2010-10-05 06:12:39

标签: ruby-on-rails parsing routing hyperlink feeds

我正在使用Ubuntu 10.04,我使用feed-zirra来解析RSS提要,我有MySQL数据库。

我正在尝试解析来自Times of India Top Stories的RSS Feed。第一个链接似乎有问题,我相信很快,TOI的人会纠正它。但无论如何,我不想在以后遇到类似的错误,这就是为什么我想问你们这些问题如何解决。

只需看看这个, 特别是寻找链接

<item>
  <title>CWG: Abhinav Bindra, Gagan Narang win first Gold for India</title
  <description>Abhinav Bindra and Gagan Narang on Tuesday bagged Gold for the men's 10 m air rifle pair's event, getting India its first gold in the 19th Commonwealth Games.</description>
<link>/cwgarticleshow/6688747.cms</link>
<guid>/cwgarticleshow/6688747.cms</guid>
<pubDate>Tue, 05 Oct 2010 04:57:46 GMT</pubDate>
</item>

链接为<link>/cwgarticleshow/6688747.cms</link>

现在,当我点击视图中的链接时,它会被路由到http://localhost:3000/cwgarticleshow/6688747.cms而不是http://timesofindia.indiatimes.com/cwgarticleshow/6688747.cms

我得到的错误是

**Routing Error**

No route matches "/cwgarticleshow/6688747.cms" with {:method=>:get}

如何更正此类错误?

期待您的帮助和支持

由于

2 个答案:

答案 0 :(得分:0)

您只需将http://timesofindia.indiatimes.com添加到链接标记值,就可以了。

答案 1 :(得分:0)

您可以使用URI类。例如,您可以定义以下方法

require "uri"

def repair_link(feed_link)
  uri = URI.parse(feed_link)
  uri.scheme ||= "http"
  uri.host   ||= "timesofindia.indiatimes.com"
  uri.to_s
end

如果URL尚未填充,它将设置方案和主机部分。因此,如果您将其称为普通链接(如http://foo/bar.cms),则不会更改任何内容。

最后一件事 - 你可能应该在某处捕获异常,因为#parse方法在URI无效的情况下引发异常InvalidURIError。但是你应该如何处理它。