04.07.07

Digging for Rubies - The Digg Wrapper for Ruby on Rails

Recently, digg.com released their API to the public, allowing users to pull xml information from digg for use in all sorts of creative applications. It wasn’t more than a day or so before John Wulff started working on and released a ruby wrapper for the digg api. This saved me the trouble of stumbling through creating my own, and allowed me to get to work on some ideas I had for working with the digg API.

 

The beauty of Ruby on Rails, of course, is that it makes seemingly complex applications very simple to write, often requiring a fraction of the code as it would take in an alternative programming language, such as Java or PHP. So it wasn’t long before I had ”Diggraphy” up and running, my first attempt at a digg tool.

 

Diggraphy displays digg stories on a vertical histogram, with the y-axis representing time and the x-axis the number of diggs for each story. The result is a useful visual aid for differentiating popular stories over a given time period. I included a little ruby/ajax magic to aid the user experience a bit, and tried to keep the app as lightweight as possible.

 

The first problem I ran into was abnormally popular stories skewing the graph to the point where all other stories were scrunched up on the left side of the graph. This was solved using a logarithmic scale for the x-axis. Other issues I had to deal with involved getting the y-axis grid lines to display underneath the bars. It’s clear I have a long way to go in cleaning up the look and feel.

 

So how was this done? I’ll take this opportunity to explain about how one goes about using Wulff’s ruby wrapper for digg:

 

Digg Ruby Wrapper Tutorial:

 

First off, you need to grab the wrapper, which can be found here. Download digg.rb and save it to your /lib folder in your rails application.

 

At this point you’re going to want to add the following line to the bottom of your environment.rb file:

require 'lib/digg.rb'

 

Digg wants people to use their API, but within limits, so to prevent people from making billions of requests per second for information and clogging the internet tubes, they have strict guidelines in place for the amount of requests one user can make in a given period of time.

 

So before moving on we’re going to want to put our own API key in place of John’s, so that he doesn’t get banned for too many requests to the digg API, so open up the digg.rb file you just downloaded and replace the USER_AGENT value with your website URL as well as the API Key value in the initialize method.

 

Go ahead and create the controllers and views for the app you intend to create. Create a new digg object with

digg = Digg.new

 

From here you can grab all sorts of information from digg.com. For instance, if you want to grab 30 of the most popular stories in chronological order, you would use:

@popular = digg.stories('stories/popular', :count =>  '30' , 
:sort => 'promote_date-desc')

 

Now that we have an array with our 30 popular stories, we can cycle through them using

@popular.each do |popular|

 

and output any amount of information, such as the direct link to the article using @popular.href, the digg link using @popular.link, the title using @popular.title, and so on. The values given to us by digg are fully outlined in digg’s API documentation.

 

What John’s code is doing here is using a method called ‘fetch’ to specify to digg which function to use, in this case the “list stories” function, along with any other arguments like our request to sort by promote_date-desc and for a limit of 30 stories. What’s actually being sent to digg is a url which digg.rb constructs sends out to digg.com.

 

The URL for the example code above is http://www.services.digg.com/stories/popular?appkey=http%3A%2F%2F www.exampleapikey.com%2Fdiggraph&sort=promote_date-desc&count=30

which returns an xml file with all the information you requested neatly nested away into proper categories. Go ahead and follow the link to see what type of information is included with a given request.

 

The Ruby wrapper then goes merrily along building a neat array from which you can extract all the information provided.

 

Fetch can grab other information as well, such information about what users are digging, how much they’re digging, and plenty of other juicy details. The most clever use of this information has to be the smart digg button firefox extension, which places the number of diggs for the page you’re currently viewing on the status bar of your browser, as well as giving you the option to digg or submit the story.

 

But I digress. Perhaps you’re an Apple fanatic (a veritable rarity among digg users) and you’re only interested in working with stories under the Apple topic on digg. We’ll change our request to

@popular = digg.stories('stories/topic/apple/popular', :count =>  '30' , 
:sort => 'promote_date-desc')

 

The key is the ‘stories/topic/apple/popular’ part. Remember the URL that was generated? We’re adding this line to the URL and thus getting different information back from digg. It’s that simple.

 

I’d go more in depth, but I have confidence that by playing around with it and referring always to digg’s API documentation, you’ll have your very clever Digg on Rails app up and running in no time.



Subscribe to Just a Nutter RSS Feed

Comment_button_spacer

Current Projects

Area Studios

My web development company and project incubator. We’re always looking for interesting projects. Stop by and check out our work.

View Now

oqodo.com

Oqodo started as a mini competition between a friend and myself to build an app for our friends to keep in touch. It will soon grow into something much bigger.

View Now

My Web Presence

Flickr
Facebook
Digg
Lastfm
Linkedin
Twitter
Basecamp