Hey, it's me, josh - as a memoji


Parsing an RSS Feed in Ruby on Rails

Josh Echeverri
Josh Echeverri

Parsing an RSS Feed in Ruby on Rails

And Displaying the Resulting Data

yea that’s rails’ new logo

Earlier this week I was tasked to parse some data from an RSS feed with Ruby. As soon as I start googling I get a bunch of stuff about “simple” ways with gem’s like “simple-rss” or “feedjira.” Both have great documentation, but Ruby is already easy enough sometimes. SOMETIMES. Ruby already has the ability to parse an RSS feed if you require rss and open-uri.

Here’s an example what the method in my controller looks like that parses XML data from the CoinDesk.com newsfeed:

def news
  require ‘rss’
  require ‘open-uri’
  rss\_results = \[\]
  rss = RSS::Parser.parse(open(['http://feeds.feedburner.com/CoinDesk?format=xml').read](http://feeds.feedburner.com/CoinDesk?format=xml%27%29.read), false).items\[0..5\]

  rss.each do |result|
    result = { title: result.title, date: result.pubDate, link: result.link, description: result.description }
    return rss\_results

I’m fetching the data for the newest 6 top articles. Then I iterate through the results to fetch the data I actually need to use. After that I push it all into an empty array called `rss_results`. When I look at the data dump this is what I got so I knew what to look for:

thats a lot of data…

All I need to work with here is just the title, date, link, and description. (Note: You can see I may need to split the description data since it has an image, too, but I’ll do that later.)

The way I’m going to access those specific parts of the dataset is by iterating through the array in my view like this:

<%= [@news](http://twitter.com/news "Twitter profile for @news").each do |article| %>
  <div class=”col-md-4 article-padding”>
  <a href=”<%= article\[:link\] %>”><p><strong><%= article\[:title\] %></strong></p></a>
  <small><p style=”margin-top:-10px”> <%= article\[:date\].strftime(“Published: %F”) %> | <a href=”<%= article\[:link\] %>”><em>Source: CoinDesk</em></a></a></p></small>
  <%= article\[:description\] %>
<% end %>

Pretty straight forward stuff, right? Now I have a clean set of 6 news items in my Rails app.

still have to split that image with regex

All that’s left is figuring out how/if I’m going to use that image in the description. I’ll split it later. It’s midnight on a Friday, and I lost track of time. :)

UPDATE EDIT: It just came to my attention that I wrote this post on the 1 year anniversary of me writing another post. Time flies when you’re having fun learning:

I Know What I Want To Do _How I Figured Out I Wanted To Be A Software Engineer_medium.com

Hello@josh.miami— Follow me on Twitter, Instagram, and LinkedIn to discuss the finer things in life like Beagles, Bitcoin, and Beer. 🌴🍺🐶

By Josh Echeverri on March 26, 2016.

Canonical link

Exported from Medium on February 21, 2022.