Class: Triannon::AnnotationsController

Inherits:
ApplicationController show all
Includes:
RdfResponseFormats
Defined in:
app/controllers/triannon/annotations_controller.rb

Instance Method Summary collapse

Methods included from RdfResponseFormats

#context_url_from_accept, #context_url_from_link, #default_format_jsonld, #mime_type_from_accept

Instance Method Details

#createObject

POST /annotations



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/triannon/annotations_controller.rb', line 63

def create
  # FIXME: this is probably a bad way of allowing app form to be used as well as direct post requests
  # see https://github.com/sul-dlss/triannon/issues/90 -- prob just want to fix the form to do a POST
  #  note that need to check for empty? if HTTP Header Content-Type is json (but not jsonld).
  if params["annotation"] && !params["annotation"].empty?
    # it's from app html form
    params.require(:annotation).permit(:data)
    if params["annotation"]["data"]
      @annotation = Annotation.new({:data => params["annotation"]["data"]})
    end
  else
    # it's a direct post request
    content_type = request.headers["Content-Type"]
    @annotation = Annotation.new({:data => request.body.read, :expected_content_type => content_type})
  end

  if @annotation.save
    default_format_jsonld # NOTE: this must be here and not in before_filter or we get Missing template errors
    flash[:notice] = "Annotation #{@annotation.id} was successfully created."
    respond_to do |format|
      format.jsonld {
        context_url = context_url_from_link ? context_url_from_link : context_url_from_accept
        if context_url && context_url == OA::Graph::IIIF_CONTEXT_URL
          render :json => @annotation.jsonld_iiif, status: 201, content_type: "application/ld+json"
        else
          render :json => @annotation.jsonld_oa, status: 201, content_type: "application/ld+json"
        end
      }
      format.ttl {
        accept_return_type = mime_type_from_accept(["application/x-turtle", "text/turtle"])
        render :body => @annotation.graph.to_ttl, status: 201, content_type: accept_return_type if accept_return_type }
      format.rdfxml {
        accept_return_type = mime_type_from_accept(["application/rdf+xml", "text/rdf+xml", "text/rdf"])
        render :body => @annotation.graph.to_rdfxml, status: 201, 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"])
        context_url = context_url_from_link ? context_url_from_link : context_url_from_accept
        if context_url && context_url == OA::Graph::IIIF_CONTEXT_URL
          render :json => @annotation.jsonld_iiif, status: 201, content_type: accept_return_type if accept_return_type
        else
          render :json => @annotation.jsonld_oa, status: 201, content_type: accept_return_type if accept_return_type
        end
      }
      format.xml {
        accept_return_type = mime_type_from_accept(["application/xml", "text/xml", "application/x-xml"])
        render :body => @annotation.graph.to_rdfxml, status: 201, content_type: accept_return_type if accept_return_type }
      format.html { redirect_to @annotation }
    end
  else
    render :new, status: 400
  end
end

#destroyObject

DELETE /annotations/1



127
128
129
130
# File 'app/controllers/triannon/annotations_controller.rb', line 127

def destroy
  @annotation.destroy
  redirect_to annotations_url, status: 204, notice: 'Annotation was successfully destroyed.'
end

#indexObject

GET /annotations



14
15
16
# File 'app/controllers/triannon/annotations_controller.rb', line 14

def index
  redirect_to "/search"
end

#newObject

GET /annotations/new



53
54
55
# File 'app/controllers/triannon/annotations_controller.rb', line 53

def new
  @annotation = Annotation.new
end

#showObject

GET /annotations/1



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/triannon/annotations_controller.rb', line 19

def show
  # TODO:  json.set! "@context", OA::Graph::OA_DATED_CONTEXT_URL - would this work?
  respond_to do |format|
    format.jsonld {
      context_url = context_url_from_accept ? context_url_from_accept : context_url_from_link
      if context_url && context_url == OA::Graph::IIIF_CONTEXT_URL
        render_jsonld_per_context("iiif", "application/ld+json")
      else
        render_jsonld_per_context(params[:jsonld_context], "application/ld+json")
      end
    }
    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 }
    format.rdfxml {
      accept_return_type = mime_type_from_accept(["application/rdf+xml", "text/rdf+xml", "text/rdf"])
      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"])
      context_url = context_url_from_link ? context_url_from_link : context_url_from_accept
      if context_url && context_url == OA::Graph::IIIF_CONTEXT_URL
        render_jsonld_per_context("iiif", accept_return_type)
      else
        render_jsonld_per_context(params[:jsonld_context], accept_return_type)
      end
    }
    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 }
    format.html { render :show }
  end
end