ruby-rdf / rack-linkeddata

Rack middleware for Linked Data content negotiation.
http://rubygems.org/gems/rack-linkeddata
The Unlicense
25 stars 2 forks source link

Documentation for Rails? #6

Open no-reply opened 7 years ago

no-reply commented 7 years ago

The README.md says using this with Rails should look like:

# config/application.rb
require 'rack/linkeddata'

class Application < Rails::Application
  config.middleware.use Rack::LinkedData::ContentNegotiation
end

This works fine to get the middleware into the Rails stack, but it's not at all clear what to do from there. Returning an RDF::Enumerable from a controller action doesn't do the trick, and I can't find any good recommendations about how to give an arbitrary object as a response body. Is there a right way to do this we can get into the README?

gkellogg commented 7 years ago

Returning Enumerable from a controller should kick in content-negotiation to get the appropriate form. This is used (through sinatra-linked data) in the Linter, for example. SPARQL has a similar mechanism for doing content negotiation.

Certainly the README could use improvement, and this probably isn't a common way for Rails developers to do content negotiation. Specs should describe how it's done in rack-linkeddata, though.

Contributions welcome.

no-reply commented 7 years ago

When returning an enumerable from e.g. MyController#index, I get an ActionView::MissingTemplate error.

Several other things I've tried (e.g. response.body = RDF::Graph.new and return) fail in other ways.

no-reply commented 7 years ago

I'm able to hack around the ActionView issue by adding an empty index.erb template, but then the best result I've been able to achieve is getting an ntriples string to appear in an ActionDispatch::Response::RackBody#body. (this is with response.body = my_graph).

Returning an Enumerable leads to an empty response body; but I haven't been able to spot where the enumerable disappears.

gkellogg commented 7 years ago

It's possible that Rails has changed enough since this was introduced that it either needs to be modified, or is no longer relevant for use with Rails.

A more natural way in Rails is to use respond_to {} for the mime types you're interested in and use Enumerable#dump with the appropriate format.

I use this in the context of Sinatra, where things may be a bit simpler.