[Developers] Another Ruby API
Chris Eppstein
chris at eppsteins.net
Mon Dec 31 23:42:15 UTC 2007
Hi,
I'm starting work on a new freebase-enabled web application using ruby
on rails (more on that in a later) and couldn't find a full featured
ruby library for accessing freebase.com nor one that appears to have
any actively development. So I started writing my own :-). I've
checked in the code and it is accessible on rubyforge(http://rubyforge.org/projects/freebaseapi/
). It has many of the features that others have said they've not
gotten around to yet on their blog posts, etc, but it's still missing
many things (like writes and authentication). However, it does provide
very nice read access right now so I'm not going to wait to release
what I've got. It's early-alpha (I wrote it yesterday) and is probably
full of bugs as it's only been manually tested so far.
Read the docs:
http://freebaseapi.rubyforge.org/svn/trunk/freebase/rdoc/index.html
It's currently packaged as a rails plugin and it uses rails extensions
to ruby core. Install the plug-in into your application:
script/plugin install svn://rubyforge.org/var/svn/freebaseapi/trunk/
freebase
I'd love to join forces with other interested ruby developers -- I'm
not trying to muddy the waters so if there's active development going
on elsewhere please let me know.
Without further ado, here's an example of the library used to iterate
all the tracks of all the albums for a given music artist (This code
lazy loads the associations -- if they were specified in the find
conditions, cached values would be used instead):
# Usage:
# ./script/runner vendor/plugins/freebase/examples/albums.rb "The
Police"
Freebase::Type.define('/music/artist')
artist = Freebase::Types::Music::Artist.find(:first, :conditions =>
{:name => ARGV[0]})
artist.albums.each_with_index do |album, i|
# I don't know why, but I'm getting some random nils here that
makes this code a little awkward.
# It's probably a bug in my code.
puts "#{i+1}) #{album.name} (#{(album.release_date rescue "ERROR")
|| '?'})"
album.tracks.each_with_index do |track, j|
puts "\tT#{j+1}. #{track ? track.name : '???'}"
end rescue nil
end
Of note here is that freebase types become actual ruby classes that
behave like active record models (Inheriting from Freebase::Base). The
type schema is loaded when the class is defined, this is done for
other types as they are encountered. So after running thru this code
there are three type classes:
Freebase::Types::Music::Artist
Freebase::Types::Music::Album
Freebase::Types::Music::Track
Once loaded, Freebase::Type.define('/music/album') returns the cached
class definition.
I think ruby is especially well suited to make the freebase API easily
accessible to developers because it's indistinguishable from working
with a local database (easier, in fact, because the association
metadata is inferred instead of specified). I would love any and all
feedback. If it's positive, I'll write unit tests, etc to enable
collaboration.
Thanks,
Chris Eppstein
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freebase.com/pipermail/developers/attachments/20071231/fbf34ecb/attachment.htm
More information about the Developers
mailing list