简体   繁体   English

Rails多个RSS Feed解析

[英]Rails Multiple RSS Feed Parsing

I'm new to rails and need to figure out how to do something. 我是Rails的新手,需要弄清楚该怎么做。 I'm using the Feedzirra gem and have a method in the FeedEntry model called update_from_feed. 我正在使用Feedzirra宝石,并且FeedEntry模型中有一个称为update_from_feed的方法。 This currently works using rails console. 目前,这可以在Rails控制台上使用。

How can I pass in all the rss feed urls from the feeds table so I can parse all of the rss feeds and save it to the feed_entries table? 如何传递提要表中的所有rss提要URL,以便我可以解析所有rss提要并将其保存到feed_entries表中? I imagine I need to take the urls from the feeds table and somehow make an array of feed urls, loop through it and then pass the array into the update_from_feed method. 我想我需要从feeds表中获取url,并以某种方式制作一个feed url数组,循环遍历它,然后将该数组传递给update_from_feed方法。 I'm not sure what to do. 我不确定该怎么办。 Here is the current FeedEntry model: 这是当前的FeedEntry模型:

class FeedEntry < ActiveRecord::Base
  attr_accessible :guid, :name, :published_at, :summary, :url

  belongs_to :feed

  def self.update_from_feed(feed_url)
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    add_entries(feed.entries)
  end

  private

  def self.add_entries(entries)
    entries.each do |entry|
      unless exists? :guid => entry.id
        create!(
          :name => entry.title,
          :summary => entry.summary,
          :url => entry.url,
          :published_at => entry.published,
          :guid => entry.id
        )
      end
    end
  end
end

You can do it by adding the following method to your Feed model: 您可以通过在Feed模型中添加以下方法来做到这一点:

class Feed < ActiveRecord::Base

  def self.update_feeds
    all.each do |feed|
      FeedEntry.update_from_feed(feed.url)
    end
  end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM