Skip to content

Commit

Permalink
create action honors context_url info in HTTP Accept or Link headers …
Browse files Browse the repository at this point in the history
…for jsonld, json
  • Loading branch information
ndushay committed Mar 9, 2015
1 parent 5709ebe commit d780b8b
Show file tree
Hide file tree
Showing 35 changed files with 88,409 additions and 21 deletions.
17 changes: 15 additions & 2 deletions app/controllers/triannon/annotations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def index

# GET /annotations/1
def show
# TODO: json.set! "@context", Triannon::JsonldContext::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
Expand Down Expand Up @@ -75,7 +76,13 @@ def create
default_format_jsonld # NOTE: this must be here and not in before_filter or we get Missing template errors
respond_to do |format|
format.jsonld {
render :json => @annotation.jsonld_oa, status: 201, content_type: "application/ld+json", notice: "Annotation #{@annotation.id} was successfully created." }
context_url = context_url_from_link ? context_url_from_link : context_url_from_accept
if context_url && context_url == Triannon::JsonldContext::IIIF_CONTEXT_URL
render :json => @annotation.jsonld_iiif, status: 201, content_type: "application/ld+json", notice: "Annotation #{@annotation.id} was successfully created."
else
render :json => @annotation.jsonld_oa, status: 201, content_type: "application/ld+json", notice: "Annotation #{@annotation.id} was successfully created."
end
}
format.ttl {
accept_return_type = mime_type_from_accept(["application/x-turtle", "text/turtle"])
render :body => @annotation.graph.to_ttl, status: 201, notice: "Annotation #{@annotation.id} was successfully created.", content_type: accept_return_type if accept_return_type }
Expand All @@ -84,7 +91,13 @@ def create
render :body => @annotation.graph.to_rdfxml, status: 201, notice: "Annotation #{@annotation.id} was successfully created.", 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.jsonld_oa, status: 201, notice: "Annotation #{@annotation.id} was successfully created.", content_type: accept_return_type if accept_return_type }
context_url = context_url_from_link ? context_url_from_link : context_url_from_accept
if context_url && context_url == Triannon::JsonldContext::IIIF_CONTEXT_URL
render :json => @annotation.jsonld_iiif, status: 201, notice: "Annotation #{@annotation.id} was successfully created.", content_type: accept_return_type if accept_return_type
else
render :json => @annotation.jsonld_oa, status: 201, notice: "Annotation #{@annotation.id} was successfully created.", 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, notice: "Annotation #{@annotation.id} was successfully created.", content_type: accept_return_type if accept_return_type }
Expand Down
191 changes: 172 additions & 19 deletions spec/controllers/triannon/annotations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,6 @@
expect(response.status).to eq 201
end
context 'jsonld context' do
=begin
context 'included in header' do
it "honors oa generic uri" do
# creates anno properly
fail "test to be implemented"
end
it "honors oa dated uri" do
# creates anno properly
fail "test to be implemented"
end
it "honors iiif uri" do
# creates anno properly
fail "test to be implemented"
end
it "ignores unrecognized uri (looks inline)" do
fail "test to be implemented"
end
end # included in header
=end
context "NOT included in header" do
shared_examples_for 'creates anno successfully' do | test_data |
it "" do
Expand Down Expand Up @@ -261,6 +242,178 @@
expect(response.status).to eql(201)
end
end

context 'jsonld context' do
context 'Accept header profile specifies context URL' do
shared_examples_for 'creates anno successfully' do | mime_type, context_url, result_url |
it "" do
request.accept = "#{mime_type}; profile=\"#{context_url}\""
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql(mime_type)
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
if result_url
expect(response.body).to match result_url
else
expect(response.body).to match context_url
end
end
end
context 'jsonld' do
context 'oa dated' do
it_behaves_like 'creates anno successfully', "application/ld+json", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'oa generic' do
it_behaves_like 'creates anno successfully', "application/ld+json", Triannon::JsonldContext::OA_CONTEXT_URL, Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'iiif' do
it_behaves_like 'creates anno successfully', "application/ld+json", Triannon::JsonldContext::IIIF_CONTEXT_URL
end
it "missing context returns oa dated" do
request.accept = "application/ld+json"
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql("application/ld+json")
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
expect(response.body).to match Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
it "unrecognized context returns oa dated" do
request.accept = "application/ld+json; profile=\"http://not.a.real.doctor.com/\""
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql("application/ld+json")
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
expect(response.body).to match Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
end
context 'json (be nice and pay attention to profile)' do
context 'oa dated' do
it_behaves_like 'creates anno successfully', "application/json", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'oa generic' do
it_behaves_like 'creates anno successfully', "text/x-json", Triannon::JsonldContext::OA_CONTEXT_URL, Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'iiif' do
it_behaves_like 'creates anno successfully', "application/jsonrequest", Triannon::JsonldContext::IIIF_CONTEXT_URL
end
it "missing context returns oa dated" do
request.accept = "application/json"
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql("application/json")
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
expect(response.body).to match Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
end
it "context specified for non-json returns non-json" do
request.accept = "application/x-turtle; profile=\"#{Triannon::JsonldContext::IIIF_CONTEXT_URL}\""
post :create, Triannon.annotation_fixture("body-chars.ttl")
expect(response.status).to eq 201
expect(response.content_type).to eql("application/x-turtle")
expect(response.body).to match /\.\Z/ # \Z is needed instead of $ due to \n in data)
expect(response.body).to match "kq131cs7229"
expect(response.body).not_to match Triannon::JsonldContext::IIIF_CONTEXT_URL
end
end
context 'Link header specifies context URL' do
shared_examples_for 'creates anno successfully' do | mime_type, context_url, result_url |
it "link type specified" do
request.accept = "#{mime_type}"
request.headers["Link"] = "#{context_url}; rel=\"http://www.w3.org/ns/json-ld#context\"; type=\"application/ld+json\""
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql(mime_type)
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
if result_url
expect(response.body).to match result_url
else
expect(response.body).to match context_url
end
end
it "link type not specified" do
request.accept = "#{mime_type}"
request.headers["Link"] = "#{context_url}; rel=\"http://www.w3.org/ns/json-ld#context\""
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql(mime_type)
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
if result_url
expect(response.body).to match result_url
else
expect(response.body).to match context_url
end
end
end
context 'json' do
context 'oa dated' do
it_behaves_like 'creates anno successfully', "application/json", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'oa generic' do
it_behaves_like 'creates anno successfully', "text/x-json", Triannon::JsonldContext::OA_CONTEXT_URL, Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'iiif' do
it_behaves_like 'creates anno successfully', "application/jsonrequest", Triannon::JsonldContext::IIIF_CONTEXT_URL
end
context "unrecognized context returns oa dated" do
it_behaves_like 'creates anno successfully', "application/jsonrequest", "http://context.unknown.org", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context "missing context returns oa dated" do
it_behaves_like 'creates anno successfully', "text/x-json", "", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
it "no link header returns oa dated" do
request.accept = "application/json"
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql("application/json")
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
expect(response.body).to match Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
end
context 'jsonld (be nice and pay attention to link)' do
context 'oa dated' do
it_behaves_like 'creates anno successfully', "application/ld+json", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'oa generic' do
it_behaves_like 'creates anno successfully', "application/ld+json", Triannon::JsonldContext::OA_CONTEXT_URL, Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context 'iiif' do
it_behaves_like 'creates anno successfully', "application/ld+json", Triannon::JsonldContext::IIIF_CONTEXT_URL
end
context "unrecognized context returns oa dated" do
it_behaves_like 'creates anno successfully', "application/ld+json", "http://context.unknown.org", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
context "missing context returns oa dated" do
it_behaves_like 'creates anno successfully', "application/ld+json", "", Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
it "no link header returns oa dated" do
request.accept = "application/ld+json"
post :create, Triannon.annotation_fixture("bookmark.json")
expect(response.status).to eq 201
expect(response.content_type).to eql("application/ld+json")
expect(response.body).to match json_regex
expect(response.body).to match "kq131cs7229"
expect(response.body).to match Triannon::JsonldContext::OA_DATED_CONTEXT_URL
end
end
it "context specified for non-json returns non-json" do
request.accept = "application/x-turtle"
request.headers["Link"] = "#{Triannon::JsonldContext::IIIF_CONTEXT_URL}; rel=\"http://www.w3.org/ns/json-ld#context\""
post :create, Triannon.annotation_fixture("body-chars.ttl")
expect(response.status).to eq 201
expect(response.content_type).to eql("application/x-turtle")
expect(response.body).to match /\.\Z/ # \Z is needed instead of $ due to \n in data)
expect(response.body).to match "kq131cs7229"
expect(response.body).not_to match Triannon::JsonldContext::IIIF_CONTEXT_URL
end
end
end # jsonld context
end # response format

end # #create
Expand Down

0 comments on commit d780b8b

Please sign in to comment.