Monday, September 8, 2008

Diggr released

I finally got around to a small side project I have wanted to work on: a new ruby wrapper for the Digg API. There's one existing library out there that I know of, but I didn't like the syntax all that much. So, like any good developer, I've reinvented the wheel! My library is called diggr and it strives to be simple and consistent with the Digg API endpoints listed here. Rather than boring you with a detailed description, I'll just show some code snippets below. For more information on diggr check out:

- Docs
- Source

Here are a few simple examples:

require 'rubygems'
require 'diggr'

diggr = Diggr::API.new

# retrieve a single user by user name and print the number of profile views
user = diggr.user("johndoe").fetch
puts user.profileviews

# iterator over the most recent 10 stories (default return size) and print their titles
diggr.stories.each do |story|
puts story.title
end

# print the title of the 3 most recent hot stories
diggr.stories.hot.options(:count => 3).each do |story|
puts story.title
end

# build an array of stories whos title contains "foo"
diggr.stories.inject([]) do |array,story|
array << story if story.title =~ /foo/
array
end

# print the title of the 2nd and 3rd most recent stories
diggr.stories.options(:count => 2, :offset => 2).each do |story|
puts story.title
end