Ruby - Feedzirra and updates

Posted by mplacona on Stack Overflow See other posts from Stack Overflow or by mplacona
Published on 2010-03-20T00:06:23Z Indexed on 2010/03/20 0:11 UTC
Read the original article Hit count: 690

Filed under:
|
|

Hi, trying to get my head around Feedzirra here.

I have it all setup and everything, and can even get results and updates, but something odd is going on.

I came up with the following code:

  def initialize(feed_url)
    @feed_url = feed_url
    @rssObject =  Feedzirra::Feed.fetch_and_parse(@feed_url)
  end

  def update_from_feed_continuously()    
    @rssObject = Feedzirra::Feed.update(@rssObject)
    if @rssObject.updated?
      puts @rssObject.new_entries.count
    else
      puts "nil"
    end
  end

Right, what I'm doing above, is starting with the big feed, and then only getting updates. I'm sure I must be doing something stupid, as even though I'm able to get the updates, and store them on the same instance variable, after the first time, I'm never able to get those again.

Obviously this happens because I'm overwriting my instance variable with only updates, and lose the full feed object.

I then thought about changing my code to this:

  def update_from_feed_continuously()    
    feed = Feedzirra::Feed.update(@rssObject)
    if feed.updated?
      puts feed.new_entries.count
    else
      puts "nil"
    end
  end

Well, I'm not overwriting anything and that should be the way to go right?

WRONG, this means I'm doomed to always try to get updates to the same static feed object, as although I get the updates on a variable, I'm never actually updating my "static feed object", and newly added items will be appended to my "feed.new_entries" as they in theory are new.

I'm sure I;m missing a step here, but I'd really appreciate if someone could shed me a light on it. I've been going through this code for hours, and can't get to grips with it.

Obviously it should work fine, if I did something like:

if feed.updated?
  puts feed.new_entries.count
  @rssObject = initialize(@feed_url)
else

Because that would reinitialize my instance variable with a brand new feed object, and the updates would come again.

But that also means that any new update added on that exact moment would be lost, as well as massive overkill, as I'd have to load the thing again.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about feedparser