首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >红宝石- Feedzirra和更新

红宝石- Feedzirra和更新
EN

Stack Overflow用户
提问于 2010-03-20 00:06:23
回答 3查看 2.3K关注 0票数 1

想让我的头在Feedzirra身边。

我有所有的设置和一切,甚至可以得到结果和更新,但有些奇怪的事情正在进行。

我想出了以下代码:

代码语言:javascript
复制
  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

是的,我上面所做的,是从大提要开始,然后只得到更新。我确信我一定是在做一些愚蠢的事情,就好像我能够获得更新,并将它们存储在同一个实例变量上,在第一次之后,我再也不能得到这些更新了。

很明显,这是因为我只使用更新覆盖了实例变量,并且丢失了完整的feed对象。

然后我考虑将我的代码更改为:

代码语言:javascript
复制
  def update_from_feed_continuously()    
    feed = Feedzirra::Feed.update(@rssObject)
    if feed.updated?
      puts feed.new_entries.count
    else
      puts "nil"
    end
  end

嗯,我没有覆盖任何东西,这应该是正确的方法,对吧?

错误,这意味着我注定总是试图获得对同一个静态提要对象的更新,因为尽管我在变量上获得了更新,但我从未真正更新“静态提要对象”,新添加的项将被追加到我的"feed.new_entries“中,因为它们理论上是新的。

我肯定我错过了一步,但如果有人能给我一点帮助,我会非常感激的。我已经看了好几个小时的代码了,无法理解它。

很明显,如果我做了这样的事情,它应该会很好:

代码语言:javascript
复制
if feed.updated?
  puts feed.new_entries.count
  @rssObject = initialize(@feed_url)
else

因为这将使用一个全新的feed对象重新初始化我的实例变量,所以更新将再次出现。

但这也意味着,在这一时刻添加的任何新更新都将丢失,也意味着会过度使用,因为我不得不再次加载它。

提前感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-03-21 16:42:29

对于当前的API来说,如何进行更新有点违背直觉。这个例子展示了最好的方法:

代码语言:javascript
复制
# I'm using Atom here, but it could be anything. You don't need to know ahead of time.
# It will parse out to the correct format when it updates.
feed_to_update = Feedzirra::Parser::Atom.new
feed_to_update.feed_url = some_stored_feed_url
feed_to_update.etag = some_stored_feed_etag
feed_to_update.last_modified = some_stored_feed_last_modified

last_entry = Feedzirra::Parser::AtomEntry.new
last_entry.url = the_url_of_the_last_entry_for_a_feed

feed_to_update.entries = [last_entry]

updated_feed = Feedzirra::Feed.update(feed_to_update)

updated_feed.updated? # => nil if there is nothing new
updated_feed.new_entries # => [] if nothing new otherwise a collection of feedzirra entries
updated_feed.etag # => same as before if nothing new. although could change with comments added to entries.
updated_feed.last_modified # => same as before if nothing new. although could change with comments added to entries.

基本上,您必须保存四条数据(feed_url、last_modified、etag和最新条目的url )。然后,当您想要进行更新时,您将构造一个新的feed对象并调用更新。

票数 6
EN

Stack Overflow用户

发布于 2011-05-20 19:36:45

我认为更明显的解决方案是将:if_modified_since选项添加到类Feedfetch_and_parse方法中,参见https://github.com/pauldix/feedzirra/blob/master/lib/feedzirra/feed.rb#L116https://github.com/pauldix/feedzirra/blob/master/lib/feedzirra/feed.rb#L206

票数 4
EN

Stack Overflow用户

发布于 2010-03-21 11:54:37

您可以将@rssObject重置为更新的提要。

代码语言:javascript
复制
feed = Feedzirra::Feed.update(@rssObject)
if feed.updated?
  puts feed.new_entries.count
  @rssObject = feed
else
  puts 'nil'
end

随着新条目的发现,@rssObject中的条目数量将不断增加。因此,如果第一个fetch找到10个条目,然后再查找10个新条目,那么@rssObject.entries.size将是20。

请注意,无论update是否找到新条目,都可以这样做。如果feed.updated?为false,则feed将是原始提要对象@rssObject

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2481285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档