Skip to content

Commit 333ebf2

Browse files
authored
Merge pull request #1167 from ElixirTeSS/bioschemas-api
Allow direct access to Bioschemas JSON-LD
2 parents e40562a + 347d94a commit 333ebf2

7 files changed

Lines changed: 82 additions & 11 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']

lib/bioschemas/generator.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ def self.address(event)
9696
'addressRegion' => event.county,
9797
'addressCountry' => event.country,
9898
'postalCode' => event.postcode
99-
}.compact,
99+
}.compact_blank,
100100
'latitude' => event.latitude,
101101
'longitude' => event.longitude
102-
}.compact
102+
}.compact_blank
103103
end
104104

105105
# Reverse the process used by TeSS_RDF_Extractors to turn a list of values into a markdown list.
@@ -137,15 +137,6 @@ def self.external_resources(resource)
137137
def self.provider(resource)
138138
p = []
139139

140-
if resource.respond_to?(:host_institutions)
141-
resource.host_institutions.each do |i|
142-
p << {
143-
'@type' => 'Organization',
144-
'name' => i
145-
}
146-
end
147-
end
148-
149140
if resource.content_provider
150141
p << {
151142
'@type' => 'Organization',
@@ -162,6 +153,17 @@ def self.provider(resource)
162153
}
163154
end
164155

156+
if resource.respond_to?(:host_institutions)
157+
resource.host_institutions.each do |i|
158+
p << {
159+
'@type' => 'Organization',
160+
'name' => i
161+
}
162+
end
163+
end
164+
165+
p.uniq! { |o| o['name'].downcase.strip }
166+
165167
p.any? ? p : nil
166168
end
167169
end

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

test/models/event_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,4 +757,22 @@ class EventTest < ActiveSupport::TestCase
757757
refute event.valid?
758758
assert event.errors.added?(:keywords, :too_long, count: 20)
759759
end
760+
761+
test 'blank address fields are removed from event bioschemas json' do
762+
e = Event.new(city: 'Manchester', country: '', event_types: ['workshops_and_courses'])
763+
bioschemas = e.to_bioschemas.first.generate
764+
address = bioschemas[:hasCourseInstance].first[:location]['address']
765+
assert_equal 'Manchester', address['addressLocality']
766+
assert_equal ['@type', 'addressLocality'].sort, address.keys.sort
767+
end
768+
769+
test 'de-duplicates provider in event bioschemas json' do
770+
e = Event.new(content_provider: content_providers(:goblet), host_institutions: 'Goblet',
771+
event_types: ['workshops_and_courses'])
772+
773+
bioschemas = e.to_bioschemas.first.generate
774+
assert_equal 1, bioschemas[:provider].length
775+
assert_equal 'Goblet', bioschemas[:provider].first['name']
776+
assert_equal 'http://mygoblet.org', bioschemas[:provider].first['url']
777+
end
760778
end

0 commit comments

Comments
 (0)