[Developers] Another Ruby API
Chris Eppstein
chris at eppsteins.net
Wed Jan 2 08:15:38 UTC 2008
Hello again,
I received some excellent feedback yesterday and was able to
incorporate it into the code today, along with some significant
refactoring and improved documentation. If you're interested please
check out the most recent code:
http://freebaseapi.rubyforge.org/svn/trunk/freebase/
or read the docs at:
http://freebaseapi.rubyforge.org/svn/trunk/freebase/rdoc/index.html
New Features:
No need to call Freebase::Type.define('/music/artist') before
accessing Freebase::Types::Music::Artist (thanks to pat allan for this
suggestion and code examples)
Ability to add custom methods to instances of Freebase types using a
mixin and naming conventions.
Support for nested queries to retrieve multiple levels of associations
in a single query
Open Questions/Concerns:
:fb_object => true should be added to the subquery whenever sub-
querying an association so that a distinction can be drawn for the
following differences in validation logic between object types and
core types:
objects have multi-valued types, core types do not and you may not
query a core type with [] you have to use null.
values of core types do not have an id and you may not specify 'id' :
null in the query of a core type.
In both of these cases, the validation seems arbitrarily enforced;
returning ['/type/text'] and 'id':null back respectively are in line
with the other behaviors of the system, in my estimation.
I'm getting nulls from collections associations from Artist -> Album
and from Album -> Track. I understand the null names problem now and
am handling that appropriately per the recommendations in my new
query. But I don't understand why a null would be returned as an
element of an array. Please let me know whether to begin searching for
a code defect on my end.
Thanks,
Chris
The code example again using the latest api:
# Usage:
# ./script/runner vendor/plugins/freebase/examples/albums.rb "The
Police"
# add these methods to the Track class
module Freebase::Mixins::Music
module Track
def formatted_length
"#{self.length.to_i / 60}:#{sprintf("%02i", self.length.to_i %
60)}"
end
end
end
artist = Freebase::Types::Music::Artist.find(:first,
:conditions => {
:name => {:value => ARGV[0], :lang => {:name => "English"}},
:album => [{
:fb_object => true,
:name => {:value => nil, :lang => {:name => "English"}},
:release_date => nil,
:track => [{
:fb_object => true,
:length => nil,
:name => {:value => nil, :lang => {:name => "English"}}
}]
}]
}
)
artist.albums.each_with_index do |album, i|
next unless album # XXX I don't know why I'm getting random nils
here.
puts "#{i+1}) #{album.name} (#{album.release_date || '?'})"
album.tracks.compact.each_with_index do |track, j|
puts "\tT#{j+1}. #{track.name || '???'}
(#{track.formatted_length})"
end
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freebase.com/pipermail/developers/attachments/20080102/a44a9a8d/attachment-0001.htm
More information about the Developers
mailing list