Skip to content

Commit

Permalink
Merge pull request #34 from ncbo/bugfix/edit-account-errors/320
Browse files Browse the repository at this point in the history
Adopt some optimizations implemented by LIRMM in 2023
  • Loading branch information
jvendetti authored Sep 4, 2024
2 parents 927bbe2 + 11861cf commit f589b13
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 50 deletions.
8 changes: 3 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ontologies_api_client (2.3.0)
ontologies_api_client (2.4.0)
activesupport (= 7.0.8)
addressable (~> 2.8)
excon
Expand All @@ -11,7 +11,6 @@ PATH
lz4-ruby
multi_json
oj
spawnling (= 2.1.5)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -47,7 +46,7 @@ GEM
logger (1.6.0)
lz4-ruby (0.3.3)
method_source (1.1.0)
minitest (5.24.1)
minitest (5.25.1)
minitest-hooks (1.5.1)
minitest (> 5.3)
multi_json (1.15.0)
Expand Down Expand Up @@ -84,7 +83,6 @@ GEM
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
spawnling (2.1.5)
strscan (3.1.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -99,7 +97,7 @@ PLATFORMS

DEPENDENCIES
faraday-follow_redirects (~> 0.3)
minitest (~> 5.20)
minitest (~> 5.25)
minitest-hooks (~> 1.5)
ontologies_api_client!
pry
Expand Down
1 change: 0 additions & 1 deletion lib/ontologies_api_client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'oj'
require 'multi_json'
require 'spawnling'

require_relative 'ontologies_api_client/config'
require_relative 'ontologies_api_client/http'
Expand Down
12 changes: 6 additions & 6 deletions lib/ontologies_api_client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def method_missing(meth, *args, &block)
##
# Get all top-level links for the API
def top_level_links
HTTP.get(LinkedData::Client.settings.rest_url)
@top_level_links ||= HTTP.get(LinkedData::Client.settings.rest_url)
end

##
Expand Down Expand Up @@ -73,18 +73,18 @@ def where(params = {}, &block)
end
end

##
# Find a resource by id
#
# @deprecated Use {#get} instead
def find(id, params = {})
found = where do |obj|
obj.id.eql?(id)
end
found.first
get(id, params)
end

##
# Get a resource by id (this will retrieve it from the REST service)
def get(id, params = {})
path = collection_path
id = "#{path}/#{id}" unless id.include?(path)
HTTP.get(id, params)
end

Expand Down
8 changes: 5 additions & 3 deletions lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def values_at(*selectors)
module LinkedData
module Client
module HTTP
$DEBUG_API_CLIENT ||= false

class Link < String
attr_accessor :media_type;
end
Expand All @@ -61,7 +63,7 @@ def self.get(path, params = {}, options = {})
invalidate_cache = params.delete(:invalidate_cache) || false

begin
puts "Getting: #{path} with #{params}" if $DEBUG
puts "Getting: #{path} with #{params}" if $DEBUG_API_CLIENT
begin
response = conn.get do |req|
req.url path
Expand All @@ -87,7 +89,7 @@ def self.get(path, params = {}, options = {})
obj = recursive_struct(load_json(response.body))
end
rescue StandardError => e
puts "Problem getting #{path}" if $DEBUG
puts "Problem getting #{path}" if $DEBUG_API_CLIENT
raise e
end
obj
Expand Down Expand Up @@ -143,7 +145,7 @@ def self.patch(path, obj)
end

def self.delete(id)
puts "Deleting #{id}" if $DEBUG
puts "Deleting #{id}" if $DEBUG_API_CLIENT
response = conn.delete id
raise StandardError, response.body if response.status >= 500

Expand Down
42 changes: 21 additions & 21 deletions lib/ontologies_api_client/models/ontology.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'cgi'
require_relative "../base"
require_relative '../base'

module LinkedData
module Client
Expand All @@ -8,19 +10,19 @@ class Ontology < LinkedData::Client::Base
include LinkedData::Client::Collection
include LinkedData::Client::ReadWrite

@media_type = "http://data.bioontology.org/metadata/Ontology"
@include_attrs = "all"
@media_type = 'http://data.bioontology.org/metadata/Ontology'
@include_attrs = 'all'

def flat?
self.flat
end

def private?
viewingRestriction && viewingRestriction.downcase.eql?("private")
viewingRestriction && viewingRestriction.downcase.eql?('private')
end

def licensed?
viewingRestriction && viewingRestriction.downcase.eql?("licensed")
viewingRestriction && viewingRestriction.downcase.eql?('licensed')
end

def viewing_restricted?
Expand All @@ -35,26 +37,21 @@ def purl
if self.acronym
"#{LinkedData::Client.settings.purl_prefix}/#{acronym}"
else
""
''
end
end

def access?(user)
return true if !viewing_restricted?
return false if user.nil?
return true if user.admin?
return self.full_acl.any? {|u| u == user.id}
return self.full_acl.any? { |u| u == user.id }
end

def admin?(user)
return false if user.nil?
return true if user.admin?
return administeredBy.any? {|u| u == user.id}
end

def invalidate_cache(cache_refresh_all = true)
self.class.all(invalidate_cache: true, include_views: true)
super(cache_refresh_all)
return administeredBy.any? { |u| u == user.id }
end

# ACL with administrators
Expand All @@ -75,12 +72,12 @@ def acl_select
# Method to get the property tree for a given ontology
# Gets the properties from the REST API and then returns a tree
def property_tree
properties = Hash[self.explore.properties.map {|p| [p.id, p]}]
properties.keys.each do |key|
properties = Hash[self.explore.properties.map { |p| [p.id, p] }]
properties.each_key do |key|
prop = properties[key]
prop.parents.each {|par| properties[par].children << prop if properties[par]}
prop.parents.each { |par| properties[par].children << prop if properties[par] }
end
roots = properties.values.select {|p| p.parents.empty?}
roots = properties.values.select { |p| p.parents.empty? }
root = LinkedData::Client::Models::Property.new
root.children = roots
root
Expand All @@ -97,7 +94,7 @@ def self.find_by(attrs, *args)
params[:include_views] = params[:include_views] || true
else
# Stick params back and create a new one
args.push({include_views: true})
args.push({ include_views: true })
end
args.unshift(params)
super(attrs, *args)
Expand All @@ -108,17 +105,20 @@ def self.find_by(attrs, *args)
# Override to search for views as well by default
# Views get hidden on the REST service unless the `include_views`
# parameter is set to `true`
def find(id, params = {})
def self.find(id, params = {})
params[:include_views] = params[:include_views] || true
super(id, params)
end

def self.find_by_acronym(acronym, params = {})
[find(acronym, params)]
end

##
# Include parameters commonly used with ontologies
def self.include_params
"acronym,administeredBy,group,hasDomain,name,notes,projects,reviews,summaryOnly,viewingRestriction"
'acronym,administeredBy,group,hasDomain,name,notes,projects,reviews,summaryOnly,viewingRestriction'
end

end
end
end
Expand Down
11 changes: 0 additions & 11 deletions lib/ontologies_api_client/read_write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,7 @@ def invalidate_cache(cache_refresh_all = true)
HTTP.get(self.id, invalidate_cache: true) if self.id
session = Thread.current[:session]
session[:last_updated] = Time.now.to_f if session
refresh_cache
end

def refresh_cache
Spawnling.new do
LinkedData::Client::Models::Ontology.all
LinkedData::Client::Models::OntologySubmission.all
LinkedData::Client::Models::User.all
exit
end
end

end
end
end
2 changes: 1 addition & 1 deletion lib/ontologies_api_client/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module LinkedData
module Client
VERSION = '2.3.0'
VERSION = '2.4.0'
end
end
3 changes: 1 addition & 2 deletions ontologies_api_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ Gem::Specification.new do |gem|
gem.add_dependency('lz4-ruby')
gem.add_dependency('multi_json')
gem.add_dependency('oj')
gem.add_dependency('spawnling', '2.1.5')

gem.add_development_dependency('faraday-follow_redirects', '~> 0.3')
gem.add_development_dependency('minitest', '~> 5.20')
gem.add_development_dependency('minitest', '~> 5.25')
gem.add_development_dependency('minitest-hooks', '~> 1.5')
end
14 changes: 14 additions & 0 deletions test/models/test_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ def test_find
ont = TestOntology.find('https://data.bioontology.org/ontologies/SNOMEDCT')
refute_nil ont
end

def test_get
ont = TestOntology.get('https://data.bioontology.org/ontologies/SNOMEDCT')
refute_nil ont
assert_instance_of LinkedData::Client::Models::Ontology, ont
assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id
assert_equal 'SNOMEDCT', ont.acronym

ont = TestOntology.get('SNOMEDCT')
refute_nil ont
assert_instance_of LinkedData::Client::Models::Ontology, ont
assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id
assert_equal 'SNOMEDCT', ont.acronym
end
end
31 changes: 31 additions & 0 deletions test/models/test_ontology.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require_relative '../test_case'

class OntologyTest < LinkedData::Client::TestCase
def test_find_by_acronym
result = LinkedData::Client::Models::Ontology.find_by_acronym('SNOMEDCT')
refute_empty result
assert_instance_of Array, result
assert_equal 1, result.length

ont = result.first
assert_instance_of LinkedData::Client::Models::Ontology, ont
assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id
assert_equal 'SNOMEDCT', ont.acronym
end

def test_find
ont = LinkedData::Client::Models::Ontology.find('SNOMEDCT')
refute_nil ont
assert_instance_of LinkedData::Client::Models::Ontology, ont
assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id
assert_equal 'SNOMEDCT', ont.acronym

ont = LinkedData::Client::Models::Ontology.find('BiositemapIM')
refute_nil ont
assert_instance_of LinkedData::Client::Models::Ontology, ont
assert_equal 'https://data.bioontology.org/ontologies/BiositemapIM', ont.id
assert_equal 'BiositemapIM', ont.acronym
end
end

0 comments on commit f589b13

Please sign in to comment.