红宝石,Feedzirra,typhoeus

时间:2011-06-10 23:07:49

标签: ruby feedzirra typhoeus

我是Ruby的新手(第一天!),我在这里苦苦挣扎。我正在尝试与Typhoeus一起构建一个rss-parser,因为我正在解析100多个feed,也因为我想让它工作。

这是我的代码:

require 'typhoeus'
require 'feedzirra'

feed_urls = ["feed1", "feed2"]

hydra = Typhoeus::Hydra.new
feeds = {}
entry = {}
feed_urls.each do |feed|
  r = Typhoeus::Request.new(feed)
  r.on_complete do |response|
    feeds[r.url] = response.body
    feeds[r.url] = Feedzirra::Feed.parse(response.body)
    entry = feeds.entries.each do |entry|
      puts entry.title
    end
    hydra.queue r
  end
end

hydra.run

我确定这是一些语法问题。我还有一些困难时期。例如,我始终使用;来关闭行,尽管我在编写PHP时一直忘记它。那么,也许有人可以提供帮助?没有伤寒的饲料结果并不太难。

编辑:

>> puts feeds.entries.inspect
[["http://feedurl", #<Feedzirra::Parser::AtomFeedBurner:0x1023b53f0 @title="Paul Dix Explains Nothing", @entries=[#<Feedzirra::Parser::AtomFeedBurnerEntry:0x1023b05d0 @published=Thu Jan 13 16:59:00 UTC 2011, @author="Paul Dix", @summary="Earlier this week I had the opportunity to sit with six other people from the NYC technology scene and talk to NYC Council Speaker Christine Quinn and a few members of her staff. Charlie O'Donnell organized the event to help...", @updated=Thu Jan 13 17:55:31 UTC 2011, @title="Water water everywhere and not a drop to drink: The Myth and Truth of the NYC engineer shortage", @entry_id="tag:typepad.com,2003:post-6a00d8341f4a0d53ef0148c793f692970c", @content="....

所以,我至少得到了一些东西。

2 个答案:

答案 0 :(得分:2)

好像你在on_complete块内排队。你不应该在feed_urls.each块中排队吗?或者也许您应该在所有请求完成后查看所有条目?像这样:

hydra = Typhoeus::Hydra.new
feeds = {}
entry = {}
feed_urls = ["feed1", "feed2"]

feed_urls.each do |feed|
  r = Typhoeus::Request.new(feed)
  r.on_complete do |response|
      feeds[r.url] = response.body
      feeds[r.url] = Feedzirra::Feed.parse(response.body)
  end

  hydra.queue r
end

hydra.run

feeds.entries.each do |feed|
  puts "-- " + feed[1].title

  feed[1].entries.each do |entry|
    puts entry.title
  end
end

答案 1 :(得分:0)

你在街区尽头错过end。如果您一致地缩进代码,您将看到漏洞:

require 'typhoeus'
require 'feedzirra'
feed_urls = ["feed1", "feed2"]    
hydra = Typhoeus::Hydra.new
feeds = {}
entry = {}
feed_urls.each do |feed|
  r = Typhoeus::Request.new(feed)
  r.on_complete do |response|
    feeds[r.url] = response.body
    feeds[r.url] = Feedzirra::Feed.parse(response.body)
    entry = feeds.entries.each do |entry|
      puts entry.title
    end
    hydra.queue r
  end

### You need an 'end' on this line to close the `each` ###

hydra.run

欢迎使用Ruby和Stack Overflow! :)