Ruby for Java Programmers, Part II

Part I is here.

As promised, I will now try to invoke some more complex Java classes from Ruby code. Since I might need this for a real project and one of the things I find missing in Ruby is an RSS/Atom parser that offers the same features as Rome, I figured I might just try fetch and parse an RSS feed using Rome via Ruby.

Even something simple as fetching a single feed using Rome involves a handful of classes and methods. If all you need is getting at feed entries from your Ruby code, you’d be much better of if you encapsulate API calls in some higher-level class that is intended to be called from Ruby. This will avoid you the hassle of importing more than one class and calling overridden methods for which you’ll have to supply signatures and such trivia. This is just what I did in Fetcher.java:

import java.net.URL;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;

public class Fetcher {
	public static SyndFeed fetch(String url) throws Exception {
		URL feedUrl = new URL(url);
		FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
		FeedFetcher fetcher = new HttpURLFeedFetcher(feedInfoCache);
		SyndFeed feed = fetcher.retrieveFeed(feedUrl);
		return feed;
	}
}

Calling this class from Ruby is straightforward enough:

require 'rjb'

Rjb::load('.:rome-0.7.jar:rome-fetcher-0.7.jar:jdom.jar:jdom.jar', [])

fetcher = Rjb::import('Fetcher')
feed = fetcher.fetch(ARGV[0])
p feed.getTitle
entries = feed.getEntries.iterator
while entries.hasNext do
    entry = entries.next
    p "#{entry.getPublishedDate.toString} #{entry.getTitle}"
end

Two things to note here. First, it would be great if the Ruby-Java bridge would pick up JavaBeans conventions and allow you to simply write entry.title instead of entry.getTitle.

Second, I will probably need to convert Java dates to Ruby Time objects by formatting as a string and then parsing it back. It would also be useful if the Ruby-Java bridge could provide more automatic type conversions, where such convertions are possible, besides simple strings.

6 Responses to “Ruby for Java Programmers, Part II”


  1. 1 Carfield Yim

    I support you don’t need to write while loop but able to use Ruby closure, right?

    Or rjb require you not to use closure?

  2. 2 ugo

    What you get from feed.getEntries is a java.util.Collection. If you’re asking whether it wouls be possible to just do “feed.getEntries.each do …”, well the answer is no.

  3. 3 Patrick Chanezon

    Very cool Ugo: I’ve always considered the few ruby libraries I found for parsing feeds pretty lame, but I’m too lazy to port ROME to ruby.
    Going native is a good idea.

    PS: we released ROME 0.8 a few minutes ago, we now support Atom 1.0.
    cf http://blog.chanezon.com/articles/2006/02/01/rome-0-8-released-now-support-atom-1-0-rfc-4287

  1. 1 Dave Brondsema's Blog
  2. 2 Agylen » Ruby for Java Programmers, Part IV
  3. 3 Agylen » Ruby for Java Programmers, Part V

Leave a Reply