2012年9月24日月曜日

Rails で Turtle ( .ttl ) を書き出す(単にフォーマットの追加)

参考: Linked Data on Rails
$ rails new turtleTest
$ cd turtleTest
$ rails generate scaffold user username:string
$ vi config/initializers/mime_types.rb //Edit
Mime::Type.register "text/turtle", :ttl
$ vi app/views/users/index.ttl.erb //Create
@prefix ns: <http://example.org/ns> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema> .

<% @users.each_with_index do |user, i| %>
_:a<%= (i+1) %> ns:name "<%= user.username %>"^^xsd:string .
<% end %>
$ vi app/views/users/show.ttl.erb //Create
@prefix ns: <http://example.org/ns> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema> .

_:a ns:username "<%= @user.username %>"^^xsd:string .
$ vi app/controllers/users_controller.rb //Edit
…
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @users }
      format.ttl
    end
  end
…
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @user }
      format.ttl
    end
  end
…
$ rake db:migrate
$ rails server
Result
http://0.0.0.0:3000/users.ttl
@prefix ns: <http://example.org/ns> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema> .

_:a0 ns:name "あいうえお"^^xsd:string .
_:a1 ns:name "かきくけこ"^^xsd:string .
http://0.0.0.0:3000/users/1.ttl
@prefix ns: <http://example.org/ns> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema> .

_:a ns:name "あいうえお"^^xsd:string .

0 件のコメント: