Skip to content

Commit

Permalink
Merge branch 'features/add-submission-ontology-metadata-endpoint'
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Dec 21, 2022
2 parents 1d86cee + 32f3f16 commit f3a62f5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 80 deletions.
89 changes: 9 additions & 80 deletions controllers/submission_metadata_controller.rb
Original file line number Diff line number Diff line change
@@ -1,87 +1,16 @@
class SubmissionMetadataController < ApplicationController

namespace "/submission_metadata" do

##
# Display all metadata for submissions
get do
all_attr = []

LinkedData::Models::OntologySubmission.attributes(:all).each do |attr|

if LinkedData.settings.id_url_prefix.nil? || LinkedData.settings.id_url_prefix.empty?
id_url_prefix = "http://data.bioontology.org/"
else
id_url_prefix = LinkedData.settings.id_url_prefix
end

attr_settings = {}
attr_settings[:@id] = "#{id_url_prefix}submission_metadata/#{attr.to_s}"
attr_settings[:@type] = "#{id_url_prefix}metadata/SubmissionMetadata"
attr_settings[:attribute] = attr.to_s

# Get metadata namespace
if LinkedData::Models::OntologySubmission.attribute_settings(attr)[:namespace].nil?
attr_settings[:namespace] = nil
else
attr_settings[:namespace] = LinkedData::Models::OntologySubmission.attribute_settings(attr)[:namespace].to_s
end

# Get metadata label if one
if LinkedData::Models::OntologySubmission.attribute_settings(attr)[:label].nil?
attr_settings[:label] = nil
else
attr_settings[:label] = LinkedData::Models::OntologySubmission.attribute_settings(attr)[:label]
end

# Get if it is an extracted metadata
if LinkedData::Models::OntologySubmission.attribute_settings(attr)[:extractedMetadata]
attr_settings[:extracted] = true
else
attr_settings[:extracted] = false
end

# Get mappings of the metadata
if LinkedData::Models::OntologySubmission.attribute_settings(attr)[:metadataMappings].nil?
attr_settings[:metadataMappings] = nil
else
attr_settings[:metadataMappings] = LinkedData::Models::OntologySubmission.attribute_settings(attr)[:metadataMappings]
end

# Get enforced from the metadata
if LinkedData::Models::OntologySubmission.attribute_settings(attr)[:enforce].nil?
attr_settings[:enforce] = []
else
attr_settings[:enforce] = []
LinkedData::Models::OntologySubmission.attribute_settings(attr)[:enforce].each do |enforced|
attr_settings[:enforce] << enforced.to_s
end
end

# Get enforcedValues from the metadata
attr_settings[:enforcedValues] = LinkedData::Models::OntologySubmission.attribute_settings(attr)[:enforcedValues]

# Get display from the metadata
if LinkedData::Models::OntologySubmission.attribute_settings(attr)[:display].nil?
attr_settings[:display] = "no"
else
attr_settings[:display] = LinkedData::Models::OntologySubmission.attribute_settings(attr)[:display]
end

if !LinkedData::Models::OntologySubmission.attribute_settings(attr)[:helpText].nil?
attr_settings[:helpText] = LinkedData::Models::OntologySubmission.attribute_settings(attr)[:helpText]
end

attr_settings[:@context] = {
"@vocab" => "#{id_url_prefix}metadata/"
}

all_attr << attr_settings
end
##
# Display all metadata for submissions
get "/submission_metadata" do
reply klass_metadata(LinkedData::Models::OntologySubmission, "submission_metadata")
end

reply all_attr
end

##
# Display all metadata for ontologies
get "/ontology_metadata" do
reply klass_metadata(LinkedData::Models::Ontology, "ontology_metadata")
end

end
89 changes: 89 additions & 0 deletions helpers/metadata_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'sinatra/base'
require 'date'

module Sinatra
module Helpers
module MetadataHelper

def klass_metadata(klass, type)
all_attr = []
klass.attributes(:all).each do |attr|

if LinkedData.settings.id_url_prefix.nil? || LinkedData.settings.id_url_prefix.empty?
id_url_prefix = "http://data.bioontology.org/"
else
id_url_prefix = LinkedData.settings.id_url_prefix
end

attr_settings = {}
attr_settings[:@id] = "#{id_url_prefix}#{type}/#{attr.to_s}"
attr_settings[:@type] = "#{id_url_prefix}metadata/#{type.camelize}"
attr_settings[:attribute] = attr.to_s

# Get metadata namespace
if klass.attribute_settings(attr)[:namespace].nil?
attr_settings[:namespace] = nil
else
attr_settings[:namespace] = klass.attribute_settings(attr)[:namespace].to_s
end

# Get metadata label if one
if klass.attribute_settings(attr)[:label].nil?
attr_settings[:label] = nil
else
attr_settings[:label] = klass.attribute_settings(attr)[:label]
end

# Get if it is an extracted metadata
if klass.attribute_settings(attr)[:extractedMetadata]
attr_settings[:extracted] = true
else
attr_settings[:extracted] = false
end

# Get mappings of the metadata
if klass.attribute_settings(attr)[:metadataMappings].nil?
attr_settings[:metadataMappings] = nil
else
attr_settings[:metadataMappings] = klass.attribute_settings(attr)[:metadataMappings]
end

# Get enforced from the metadata
if klass.attribute_settings(attr)[:enforce].nil?
attr_settings[:enforce] = []
else
attr_settings[:enforce] = []
klass.attribute_settings(attr)[:enforce].each do |enforced|
next if enforced.is_a? Proc
attr_settings[:enforce] << enforced.to_s
end
end

# Get enforcedValues from the metadata
attr_settings[:enforcedValues] = klass.attribute_settings(attr)[:enforcedValues]

# Get display from the metadata
if klass.attribute_settings(attr)[:display].nil?
attr_settings[:display] = "no"
else
attr_settings[:display] = klass.attribute_settings(attr)[:display]
end

if !klass.attribute_settings(attr)[:helpText].nil?
attr_settings[:helpText] = klass.attribute_settings(attr)[:helpText]
end

attr_settings[:@context] = {
"@vocab" => "#{id_url_prefix}metadata/"
}

all_attr << attr_settings
end
all_attr
end
end
end
end

helpers Sinatra::Helpers::MetadataHelper

0 comments on commit f3a62f5

Please sign in to comment.