Consuming RSS Feeds Using IronRuby with WinForms Application
In this article we are going to demonstrate how to use IronRuby to consume RSS feeds using a WinForms project. The user interface used for this article is very basic thus we encourage developers to design an attractive UI for their application.
Creating the User Interface for the WinForms Application:
Let's start by creating a WinForms application. Our application will contain the basic DataGridView control which will be used to display the title of the RSS feeds. Here is the implementation:
The DataGridView and WebBrowser controls are made global so they can be accessed from anywhere in the application. The rest of the implementation simply create different controls and adds them to the Form controls collection. The application when run looks similar to the screenshot below:
The get_rss_feed function is fired whenever the Button is clicked. Let's implement the get_rss_feed function.
Implementing the get_rss_feed Method:
The get_rss_feed method is responsible for communicating with the feed source and pulling out the information about RSS feeds. In Ruby language there is a library called rss which is used to perform the RSS functions. This library is not yet implemented in IronRuby. Currently, there is a voting going on which will decide the fate of the implementation of the rss library in IronRuby. You can vist the link below to cast your vote.
We have used "rexml/document" module to read the RSS from the source. First, you must include the namespace in your project as shown below:
Here is the implementation of the get_rss_feed method:
When you run the code above you might get an error saying "unable to locate open-uri". You will be tempted to search your harddrive for "open-uri" library and manually copy the library in your project. Unfortunately, you will not solve the problem this way since "open-uri" is dependent on "uri" and "uri" is dependent on "stringio" and so on. An easier way is to just add the path in Envirnment Variables to the ir.exe (XML configuration file) which already contains the correct paths for all the libraries.
You will find the path for ir.exe in the following folder (depending on your installation directory):
If you open ir.exe you will find the following paths:
Finally, here is the path that you will add in your windows environment variables:
Now, you will be able to access the IronRuby libraries from your application since they are included in the windows environment variables path.
Let's go back and discuss the implementation of get_rss_feeds method in detail.
We have a custom feed class which contains the title and link property. The Document.New(xml structure) creates an XML document which is based on the structure supplied by the feed URL. We used the root.elemenets.each("channel/item") method to iterate through the feeds and inserted them in the feeds collection which is an array.
Finally, we bind the DataGridView with the feeds collection as shown below:
Now if you run the application again you will see the following result:
As, you can see in the above screenshot the feeds are displayed in the DataGridView control. Our next task is to display the page in the web browser control.
Showing feeds in the WebBrowser control:
We want that when we click a button in the DataGridView control the selected feed is displayed in the browser control. For that we must first implement the button click event for a button that resides inside the DataGridView control.
In the above code we have implemented the CellDoubleClick click event of DataGridView control. Our basic task is to retrieve the URL from the DataGridView control and then use the WebBrowser control to navigate to that URL.
The screenshot below shows when we double click on the DataGridView control the WebBrowser control is updated with the contents of the selected URL.
In this article we learn how to display RSS feeds in a data grid view control using IronRuby. We also learned that it is a much better idea to set your environment path to the ir.exe file instead of manually adding the libraries to your project. In the next article we will learn how to get started with web development using IronRuby with the ASP.NET MVC framework.