From e22c3f33e9d87b1c933b44f28727a5feaef24295 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Sat, 24 Jan 2015 16:37:40 -0500 Subject: [PATCH] first trial of linked data map response --- app/controllers/maps_controller.rb | 16 ++++++++++++++ app/helpers/maps_helper.rb | 35 ++++++++++++++++++++++++++++++ config/routes.rb | 4 ++++ 3 files changed, 55 insertions(+) diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 8f0ced9b..e52e7e75 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -1,4 +1,5 @@ class MapsController < ApplicationController + include MapsHelper before_filter :require_user, only: [:create, :update, :screenshot, :destroy] @@ -123,6 +124,21 @@ class MapsController < ApplicationController end end + # GET maps/:id/ld + def ld + + @current = current_user + @map = Map.find(params[:id]).authorize_to_show(@current) + + if not @map + redirect_to root_url, notice: "Access denied. That map is private." and return + end + + respond_to do |format| + format.json { render json: linkeddata(@map) } + end + end + # POST maps def create diff --git a/app/helpers/maps_helper.rb b/app/helpers/maps_helper.rb index 0c9b3a08..9569f899 100644 --- a/app/helpers/maps_helper.rb +++ b/app/helpers/maps_helper.rb @@ -33,4 +33,39 @@ module MapsHelper return temp end + def linkeddata(map) + + @alltopics = map.topics.delete_if {|t| t.permission == "private" && (!authenticated? || (authenticated? && @current.id != t.user_id)) } + + js = Hash.new() + + js['@context'] = { "@vocab" => "http://schema.org/", "mm" => "http://ns.metamaps.cc/", "mmc" => "http://metamaps.cc/metacodes/" } + js['@id'] = "http://metamaps.cc/maps/" + map.id.to_s + js['@type'] = [ "CreativeWork", "mm:Metamap" ] + js['name'] = map.name + js['description'] = map.desc + + graph = [] + + @alltopics.each do |t| + topic = Hash.new() + topic['@id'] = 'http://metamaps.cc/topics/' + t.id.to_s + topic['@type'] = [ + 'mmc:' + t.metacode_id.to_s + ] + topic['name'] = t.name + + t.synapses2.each do |s| + topic['http://metamaps.cc/synapses/' + s.id.to_s] = [ 'http://metamaps.cc/topics/' + s.node1_id.to_s ] + end + + graph.push(topic) + end + + js['@graph'] = graph + + return js + end end + + diff --git a/config/routes.rb b/config/routes.rb index 4d9c5501..0176a673 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,6 +26,10 @@ Metamaps::Application.routes.draw do match 'explore/mapper/:id', to: 'maps#index', via: :get, as: :usermaps resources :maps, except: [:new, :edit] match 'maps/:id/contains', to: 'maps#contains', via: :get, as: :contains + + # for linked data structure + match 'maps/:id/ld', to: 'maps#ld', via: :get, as: :ld + match 'maps/:id/upload_screenshot', to: 'maps#screenshot', via: :post, as: :screenshot devise_for :users, controllers: { registrations: 'users/registrations', passwords: 'users/passwords', sessions: 'devise/sessions' }, :skip => [:sessions]