简体   繁体   中英

Rails Multiple RSS Feed Parsing

I'm new to rails and need to figure out how to do something. I'm using the Feedzirra gem and have a method in the FeedEntry model called update_from_feed. This currently works using rails console.

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? 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. I'm not sure what to do. Here is the current FeedEntry model:

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:

class Feed < ActiveRecord::Base

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

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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