«

»

Sep 08

Random Quotes

Quotes can provide inspiration or food for thought.  In this entry, I’m examining the use of open-uri and feed-normalizer to get a random quote which could be used as either a thought for the day or a “fortune” replacement.

open-uri is very useful — it provides a “nice” interface to opening and reading uri’s.  feed-normalizer provides the ability to have a single interface to both rss and atom feeds.  The two make it very easy to get a random quotation.  I’m using ThinkExist for the source of the quotes, but you may have other sources which you prefer.

The code is really pretty simple.  There is, however, one dependency, the feed-normalizer gem, which you can install via:

Once it’s installed, you can use the following code:

It opens the feed, and gets the number of quotes returned — in my tests, it’s been five (5), but I don’t want to hardcode a number, hence the variable. From this set of quotes, I’m getting one at random. ThinkExist uses the content to have the body of the quote and title to have the author. A little bit of formatting, and we’re done.

The code may be downloaded: quotation.rb

Enjoy!

Leave a Reply

%d bloggers like this: