diff --git a/Gemfile.lock b/Gemfile.lock index 74fb749..4cb70a6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -11,7 +11,6 @@ PATH lz4-ruby multi_json oj - spawnling (= 2.1.5) GEM remote: https://rubygems.org/ @@ -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) @@ -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) @@ -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 diff --git a/lib/ontologies_api_client.rb b/lib/ontologies_api_client.rb index 4bd0a86..a08dbb1 100644 --- a/lib/ontologies_api_client.rb +++ b/lib/ontologies_api_client.rb @@ -1,6 +1,5 @@ require 'oj' require 'multi_json' -require 'spawnling' require_relative 'ontologies_api_client/config' require_relative 'ontologies_api_client/http' diff --git a/lib/ontologies_api_client/collection.rb b/lib/ontologies_api_client/collection.rb index 97ccc0f..f0bb0f2 100644 --- a/lib/ontologies_api_client/collection.rb +++ b/lib/ontologies_api_client/collection.rb @@ -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 ## @@ -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 diff --git a/lib/ontologies_api_client/http.rb b/lib/ontologies_api_client/http.rb index 8b736eb..b0a4355 100644 --- a/lib/ontologies_api_client/http.rb +++ b/lib/ontologies_api_client/http.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/ontologies_api_client/models/ontology.rb b/lib/ontologies_api_client/models/ontology.rb index 5db6ed4..f595ae2 100644 --- a/lib/ontologies_api_client/models/ontology.rb +++ b/lib/ontologies_api_client/models/ontology.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'cgi' -require_relative "../base" +require_relative '../base' module LinkedData module Client @@ -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? @@ -35,7 +37,7 @@ def purl if self.acronym "#{LinkedData::Client.settings.purl_prefix}/#{acronym}" else - "" + '' end end @@ -43,18 +45,13 @@ 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 @@ -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 @@ -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) @@ -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 diff --git a/lib/ontologies_api_client/read_write.rb b/lib/ontologies_api_client/read_write.rb index 721c669..97157fa 100644 --- a/lib/ontologies_api_client/read_write.rb +++ b/lib/ontologies_api_client/read_write.rb @@ -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 diff --git a/lib/ontologies_api_client/version.rb b/lib/ontologies_api_client/version.rb index c17f0c9..ef2e69e 100644 --- a/lib/ontologies_api_client/version.rb +++ b/lib/ontologies_api_client/version.rb @@ -2,6 +2,6 @@ module LinkedData module Client - VERSION = '2.3.0' + VERSION = '2.4.0' end end diff --git a/ontologies_api_client.gemspec b/ontologies_api_client.gemspec index 9c4411b..8e2ec66 100644 --- a/ontologies_api_client.gemspec +++ b/ontologies_api_client.gemspec @@ -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 diff --git a/test/models/test_collection.rb b/test/models/test_collection.rb index 03c5993..d97cd79 100644 --- a/test/models/test_collection.rb +++ b/test/models/test_collection.rb @@ -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 diff --git a/test/models/test_ontology.rb b/test/models/test_ontology.rb new file mode 100644 index 0000000..c865d41 --- /dev/null +++ b/test/models/test_ontology.rb @@ -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