0

I'm playing about with the Ruby Feedzirra gem and have managed to acheive what I set out to do by using it in the controller.

Though everything I've seen has mentioned using it in the model. I was just wondering if this is ok to do in the controller, if not, how might I go about achieving the same in the model?

I want to submit a Feed URL and use that to update my model with the rest of the information about the feed.

Feeds_controller.rb

def create
  @feed = Feed.new(params[:feed])

  feed = Feedzirra::Feed.fetch_and_parse(@feed.feed_url)
  @feed.title         = feed.title
  @feed.url           = feed.url
  @feed.last_modified = feed.last_modified

  respond_to do |format|
    if @feed.save
      format.html { redirect_to @feed, notice: 'Feed was successfully created.' }
      format.json { render json: @feed, status: :created, location: @feed }
    else
      format.html { render action: "new" }
      format.json { render json: @feed.errors, status: :unprocessable_entity }
    end
  end
end

feed.rb

class Feed < ActiveRecord::Base
  attr_accessible :feed_url, :last_modified, :title, :url
end
cupcakekid
  • 233
  • 3
  • 13

2 Answers2

1

I'd make an instance method on the Model and use that in the controller so

class Feed < ActiveRecord::Base
   def fetch!
     feed = Feedzirra::Feed.fetch_and_parse(feed_url) # probably want some eror handling here
     title = feed.title
     url = feed.url
     last_modified = feed.last_modified
     self #or nil if you like
   end
end

then your controller thins down to

def create
  @feed = Feed.new(params[:feed])
  @feed.fetch!

  respond_to do |format|
    if @feed.save
      format.html { redirect_to @feed, notice: 'Feed was successfully created.' }
      format.json { render json: @feed, status: :created, location: @feed }
    else
      format.html { render action: "new" }
      format.json { render json: @feed.errors, status: :unprocessable_entity }
    end
  end
end

The cool thing here is that if this gets too slow you can use something like Resque to run this in the background and just return a "your request is being processed" message to the user, then alert them asynchronously when the the request is done (might not work so well for the json request)

concept47
  • 30,257
  • 12
  • 52
  • 74
  • Thanks @Concept47, appreciate it. Will give it a go. All makes sense. Though I'm a bit unsure about 'self' and what its purpose is at the end of that method. What's it there for? – cupcakekid May 25 '13 at 10:27
  • I wanted it to return something just in case you would want to check if the fetch was successful – concept47 May 25 '13 at 19:05
  • Thanks. And the '!' at the end of the method. What does that do? (just trying to work out what's rails magic and what's not) – cupcakekid May 25 '13 at 23:17
  • Ahh right, that's just a way of letting anyone who sees it know that it's a self modifying Method http://stackoverflow.com/questions/612189/why-are-exclamation-marks-used-in-ruby-methods – concept47 May 26 '13 at 23:25
0

You really don't want to use something like a RSS parser in your controller. That will slow the responsiveness of that URL and your server.

Instead, run the RSS parser in a separate application, or at least a separate thread, and store the retrieved feeds in a database table for rapid access.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303