From a761d4303ad93bd35a6a15545ed0d8f358078319 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Fri, 16 Jun 2023 02:18:08 +0200 Subject: [PATCH 1/3] add identifier model --- lib/ontologies_api_client/models/identifier.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/ontologies_api_client/models/identifier.rb diff --git a/lib/ontologies_api_client/models/identifier.rb b/lib/ontologies_api_client/models/identifier.rb new file mode 100644 index 0000000..4261de3 --- /dev/null +++ b/lib/ontologies_api_client/models/identifier.rb @@ -0,0 +1,14 @@ +require_relative "../base" + +module LinkedData + module Client + module Models + class Identifier < LinkedData::Client::Base + include LinkedData::Client::Collection + include LinkedData::Client::ReadWrite + + @media_type = "http://www.w3.org/ns/adms#Identifier" + end + end + end +end From da65de5ab99c6771d0c8c20a6e9999e4eb14b486 Mon Sep 17 00:00:00 2001 From: sirine mhedhbi Date: Fri, 13 Oct 2023 13:50:36 +0200 Subject: [PATCH 2/3] fix bug of obsolete class --- lib/ontologies_api_client/models/class.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ontologies_api_client/models/class.rb b/lib/ontologies_api_client/models/class.rb index d3bbec2..e249887 100644 --- a/lib/ontologies_api_client/models/class.rb +++ b/lib/ontologies_api_client/models/class.rb @@ -14,12 +14,12 @@ class Class < LinkedData::Client::Base # triple store predicate is def obsolete? - self.obsolete + self.obsolete && self.obsolete.to_s.eql?("true") end def prefLabel(options = {}) if options[:use_html] - if self.obsolete + if obsolete? return "#{@prefLabel}" else return "#{@prefLabel}" From ff695343b6fdf25cbe8c8239aaa8ebcba334dc9a Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Mon, 18 Dec 2023 07:05:36 +0100 Subject: [PATCH 3/3] Fix: optimize the way we do our calls to the API (#14) * use $DEBUG_API_CLIENT instead of $DEBUG for debugging request * add $API_CLIENT_INVALIDATE_CACHE option to disable cache * save and re-use the top_level_links http response as the result do not change * deprecate the find method in favor of get method for better performance * disable the refresh_cache on each update call --- Gemfile.lock | 3 ++- lib/ontologies_api_client/collection.rb | 10 ++++---- lib/ontologies_api_client/http.rb | 26 +++++++++++--------- lib/ontologies_api_client/models/ontology.rb | 9 +++---- lib/ontologies_api_client/read_write.rb | 2 +- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 2d0a48c..77dace9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,6 +54,7 @@ GEM PLATFORMS x86_64-darwin-21 + x86_64-darwin-23 x86_64-linux DEPENDENCIES @@ -63,4 +64,4 @@ DEPENDENCIES test-unit BUNDLED WITH - 2.3.23 + 2.4.21 diff --git a/lib/ontologies_api_client/collection.rb b/lib/ontologies_api_client/collection.rb index 97ccc0f..6e0e36f 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 ## @@ -75,16 +75,16 @@ def where(params = {}, &block) ## # Find a resource by id + # @deprecated replace with "get" 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..d3df241 100644 --- a/lib/ontologies_api_client/http.rb +++ b/lib/ontologies_api_client/http.rb @@ -2,7 +2,7 @@ require 'multi_json' require 'digest' require 'ostruct' - +require 'benchmark' ## # This monkeypatch makes OpenStruct act like Struct objects class OpenStruct @@ -58,18 +58,20 @@ def self.get(path, params = {}, options = {}) raw = options[:raw] || false # return the unparsed body of the request params = params.delete_if { |k, v| v == nil || v.to_s.empty? } params[:ncbo_cache_buster] = Time.now.to_f if raw # raw requests don't get cached to ensure body is available - invalidate_cache = params.delete(:invalidate_cache) || false - + invalidate_cache = params.delete(:invalidate_cache) || $API_CLIENT_INVALIDATE_CACHE || false begin - puts "Getting: #{path} with #{params}" if $DEBUG begin - response = conn.get do |req| - req.url path - req.params = params.dup - req.options[:timeout] = 60 - req.headers.merge(headers) - req.headers[:invalidate_cache] = invalidate_cache + response = nil + time = Benchmark.realtime do + response = conn.get do |req| + req.url path + req.params = params.dup + req.options[:timeout] = 60 + req.headers.merge(headers) + req.headers[:invalidate_cache] = invalidate_cache + end end + puts "Getting: #{path} with #{params} (#{time}s)" if $DEBUG_API_CLIENT rescue Exception => e params = Faraday::Utils.build_query(params) path << "?" unless params.empty? || path.include?("?") @@ -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..5a62a01 100644 --- a/lib/ontologies_api_client/models/ontology.rb +++ b/lib/ontologies_api_client/models/ontology.rb @@ -52,10 +52,6 @@ def admin?(user) 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) - end # ACL with administrators def full_acl @@ -108,7 +104,7 @@ 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 @@ -119,6 +115,9 @@ def self.include_params "acronym,administeredBy,group,hasDomain,name,notes,projects,reviews,summaryOnly,viewingRestriction" end + def self.find_by_acronym(acronym, params = {}) + [find(acronym, params)] + end end end end diff --git a/lib/ontologies_api_client/read_write.rb b/lib/ontologies_api_client/read_write.rb index 721c669..3549bb0 100644 --- a/lib/ontologies_api_client/read_write.rb +++ b/lib/ontologies_api_client/read_write.rb @@ -90,7 +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 + # refresh_cache end def refresh_cache