forked from ontoportal/ontologies_api
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclasses_helper.rb
85 lines (71 loc) · 2.62 KB
/
classes_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'sinatra/base'
module Sinatra
module Helpers
module ClassesHelper
def notation_to_class_uri(submission)
params ||= @params
if params[:cls] && !params[:cls].start_with?("http")
notation_lookup = LinkedData::Models::Class.where(
notation: RDF::Literal.new(params[:cls], :datatype => RDF::XSD.string))
.in(submission).first
if notation_lookup
cls_uri = notation_lookup.id
return cls_uri
end
prefix_lookup = LinkedData::Models::Class.where(
prefixIRI: RDF::Literal.new(params[:cls], :datatype => RDF::XSD.string))
.in(submission).first
if prefix_lookup
cls_uri = prefix_lookup.id
return cls_uri
end
end
return nil
end
def get_class(submission, load_attrs=nil)
load_attrs = load_attrs || LinkedData::Models::Class.goo_attrs_to_load(includes_param)
load_children = load_attrs.delete :children
load_has_children = load_attrs.delete :hasChildren
if !load_children
load_children = load_attrs.select { |x| x.instance_of?(Hash) && x.include?(:children) }
if load_children.length == 0
load_children = nil
end
if !load_children.nil?
load_attrs = load_attrs.select { |x| !(x.instance_of?(Hash) && x.include?(:children)) }
end
end
cls_uri = notation_to_class_uri(submission)
if cls_uri.nil?
cls_uri = RDF::URI.new(params[:cls])
if !cls_uri.valid?
error 400, "The input class id '#{params[:cls]}' is not a valid IRI"
end
end
aggregates = LinkedData::Models::Class.goo_aggregates_to_load(load_attrs)
cls = LinkedData::Models::Class.find(cls_uri).in(submission)
cls = cls.include(load_attrs) if load_attrs && load_attrs.length > 0
cls.aggregate(*aggregates) unless aggregates.empty?
cls = cls.first
if cls.nil?
error 404,
"Resource '#{params[:cls]}' not found in ontology #{submission.ontology.acronym} submission #{submission.submissionId}"
end
unless load_has_children.nil?
cls.load_has_children
end
if !load_children.nil?
LinkedData::Models::Class.partially_load_children(
[cls],500,cls.submission)
unless load_has_children.nil?
cls.children.each do |c|
c.load_has_children
end
end
end
return cls
end
end
end
end
helpers Sinatra::Helpers::ClassesHelper