Skip to content

Commit

Permalink
Merge pull request #97 from sul-dlss/request-context
Browse files Browse the repository at this point in the history
show request can have jsonld context in path or params
  • Loading branch information
ndushay committed Dec 3, 2014
2 parents 8d57432 + 23ab4f9 commit c7c0f9e
Show file tree
Hide file tree
Showing 56 changed files with 22,736 additions and 1,886 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
@@ -1,6 +1,9 @@
language: ruby
script: rake ci
rvm:
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.1
# - ruby-head
env:
Expand Down
8 changes: 8 additions & 0 deletions Gemfile
Expand Up @@ -17,4 +17,12 @@ gem 'pry-byebug', group: [:development, :test]
if File.exists?(file)
puts "Loading #{file} ..." if $DEBUG # `ruby -d` or `bundle -v`
instance_eval File.read(file)
else
# we get here when we haven't yet generated the testing app via engine_cart

# as of rails v 4.2.0.rc1 (but perhaps not needed forever):
# somewhere in the triannon dependencies (2nd level or lower) is requiring sass; the version from
# the triannon dependencies conflicts with the rails application's dependencies on sass
# magically, the following line fixes this problem.
gem 'sass-rails'
end
52 changes: 40 additions & 12 deletions app/controllers/triannon/annotations_controller.rb
Expand Up @@ -14,7 +14,7 @@ def index
# GET /annotations/annotations/1
def show
respond_to do |format|
format.jsonld { render :json => @annotation.graph.to_jsonld }
format.jsonld { render_jsonld_per_context (params[:jsonld_context]) }
format.ttl {
accept_return_type = mime_type_from_accept(["application/x-turtle", "text/turtle"])
render :body => @annotation.graph.to_ttl, content_type: accept_return_type if accept_return_type }
Expand All @@ -23,7 +23,7 @@ def show
render :body => @annotation.graph.to_rdfxml, content_type: accept_return_type if accept_return_type }
format.json {
accept_return_type = mime_type_from_accept(["application/json", "text/x-json", "application/jsonrequest"])
render :json => @annotation.graph.to_jsonld, content_type: accept_return_type if accept_return_type }
render_jsonld_per_context(params[:jsonld_context], accept_return_type) }
format.xml {
accept_return_type = mime_type_from_accept(["application/xml", "text/xml", "application/x-xml"])
render :xml => @annotation.graph.to_rdfxml, content_type: accept_return_type if accept_return_type }
Expand All @@ -36,9 +36,10 @@ def new
@annotation = Annotation.new
end

# GET /annotations/annotations/1/edit
def edit
end
# NOT YET IMPLEMENTED
# GET /annotations/annotations/1/edit
# def edit
# end

# POST /annotations/annotations
def create
Expand All @@ -61,14 +62,15 @@ def create
end
end

# NOT YET IMPLEMENTED
# PATCH/PUT /annotations/annotations/1
def update
if @annotation.update(params)
redirect_to @annotation, notice: 'Annotation was successfully updated.'
else
render :edit
end
end
# def update
# if @annotation.update(params)
# redirect_to @annotation, notice: 'Annotation was successfully updated.'
# else
# render :edit
# end
# end

# DELETE /annotations/annotations/1
def destroy
Expand Down Expand Up @@ -106,5 +108,31 @@ def mime_type_from_accept(return_mime_types)
def ext_ref_error(exception)
render plain: exception.message, status: 403
end

# render json_ld respecting requested context
# @param [String] req_context set to "iiif" or "oa". Default is OA
# @param [String] mime_type the mime type to be set in the Content-Type header of the HTTP response
def render_jsonld_per_context (req_context, mime_type=nil)
case req_context
when "iiif", "IIIF"
if mime_type
render :json => @annotation.jsonld_iiif, content_type: mime_type
else
render :json => @annotation.jsonld_iiif
end
when "oa", "OA"
if mime_type
render :json => @annotation.jsonld_oa, content_type: mime_type
else
render :json => @annotation.jsonld_oa
end
else
if mime_type
render :json => @annotation.jsonld_oa, content_type: mime_type
else
render :json => @annotation.jsonld_oa
end
end
end
end
end
12 changes: 8 additions & 4 deletions app/models/triannon/annotation.rb
Expand Up @@ -70,13 +70,17 @@ def graph= g
# @return json-ld representation of graph with OpenAnnotation context as a url
def jsonld_oa
inline_context = graph.dump(:jsonld, :context => "http://www.w3.org/ns/oa.jsonld")
inline_context.sub(/@context.*@graph/m, "@context\": \"http://www.w3.org/ns/oa.jsonld\",\n \"@graph")
hash_from_json = JSON.parse(inline_context)
hash_from_json["@context"] = "http://www.w3.org/ns/oa.jsonld"
hash_from_json.to_json
end

# @return json-ld representation of graph with IIIF context as a url
def jsonld_iiif
inline_context = graph.dump(:jsonld, :context => "http://iiif.io/api/presentation/2/context.json")
inline_context.sub(/@context.*@graph/m, "@context\": \"http://iiif.io/api/presentation/2/context.json\",\n \"@graph")
hash_from_json = JSON.parse(inline_context)
hash_from_json["@context"] = "http://iiif.io/api/presentation/2/context.json"
hash_from_json.to_json
end

# query for a subject with type of RDF::OpenAnnotation.Annotation
Expand Down Expand Up @@ -122,7 +126,7 @@ def data_to_graph
if data
data.strip!
case data
when /\A\{.+\}\Z/m
when /\A\{.+\}\Z/m # (Note: \A and \Z and m are needed instead of ^$ due to \n in data)
g ||= RDF::Graph.new << JSON::LD::API.toRdf(json_ld) if json_ld
self.data = g.dump(:ttl) if g
when /\A<.+>\Z/m # (Note: \A and \Z and m are needed instead of ^$ due to \n in data)
Expand All @@ -138,7 +142,7 @@ def data_to_graph
end
g
end

def json_ld
if data.match(/"@context"\s*\:\s*"http\:\/\/www\.w3\.org\/ns\/oa-context-20130208\.json"/)
data.sub!("\"http://www.w3.org/ns/oa-context-20130208.json\"", json_oa_context)
Expand Down
15 changes: 14 additions & 1 deletion config/routes.rb
@@ -1,5 +1,18 @@
Triannon::Engine.routes.draw do
root to: 'annotations#index'

resources :annotations
# show action must explicitly forbid "iiif" or "oa" as id values; couldn't
# figure out how to do it with regexp constraint since beginning and end
# matchers aren't allowed when enforcing formats for segment (e.g. :id)
get '/annotations/:id(.:format)', to: 'annotations#show',
constraints: lambda { |request|
id = request.env["action_dispatch.request.path_parameters"][:id]
id !~ /^iiif$/ && id !~ /^oa$/
}

resources :annotations, :except => [:update, :edit, :show]

# allow jsonld context in path
get '/annotations/:jsonld_context/:id(.:format)', to: 'annotations#show', jsonld_context: /iiif|oa/

end
2 changes: 1 addition & 1 deletion lib/triannon/version.rb
@@ -1,3 +1,3 @@
module Triannon
VERSION = "0.3.2"
VERSION = "0.4.0"
end

0 comments on commit c7c0f9e

Please sign in to comment.