2010-03-20 154 views
1

試圖讓我的腦袋周圍Feedzirra here。紅寶石 - Feedzirra和更新

我擁有它所有的設置和一切,甚至可以得到結果和更新,但有些奇怪的事情正在發生。

我想出了下面的代碼:

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 

右,我在做什麼上面,開始與大源,然後只得到更新。我確信我必須做一些愚蠢的事情,因爲即使我能夠獲得更新,並將它們存儲在同一個實例變量中,但在第一次之後,我再也無法獲得這些更新了。

很明顯,這是因爲我只用更新覆蓋了我的實例變量,而失去了完整的提要對象。

我當時以爲要改變我的代碼如下:

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

嗯,我不是任何覆蓋,並且應該去正確的方式?

WRONG,這意味着我註定要一直試圖獲得更新到相同的靜態飼料對象,但我得到一個變量的更新,我從來沒有真正更新我的「靜態飼料對象」,新添加的項目將被添加到我的「feed.new_entries」,因爲它們理論上是新的。

我敢肯定,我在這裏錯過了一步,但是如果有人能讓我看清楚,我會非常感激。我已經經歷了幾個小時的這段代碼,並且無法掌握它。

顯然,這應該正常工作,如果我不喜歡的東西:

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

,因爲這將重新初始化我的實例變量以嶄新的飼料對象,並相應更新會再來。

但是這也意味着在那個時刻添加的任何新更新都將丟失,以及大規模的過度衝擊,因爲我不得不再次加載該東西。

在此先感謝!

+0

多久你重試你的飼料?如果你第一次得到它,那麼稍後再看,如果在那個時候飼料沒有更新,你可能看不到變化,讓我覺得你看到了正確的行爲。 還有一些需要考慮的問題是將信息保存在內存中並重用變量的潛在問題。如果您的代碼死亡,您將失去該狀態,並且必須重新加載您完整跟蹤的所有Feed。如果您要跟蹤1000個Feed,這可能會非常昂貴。對於單個Feed而言,這不是什麼大問題,但對於大量數據,您需要一個用於跟蹤狀態的數據庫。 – 2010-03-21 00:14:54

回答

6

如何做更新與當前的API有點不直觀。這個例子顯示了這樣做的最佳方式:

# 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,以及最近進入的網址)。然後當你想要做更新 構建一個新的feed對象並致電更新 那。

+0

你可以保存updated_feed編組在你的分貝,而不是每次重建它? – Nader 2010-10-07 21:24:34

0

您可以將@rssObject重置爲已更新的Feed。

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

@rssObject中的條目數將隨着新條目的增加而持續增長。因此,如果第一次提取找到10個條目,然後接下來找到10個新條目,則​​將爲20.

請注意,無論update是否找到新條目,都可以執行此操作。如果feed.updated?爲false,則feed將爲原始提要對象@rssObject