Building a News Aggregator with Rails: RSS Feeds and Articles
Introduction
In this article, we will explore how to build a news aggregator using the Ruby on Rails framework. News aggregators are web applications that gather news articles from various sources and present them to users in one centralized location. We will focus on incorporating RSS feeds and managing the articles within our Rails application.
Setting up the Project
To get started, make sure you have Rails installed on your machine. Open your terminal and create a new Rails application using the command:
rails new news_aggregator
This will generate a new Rails application with the name “news_aggregator”. Navigate into the project directory:
cd news_aggregator
Adding RSS Feeds
RSS (Really Simple Syndication) feeds allow us to fetch news articles from different sources. Let’s add a model and a controller to manage our feeds. In the terminal, run the following commands:
rails generate model Feed title:string url:string
This will generate a feed model with “title” and “url” attributes. Next, generate the associated controller:
rails generate controller Feeds
Open the generated app/controllers/feeds_controller.rb
file and implement the necessary actions (e.g., create, index, show, delete) to manage the feeds.
Fetching Articles
To fetch articles from the feeds, we can use a library like Feedjira. Add it to your Gemfile:
gem 'feedjira'
Run bundle install
to install the gem. Now, let’s create an Article model with the necessary attributes:
rails generate model Article title:string summary:text url:string published_at:datetime feed:references
Generate the Articles controller:
rails generate controller Articles
In the app/controllers/articles_controller.rb
file, define the actions to manage articles associated with a feed (e.g., index, show).
Parsing RSS feeds
Now, let’s add functionality to fetch and parse RSS feeds. In the feeds controller, create a method to fetch the feed data using Feedjira. Then, iterate over the fetched feed entries and create corresponding Article records:
def fetch_feed
feed = Feedjira::Feed.fetch_and_parse(params[:url])
if feed.present?
feed.entries.each do |entry|
Article.create(
title: entry.title,
summary: entry.summary,
url: entry.url,
published_at: entry.published,
feed_id: params[:id]
)
end
end
redirect_to feed_path(params[:id])
end
Displaying Articles
Now, let’s create views to display the feeds and their associated articles. In the app/views/feeds/index.html.erb
file, iterate over the feeds and display their titles along with a link to the corresponding show page.
<% @feeds.each do |feed| %>
<%= link_to feed.title, feed_path(feed) %>
<% end %>
In the app/views/feeds/show.html.erb
file, display the articles associated with the selected feed:
<% @articles.each do |article| %>
<%= link_to article.title, article.url %>
<%= truncate(article.summary, length: 150) %>
<%= article.published_at %>
<% end %>
Conclusion
Now you have a basic news aggregator application that fetches and displays news articles from RSS feeds. You can further enhance this application by adding features like user authentication, favoriting articles, and filtering articles by categories or tags. The possibilities are endless! Enjoy building your own personalized news aggregator with Rails.
Leave a Reply