Read and Write RDF in your Rails apps
As we’ve long been promising, we’ve started to release some open source code to help people publish and consume Linked Open Data. The first component that we’d like to talk about is Tripod. It’s an RDF and SPARQL ORM, designed for use in Ruby-on-Rails apps.
What’s the point of Tripod?
If you’re writing a Ruby application that reads or writes a lot of RDF data from SPARQL endpoints, then Tripod should make life easier for you. Tripod lets you define ActiveModel-compliant Rails models for use in your applications, providing a familiar interface for use in your Rails controllers and views.
To use all features of Tripod, you need to have access to a SPARQL 1.1 database endpoint (over HTTP). We use Fuseki, part of the Apache Jena project. If you want to be able to persist any changes to data, the endpoint needs to support SPARQL UPDATE (and you’ll need the appropriate permissions), but note that Tripod is also useful for read-only use cases too.
How to use Tripod
Tripod::Resource into your model class, and specify as much or as little information as required. Here’s quite a specific Person model, with a default type and graph:
class Person include Tripod::Resource # these are the default rdf-type and graph for resources of this class rdf_type 'http://person' graph_uri 'http://people' field :name, 'http://name' field :knows, 'http://knows', :multivalued => true field :aliases, 'http://alias', :multivalued => true field :age, 'http://age', :datatype => RDF::XSD.integer field :important_dates, 'http://importantdates', :datatype => RDF::XSD.date, :multivalued => true end
… and here’s a fairly free-form Resource model, with only the label predicate specified.
class Resource include Tripod::Resource field :label, RDF::RDFS.label end
field just creates the relevant getters and setters on the instances of the model.
Note: If you don’t specify a type or graph at the class-level, you can still set them on specific instances. For now, we assume that everything in the database is stored in named graphs, so if you don’t set a graph at all then you won’t be able to persist a resource to the db.
p = Person.new('http://ric') p.name = 'Ric' p.age = 31 p.save r = Resource.new('http://myresource', 'http://mygraph') r.label = 'my resource' r.save person = Person.find('http://ric') person.name #=> Ric
There’s also chainable query DSL which helps you DRY-up your code by preventing you from having to write lots similar sparql queries throughout your application.
Person.where("?uri <http://name> ?name").limit(1).offset(0).order("DESC(?name)")
This is just a taster of what Tripod can do. For full installation and usage details see the Github repo.
Uses of Tripod in the wild
We first used Tripod in the SmartJourney application we worked on for Aberdeen City and County councils. And we’re currently working on an open source version of our PublishMyData engine, which uses Tripod for database access.
We’ve released Tripod under the MIT Licence, which means that there are almost no restrictions on what you can do with it. We’d love to hear about what you build with it.
We'd love it if you shared this article.