Chris Illusion
Chris Illusion

Reputation: 287

Ruby on Rails 4 Parsing first 3 RSS items

I'm currently making a website in RoR 4 and I want to include RSS from my blog on the front page. However, currently I get all of the articles from the blog while I need only first 3.

I'm fairly new to rails and I couldn't find any pointers to how I could do such thing.

My current code in the controller is:

  def index
    require 'rss'
        @rss = RSS::Parser.parse(open('FEED_URL').read, false)
  end

I imagine I could simply cut the @rss after the third element after it has been parsed, but that seems to me a bit dirty in Ruby. Is there a better way to do it?

Thank you very much!

Upvotes: 2

Views: 3274

Answers (2)

mahemoff
mahemoff

Reputation: 46389

Feedjira is a fantastic RSS parser. Just do:

Feedjira::Feed.fetch_and_parse('FEED_URL').entries[0,3]

Upvotes: 2

Looking here, I'd take a gander and try...

@rss = RSS::Parser.parse(open('FEED_URL').read, false).items[0..2]

Upvotes: 8

Related Questions