diff --git a/controllers/submission_metadata_controller.rb b/controllers/submission_metadata_controller.rb index a88492f8..db6fbb78 100644 --- a/controllers/submission_metadata_controller.rb +++ b/controllers/submission_metadata_controller.rb @@ -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 \ No newline at end of file diff --git a/helpers/metadata_helper.rb b/helpers/metadata_helper.rb new file mode 100644 index 00000000..db61c414 --- /dev/null +++ b/helpers/metadata_helper.rb @@ -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 +