views:

36

answers:

0

Background: I am playing around with the feedzirra plugin in a rails app, and I am attempting to utilize both the feed and entry accessors of the parsed feed in a single view.

To accomplish this, I have created two models: feed and feed_entry. They are as follows:

feed.rb:

class Feed < ActiveRecord::Base
  attr_accessible :title, :url, :feed_url, :last_modified, :guid
  has_many :feed_entries

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


  private

  def self.add_feed(feed)
    unless exists? :guid => feed.id
      create!(
        :title          => feed.title,
        :url            => feed.url,
        :feed_url       => feed.feed_url,
        :last_modified  => feed.last_modified,
        :guid           => feed.id
      )
    end
  end
end

feed_entry.rb:

class FeedEntry < ActiveRecord::Base
  attr_accessible :title, :url, :author, :summary, :content, :published, :guid
  belongs_to :feed

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

Where the intention is to call Feed.update_from_feed(feed_url) as a cron job. In reality, I'm hoping to have the cron job call this function by passing in multiple feeds; but I figured I'd get one working first. For sake of testing, I'm currently calling this function from the console.

Questions:

NoMethodError: undefined method `id' for #<Feedzirra::Parser::RSS:0x00000100bcb0f0>
  1. This may seem very simple, but in calling the update_from_feed function, I get the above error in my console. While new to ruby and rails, I was under the impression that every new entry in a table is given an id that can be referenced - in this scenario, by feed.id. Am I missing something here?

  2. Is my 2-model approach to using this plugin a good direction to go in? Since I'm not able to verify the rest of my approach works until I puzzle out question 1, I thought I'd ask for a tip on best practice in the mean time.

  3. The rest of my plan is to utilize the feed and entry accessors of the plugin within a single controller, and then reference those variables within the relative view somehow. Is this how I should be perceiving the MVC architecture and using it in a RESTful way? If so, how do I reference the feed and entry accessors in the FeedReader view?

This is what the FeedReader controller would look like:

class FeedEntriesController < ApplicationController
  def index
    @feed_entries = FeedEntry.all
    @feeds = Feed.all
  end
end

I know this was a bitch of a post, but any help would really be appreciated.