Videos | Podcasts

Consuming RSS Feeds Using IronRuby with WinForms Application
AzamSharp
Published Date: 6/27/2009 10:28:33 AM
Views: 1355

Abstract:
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:


$gvFeeds = DataGridView.new
$browser = WebBrowser.new
$browser.width = 400

  def create_rss_reader_form()


   $browser.Location = Point.new(500,100)

    getFeedButton = Button.new
    getFeedButton.Text = 'Get Feed'
    getFeedButton.Location = Point.new(100,60)
    getFeedButton.click do |sender,args|

      get_rss_feed()

    end

    buttonColumn = DataGridViewButtonColumn.new
    buttonColumn.Text = "View Details"

    $gvFeeds.Location = Point.new(100,100)
    $gvFeeds.Width = 300
    $gvFeeds.Height = 300   

    
    $gvFeeds.Columns.AddRange(buttonColumn)
    
    form = Form.new
    form.Width = 800
    form.Height = 600
    form.Controls.Add getFeedButton
    form.Controls.Add $gvFeeds
    form.Controls.Add $browser

    form.ShowDialog()

  end


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.

http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=1483

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:


require 'rexml/document'
require 'open-uri'

include REXML


Here is the implementation of the get_rss_feed method:


def get_rss_feed()

  rss_feed = 'http://feeds.feedburner.com/Azamsharp'
  xml  =   Document.new(open(rss_feed).read())
  root = xml.root  

  # go through the collection and prints the title  
  feeds = Array.new

  root.elements.each("channel/item") do |item|

    feed = RssFeed.new()
      
    feed.title = item.text("title")
    feed.link = item.text("link")

    feeds.push(feed)
    
  end


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):

C:\ironruby\ironruby\Merlin\Main\bin\Debug

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.


 rss_feed = 'http://feeds.feedburner.com/Azamsharp'
  xml  =   Document.new(open(rss_feed).read())
  root = xml.root  

  # go through the collection and prints the title  
  feeds = Array.new

  root.elements.each("channel/item") do |item|

    feed = RssFeed.new()
      
    feed.title = item.text("title")
    feed.link = item.text("link")

    feeds.push(feed)





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:


 $gvFeeds.DataSource =feeds


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.


$gvFeeds.CellDoubleClick do |sender,e|

      if e.ColumnIndex == 0
        row = $gvFeeds.Rows[e.RowIndex]

        url = row.Cells[2].Value  # accessing the url column

        $browser.Navigate(System::Uri.new(url))

      end
    end


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.



Conclusion:

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.

[Download Sample]

[Hello IronRuby Podcast]



Did you like this article?
kick it on DotNetKicks.com Submit
Similar Articles

Behavior Driven Development Using Cucumber and IronRuby

Unit Testing CLR Assembly Using IronRuby with Spec

IronRuby with WinForms and ADO.NET Data Access

First Look at the IronRuby

Enter Comment/Feedback

 
 
 
 
 

Comments/Feedbacks