Ruby RSS::Parser.to_s 默默失败?

发布于 2024-11-30 17:44:07 字数 1080 浏览 2 评论 0原文

我正在使用 Ruby 1.8.7 的 RSS::Parser,它是 stdlib 的一部分。我是红宝石新手。

我想解析 RSS 提要,对数据进行一些更改,然后将其输出(作为 RSS)。

文档说我可以使用“#to_s”,但是它似乎适用于某些提要,但不适用于其他提要。

这有效:

#!/usr/bin/ruby -w

require 'rss'
require 'net/http'

url = 'http://news.ycombinator.com/rss'
feed = Net::HTTP.get_response(URI.parse(url)).body

rss = RSS::Parser.parse(feed, false, true)

# Here I would make some changes to the RSS, but right now I'm not.

p rss.to_s

返回预期输出:XML 文本。

失败:

#!/usr/bin/ruby -w

require 'rss'
require 'net/http'

url = 'http://feeds.feedburner.com/devourfeed'
feed = Net::HTTP.get_response(URI.parse(url)).body

rss = RSS::Parser.parse(feed, false, true)

# Here I would make some changes to the RSS, but right now I'm not.

p rss.to_s

不返回任何内容(空引号)。

然而,如果我将最后一行更改为:

p rss

我可以看到该对象填充了所有提要数据。这是 to_s 方法失败了。

  1. 为什么?
  2. 如何获得某种错误输出来调试这样的问题?

I'm using Ruby 1.8.7's RSS::Parser, part of stdlib. I'm new to Ruby.

I want to parse an RSS feed, make some changes to the data, then output it (as RSS).

The docs say I can use '#to_s', but and it seems to work with some feeds, but not others.

This works:

#!/usr/bin/ruby -w

require 'rss'
require 'net/http'

url = 'http://news.ycombinator.com/rss'
feed = Net::HTTP.get_response(URI.parse(url)).body

rss = RSS::Parser.parse(feed, false, true)

# Here I would make some changes to the RSS, but right now I'm not.

p rss.to_s

Returns expected output: XML text.

This fails:

#!/usr/bin/ruby -w

require 'rss'
require 'net/http'

url = 'http://feeds.feedburner.com/devourfeed'
feed = Net::HTTP.get_response(URI.parse(url)).body

rss = RSS::Parser.parse(feed, false, true)

# Here I would make some changes to the RSS, but right now I'm not.

p rss.to_s

Returns nothing (empty quotes).

And yet, if I change the last line to:

p rss

I can see that the object is filled with all of the feed data. It's the to_s method that fails.

  1. Why?
  2. How can I get some kind of error output to debug a problem like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2024-12-07 17:44:07

据我所知,问题不在 to_s 中,而是在解析器本身中。单步执行 parser.rb 代码显示没有返回任何内容,因此 to_s 返回空字符串是有效的。

我建议查看类似 Feedzirra 的内容。

另外,仅供参考,请查看 Ruby 的 Open::URI< /code>模块,用于轻松检索网络资产,例如提要。 Open-URI 很简单,但足以满足大多数任务。 Net::HTTP 是较低级别的,这将需要您键入更多代码来替换 Open-URI 的功能。

From what I can tell, the problem isn't in to_s, it's in the parser itself. Stepping way into the parser.rb code showed nothing being returned, so to_s returning an empty string is valid.

I'd recommend looking at something like Feedzirra.

Also, as a FYI, take a look at Ruby's Open::URI module for easy retrieval of web assets, like feeds. Open-URI is simple but adequate for most tasks. Net::HTTP is lower level, which will require you to type a lot more code to replace the functionality of Open-URI.

丢了幸福的猪 2024-12-07 17:44:07

我也遇到了同样的问题,所以我开始调试代码。我认为 ruby​​ rss 有太多必需的元素。频道需要有“标题、链接、描述”,缺少其中一项就会失败。

上面示例中的第二个提要缺少描述,这将使 to_s 失败...

我相信这是一个错误,但我真的不理解代码,几乎不懂 ruby​​,所以谁知道呢。对我来说,即使缺少某些元素,to_s 也会尽力而为,这似乎很自然。

无论哪种方式

rss.channel.description="something"
rss.to_s

将“工作”

问题在于 def have_required_elements?

或者在
self.class::模型

I had the same problem, so I started debugging the code. I think the ruby rss has a few too many required elements. The channel need to have "title, link, description", if one is missing to_s will fail.

The second feed in the example above is missing the description, which will make the to_s fail...

I believe this is a bug, but I really don't understand the code and barely ruby so who knows. It would seem natural to me that to_s would try its best even if some elements are missing.

Either way

rss.channel.description="something"
rss.to_s

will "work"

The problem lies in def have_required_elements?

Or in the
self.class::MODELS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文