项目作者: ontola

项目描述 :
Adds RDF serialization, like n-triples, json-ld or turtle, to Ruby on Rails active model serializers
高级语言: Ruby
项目地址: git://github.com/ontola/rdf-serializers.git
创建时间: 2017-12-12T09:58:42Z
项目社区:https://github.com/ontola/rdf-serializers

开源协议:MIT License

下载


RDF Serializers

Build Status

About

RDF Serializers enables serialization to RDF formats. It uses fast-jsonapi serializers, with a few modifications.
The serialization itself is done by the rdf gem.

This was built at Ontola. If you want to know more about our passion for open data, send us an e-mail.

Installation

Add this line to your application’s Gemfile:

  1. gem 'rdf-serializers'

And then execute:

  1. $ bundle

Getting started

First, register the formats you wish to serialize to. For example, add the following to config/initializers/rdf_serializers.rb:

  1. require 'rdf/serializers/renderers'
  2. RDF::Serializers::Renderers.register(:ntriples)

This automatically registers the MIME type.

In your controllers, add:

  1. respond_to do |format|
  2. format.nt { render nt: model }
  3. end

Configuration

You can configure the gem using RDF::Serializers.configure.

  1. RDF::Serializers.configure do |config|
  2. config.always_include_named_graphs = false # true by default. Whether to include named graphs when the serialization format does not support quads.
  3. config.default_graph = RDF::URI('https://example.com/graph') # nil by default.
  4. end

Formats

You can register multiple formats, if you add the correct gems. For example, add rdf-turtle to your gemfile and put this in the initializer:

  1. require 'rdf/serializers/renderers'
  2. opts = {
  3. prefixes: {
  4. ns: 'http://rdf.freebase.com/ns/',
  5. key: 'http://rdf.freebase.com/key/',
  6. owl: 'http://www.w3.org/2002/07/owl#',
  7. rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
  8. rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  9. xsd: 'http://www.w3.org/2001/XMLSchema#'
  10. }
  11. }
  12. RDF::Serializers::Renderers.register(%i[ntriples turtle], opts)

The RDF gem has a list of available RDF Serialization Formats, which includes:

  • NTriples
  • Turtle
  • N3
  • RDF/XML
  • JSON::LD

and more

Serializing

Add a predicate to the attributes and relations you wish to serialize.

It’s recommended to reuse existing vocabularies provided by the rdf gem and the rdf-vocab gem,
and add your own vocab for missing predicates. One way to be able to access the different vocabs throughout your application is by defining a module:

  1. require 'rdf'
  2. require "rdf/vocab"
  3. module NS
  4. SCHEMA = RDF::Vocab::SCHEMA
  5. MY_VOCAB = RDF::Vocabulary.new('http://example.com/')
  6. end

Now add the predicates to your serializers.

Old:

  1. class PostSerializer
  2. include JSONAPI::Serializer
  3. attributes :title, :body
  4. belongs_to :author
  5. has_many :comments
  6. end

New:

  1. class PostSerializer
  2. include RDF::Serializers::ObjectSerializer
  3. attribute :title, predicate: NS::SCHEMA[:name]
  4. attribute :body, predicate: NS::SCHEMA[:text]
  5. belongs_to :author, predicate: NS::MY_VOCAB[:author]
  6. has_many :comments, predicate: NS::MY_VOCAB[:comments]
  7. end

For RDF serialization, you are required to add an iri method to your model, which must return a RDF::Resource. For example:

  1. def iri
  2. RDF::URI(Rails.application.routes.url_helpers.comment_url(object))
  3. end

In contrast to the JSON API serializer, this rdf serializers don’t automatically serialize the type and id of your model.
It’s recommended to add attribute :type, predicate: RDF[:type] and a method defining the type to your serializers to fix this.

Custom statements per model

You can add custom statements to the serialization of a model in the serializer, for example:

  1. class PostSerializer
  2. include RDF::Serializers::ObjectSerializer
  3. statements :my_custom_statements
  4. def my_custom_statements
  5. [RDF::Statement.new(RDF::URI('https://example.com'), NS::MY_VOCAB[:fooBar], 1)]
  6. end
  7. end

Meta statements

You can add additional statements to the serialization in the controller, for example:

  1. render nt: model, meta: [RDF::Statement.new(RDF::URI('https://example.com'), NS::MY_VOCAB[:fooBar], 1)]

Contributing

The usual stuff. Open an issue to discuss a change, open pull requests directly for bugfixes and refactors.