Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
134 lines (72 sloc) 7.35 KB
RDFObjects are intended to simplify working with RDF data by providing a (more) Ruby-like interface to resources (thanks to OpenStruct).
There is a Google Group at to discuss problems, enhancements, changes, announcements, etc.
The gem is currently hosted at so you'll either need the gemcutter gem (and run "gem tumble") or add to your sources manually (gem sources -a
$ sudo gem install rdfobjects
* Nokogiri (the idea is for more options in the future)
* curies
* json (or json_pure)
>> require 'rdf_objects'
>> include RDFObject
>> Curie.add_prefixes! :skos=>""
>> resource ='')
>> resource.describe
>> resource.skos
=> {"inScheme"=>[#<RDFObject::Resource uri="">, #<RDFObject::Resource uri="">], "broader"=>[#<RDFObject::Resource skos={"prefLabel"=>"Semantic networks (Information theory)"}, uri="">, #<RDFObject::Resource skos={"prefLabel"=>"World Wide Web"}, uri="">, #<RDFObject::Resource skos={"prefLabel"=>"Semantic integration (Computer systems)"}, uri="">], "closeMatch"=>#<RDFObject::Resource uri="">, "prefLabel"=>"Semantic Web"}
>> resource["[skos:prefLabel]"]
=> "Semantic Web"
>> resource.skos["prefLabel"]
=> "Semantic Web"
>> resource[""]
=> "Semantic Web"
>> resource.skos["broader"].first.skos["prefLabel"]
=> "Semantic networks (Information theory)"
Unnecessary, but helpful, way to define typed literals
>> source ="Library of Congress Authorities", {:language=>"en"})
And assert them
>> resource.assert("", source)
=> ["Work cat.: 2002070545: The Semantic Web--ISWC 20002, 2002.", "ASTI on FirstSearch, May 6, 2002: in titles (semantic Web)", "Engr. index online, May 6, 2002 (identifier: Semantic Web)", "Library of Congress Authorities"]
>> resource[""].last.language
=> "en"
To relate a resource to another URI you can use #.relate - it will accept full uri strings, safe curies or other RDFObject::Resource objects
>> resource.relate("[skos:closeMatch]", "")
=> [#<RDFObject::Resource uri="">, #<RDFObject::Resource uri="">]
These relationships are actually replaced with RDFObject::ResourceReference objects (to prevent massive recursion errors). To access the actual Resource object, use:
>> resource["[skos:closeMatch]"].first.resource
=> #<RDFObject::Resource uri="">
In order to be sure you are always asserting against the same object, use RDFObject::Collection objects to manage your Resources.
>> collection =
=> {}
>> r1 = collection.find_or_create('')
=> #<RDFObject::Resource uri="">
>> r1.object_id
=> 8996290
>> r2 = collection.find_or_create('')
=> #<RDFObject::Resource uri="">
>> r2.object_id
=> 8996290
So relationships and assertions are always applied to the same objects.
You can delete a single resource:
>> collection.remove(r1)
>> collection
=> {}
There is also a convenience method on RDFObject::Collection to help find particular resources by predicate:
>> resources_with_types = collection.find_by_predicate("")
And to find resources by predicate and object:
>> resources_i_want = collection.find_by_predicate_and_object("", "")
Both these return RDFObject::Collection objects with your matches. Changes to resources in these collections also affect the resource in original collection.
There are also parsers for ntriples, rdf/xml, RSS 1.0, RDFa, and JSON. Parsers return a RDFObject::Collection with all of the resources discovered in the RDF source.
>> resources = Parser.parse(open('lcsh.nt').read)
>> resources.values.first
=> #<RDFObject::Resource n0={"altLabel"=>"Lichen ruber planus", "inScheme"=>[#<RDFObject::Resource uri="">, #<RDFObject::Resource uri="">], "prefLabel"=>"Lichen planus"}, n1={"sameAs"=>#<RDFObject::Resource uri="info:lc/authorities/sh85076767">}, uri="", n2={"modified"=>#<DateTime: 211644344801/86400,-1/6,2299161>}, rdf={"type"=>#<RDFObject::Resource uri="">}>
There are serializers for RDF/XML and ntriples. Both resources and collections can be serialized.
>> resource.to_xml
=> "<rdf:RDF xmlns:n0="" xmlns:n1="" xmlns:rdf="" xmlns:n2=""><rdf:Description rdf:about=""><n0:altLabel>Lichen ruber planus</n0:altLabel><n0:inScheme><rdf:Description rdf:about=""></rdf:Description></n0:inScheme><n0:inScheme><rdf:Description rdf:about=""></rdf:Description></n0:inScheme><n0:prefLabel>Lichen planus</n0:prefLabel><n1:sameAs><rdf:Description rdf:about="info:lc/authorities/sh85076767"></rdf:Description></n1:sameAs><n2:modified rdf:datatype="">1994-08-22T15:46:41-04:00</n2:modified><rdf:type><rdf:Description rdf:about=""></rdf:Description></rdf:type></rdf:Description></rdf:RDF>"
>> collection.to_xml
>> resource.to_ntriples
=> "<> <> "Lichen ruber planus"@en.\n<> <> <>.\n<> <> <>.\n<> <> "Lichen planus"@en.\n<> <> <info:lc/authorities/sh85076767>.\n<> <> "1994-08-22T15:46:41-04:00"^^<>.\n<> <> <>.\n"
>> collection.to_ntriples