Skip to content

Commit 542de22

Browse files
committed
Provide JSON-LD format for event/material show endpoints which returns Bioschemas JSON-LD
1 parent 81930b8 commit 542de22

5 files changed

Lines changed: 51 additions & 0 deletions

File tree

app/controllers/events_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def show
6767
format.json
6868
format.json_api { render json: @event }
6969
format.ics { send_data @event.to_ical, type: 'text/calendar', disposition: 'attachment', filename: "#{@event.slug}.ics" }
70+
format.jsonld { render plain: @bioschemas.first.to_json }
7071
end
7172
end
7273

app/controllers/materials_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def show
3535
format.html
3636
format.json
3737
format.json_api { render json: @material }
38+
format.jsonld { render plain: @bioschemas.first.to_json }
3839
end
3940
end
4041

config/initializers/mime_types.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
# Mime::Type.register "text/richtext", :rtf
55

66
Mime::Type.register 'application/vnd.api+json', :json_api
7+
Mime::Type.register 'application/ld+json', :jsonld, ['application/vnd.schemaorg.ld+json']

test/controllers/events_controller_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,31 @@ class EventsControllerTest < ActionController::TestCase
323323
assert_equal 'hybrid', body['data']['attributes']['presence']
324324
end
325325

326+
test 'should show event as bioschemas JSON-LD' do
327+
@event.scientific_topic_uris = ['http://edamontology.org/topic_0654']
328+
@event.materials << @material
329+
@event.collections << @collection
330+
@event.save!
331+
332+
get :show, params: { id: @event, format: :jsonld }
333+
assert_response :success
334+
assert assigns(:event)
335+
336+
body = nil
337+
assert_nothing_raised do
338+
body = JSON.parse(response.body)
339+
end
340+
341+
assert_equal 'http://schema.org', body['@context']
342+
assert_equal 'Course', body['@type']
343+
assert_equal 'https://bioschemas.org/profiles/Course/1.0-RELEASE', body['dct:conformsTo']['@id']
344+
assert_equal @event.title, body['name']
345+
assert_equal @event.url, body['url']
346+
assert_equal @event.scientific_topic_uris.first, body['about'].first['@id']
347+
assert_equal event_url(assigns(:event), host: TeSS::Config.base_url), body['@id']
348+
assert_equal @event.external_resources.first.url, body['mentions'].first['url']
349+
end
350+
326351
# UPDATE TEST
327352
test 'should update event' do
328353
sign_in @event.user

test/controllers/materials_controller_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,29 @@ class MaterialsControllerTest < ActionController::TestCase
398398
assert_equal @collection.id.to_s, body['data']['relationships']['collections']['data'][0]['id']
399399
end
400400

401+
test 'should show material as bioschemas JSON-LD' do
402+
@material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
403+
@material.events << @event
404+
@material.collections << @collection
405+
@material.save!
406+
407+
get :show, params: { id: @material, format: :jsonld }
408+
assert_response :success
409+
assert assigns(:material)
410+
body = nil
411+
assert_nothing_raised do
412+
body = JSON.parse(response.body)
413+
end
414+
415+
assert_equal 'http://schema.org', body['@context']
416+
assert_equal 'LearningResource', body['@type']
417+
assert_equal 'https://bioschemas.org/profiles/TrainingMaterial/1.0-RELEASE', body['dct:conformsTo']['@id']
418+
assert_equal @material.title, body['name']
419+
assert_equal @material.url, body['url']
420+
assert_equal @material.scientific_topic_uris.first, body['about'].first['@id']
421+
assert_equal material_url(assigns(:material), host: TeSS::Config.base_url), body['@id']
422+
end
423+
401424
#UPDATE TEST
402425
test 'should update material' do
403426
sign_in @material.user

0 commit comments

Comments
 (0)