Question

I'm really new to Ruby (first day!) and I'm struggling with this here. I'm trying to build a rss-parser in combination with Typhoeus since I'm parsing over 100 feeds and also because I want to get it working.

So that is my code here:

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

I'm sure it's some syntax problem. I still have some hard times. For example I always keep closing lines with ; although I forget it all the time when writing PHP. So, maybe someone could help? Getting feed-results without typhoeus wasn't too hard.

edit:

>> 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="....

So, I at least get something.

Was it helpful?

Solution

It seems that you are queing inside the on_complete block. Shouldn't you queue in the feed_urls.each block? Or maybe you should go through all the entries after all requests have been completed? Like this:

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

OTHER TIPS

You're missing an end at the end of your block. If you indent your code consistently, you will see the hole:

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

Welcome to Ruby and Stack Overflow! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top