Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ gemspec
gem 'rake'
gem 'pry'
gem 'test-unit'
gem 'webmock', require: false
gem 'webmock', require: false
30 changes: 15 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.8.6)
activesupport (7.0.8.7)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
bigdecimal (3.1.8)
bigdecimal (3.1.9)
coderay (1.1.3)
concurrent-ruby (1.3.4)
concurrent-ruby (1.3.5)
crack (1.0.0)
bigdecimal
rexml
Expand All @@ -37,35 +37,35 @@ GEM
faraday-excon (2.0.0)
excon (>= 0.27.4)
faraday (~> 2.0.0.alpha.pre.2)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-multipart (1.1.0)
multipart-post (~> 2.0)
faraday-net_http (2.1.0)
hashdiff (1.1.1)
i18n (1.14.6)
hashdiff (1.1.2)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
lz4-ruby (0.3.3)
method_source (1.1.0)
minitest (5.25.1)
minitest (5.25.4)
multi_json (1.15.0)
multipart-post (2.4.1)
oj (3.16.6)
oj (3.16.9)
bigdecimal (>= 3.0)
ostruct (>= 0.2)
ostruct (0.6.0)
ostruct (0.6.1)
parallel (1.26.3)
power_assert (2.0.4)
pry (0.14.2)
power_assert (2.0.5)
pry (0.15.2)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.1.1)
rack (3.1.8)
rack (3.1.9)
rake (13.2.1)
request_store (1.7.0)
rack (>= 1.4)
rexml (3.3.9)
rexml (3.4.0)
ruby2_keywords (0.0.5)
spawnling (2.1.5)
test-unit (3.6.2)
test-unit (3.6.7)
power_assert
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand Down
7 changes: 7 additions & 0 deletions lib/ontologies_api_client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def type
@type
end

def self.explore(id)
path = self.respond_to?(:collection_path) ? collection_path : ''
id = "#{path}/#{id}" unless id.include?(path)
inst = self.new(values: {id: id})
LinkedData::Client::LinkExplorer.new({}, inst)
end

##
# Retrieve a set of data using a link provided on an object
# This instantiates an instance of this class and uses
Expand Down
5 changes: 3 additions & 2 deletions lib/ontologies_api_client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def method_missing(meth, *args, &block)
##
# Get all top-level links for the API
def top_level_links(link = LinkedData::Client.settings.rest_url)
HTTP.get(link)
@top_level_links ||= {}
@top_level_links[link] ||= HTTP.get(link)
end

##
# Return a link given an object (with links) and a media type
def uri_from_context(object, media_type)
object.links.each do |type, link|
return link if link.media_type && link.media_type.downcase.eql?(media_type.downcase)
return link.dup if link.media_type && link.media_type.downcase.eql?(media_type.downcase)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def self.get(path, params = {}, options = {})
response = connection.get do |req|
req.url path
req.params = params.dup
req.options[:timeout] = 60
req.options[:timeout] = 30
req.headers.merge(headers)
req.headers[:invalidate_cache] = invalidate_cache
end
Expand Down
30 changes: 25 additions & 5 deletions lib/ontologies_api_client/link_explorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ def initialize(links, instance)
@instance = instance
end


def get(params = {})
get_link(@instance.id, params)
end

def method_missing(meth, *args, &block)
if combined_links.key?(meth.to_s)
explore_link(meth, *args)
elsif meth == :batch
explore_link(args)
elsif !@instance.id.blank?
forward_explore(meth, *args)
else
super
end
Expand Down Expand Up @@ -43,10 +50,7 @@ def explore_link(*args)
ids = link.map {|l| l.to_s}
value_cls.where {|o| ids.include?(o.id)}
else
url = replace_template_elements(link.to_s, replacements)
value_cls = LinkedData::Client::Base.class_for_type(link.media_type)
params[:include] ||= value_cls.attributes(full_attributes)
HTTP.get(url, params)
get_link(link, params, replacements, full_attributes)
end
end

Expand All @@ -56,6 +60,22 @@ def combined_links

private

def forward_explore(meth, *args)
sub_id = Array(args).find { |x| x.is_a?(String) } || ''
link = "#{@instance.id}/#{meth}/#{CGI.escape(sub_id)}".chomp('/')
@instance.id = link
LinkExplorer.new(@links, @instance)
end

def get_link(link, params, replacements = [], full_attributes = {})
url = replace_template_elements(link.to_s, replacements)
if link.respond_to? :media_type
value_cls = LinkedData::Client::Base.class_for_type(link.media_type)
params[:include] ||= value_cls.attributes(full_attributes)
end
HTTP.get(url, params)
end

def replace_template_elements(url, values = [])
return url if values.nil? || values.empty?
values = values.dup
Expand Down Expand Up @@ -94,4 +114,4 @@ def linkable_attributes
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/ontologies_api_client/request_federation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ def federated_get(params = {}, &link)
main_thread_locals = Thread.current.keys.map { |key| [key, Thread.current[key]] }.to_h

connections = Parallel.map(portals, in_threads: portals.size) do |conn|
portal_name = portal_name_from_id(conn.url_prefix.to_s)
portal_status = true

unless Rails.cache.read("federation_portal_up_#{portal_name}").nil?
portal_status = Rails.cache.read("federation_portal_up_#{portal_name}")
end

unless portal_status
next [OpenStruct.new(errors: "Problem retrieving #{portal_name}")]
end

main_thread_locals.each { |key, value| Thread.current[key] = value }
begin
HTTP.get(link.call(conn.url_prefix.to_s.chomp('/')), params, connection: conn)
rescue Exception => e
Rails.cache.write("federation_portal_up_#{portal_name}", false, expires_in: 10.minutes)
[OpenStruct.new(errors: "Problem retrieving #{link.call(conn.url_prefix.to_s.chomp('/')) || conn.url_prefix}")]
end
end
Expand All @@ -42,6 +54,11 @@ def request_portals(params = {})

portals
end


def portal_name_from_id(id)
LinkedData::Client::HTTP.federated_conn.find { |_, value| value.url_prefix.to_s.eql?(id) }&.first || ''
end
end

end
Expand Down
2 changes: 2 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
ruby = "2.7.8"
45 changes: 45 additions & 0 deletions test/models/test_explore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative '../test_case'
require 'pry'

module Models
def self.method_missing
binding.pry
end
end
class LinkExploreTest < LinkedData::Client::TestCase

def test_explore
sub_direct_explore = LinkedData::Client::Models::Ontology.explore('MEDDRA')
.latest_submission
.get(include: 'all')

sub_indirect_explore = LinkedData::Client::Models::Ontology.find('MEDDRA').explore.latest_submission
sub_direct_explore.to_hash.each do |key, value|
value_to_compare = sub_indirect_explore[key]
if value.class.ancestors.include?(LinkedData::Client::Base)
value = value.to_hash
value_to_compare = value_to_compare.to_hash
end
assert_equal value, value_to_compare
end
end

def test_explore_class

id = 'http://purl.org/sig/ont/fma/fma62955'
cls = LinkedData::Client::Models::Ontology.explore('FMA')
.classes(id)
.children
.get

assert_not_empty cls.collection

cls = LinkedData::Client::Models::Ontology.explore('FMA')
.classes(id)
.children
.get(include: 'prefLabel')

assert_not_empty cls.collection
end

end
8 changes: 2 additions & 6 deletions test/models/test_federation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_federated_ontologies_all

refute_equal ontologies.length, ontologies_federate_all.length

ontologies_federate_all.group_by{|x| x.id.split('/')[0..-2].join('/')}.each do |portal, onts|
ontologies_federate_all.reject{|x| x.errors}.group_by{|x| x.id.split('/')[0..-2].join('/')}.each do |portal, onts|
puts "#{portal} ontologies: #{onts.length}"
end

Expand All @@ -45,10 +45,6 @@ def test_federated_ontologies_all
puts "Federated ontologies with two portal only with cache: #{ontologies_federate_two.length} in #{time2}s"

refute_equal ontologies_federate_two.size, ontologies_federate_all.size

federated_portals = ontologies_federate_two.map{|x| x.id.split('/')[0..-2].join('/')}.uniq
assert_equal 3, federated_portals.size
assert %w[bioontology ecoportal biodivportal].all? { |p| federated_portals.any?{|id| id[p]} }
end

def test_federated_submissions_all
Expand All @@ -68,7 +64,7 @@ def test_federated_submissions_all

refute_equal onts.length, onts_federate.length

onts_federate.group_by{|x| x.id.split('/')[0..-4].join('/')}.each do |portal, onts|
onts_federate.reject{|x| x.errors}.group_by{|x| x.id.split('/')[0..-4].join('/')}.each do |portal, onts|
puts "#{portal} submissions: #{onts.length}"
end

Expand Down
12 changes: 12 additions & 0 deletions test/test_case.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
require 'test-unit'
require 'logger'
require 'active_support'
require 'active_support/logger'
require 'active_support/cache'
require_relative '../lib/ontologies_api_client'
require_relative '../config/config'
require 'webmock'

WebMock.allow_net_connect!

Logger = ::Logger unless defined?(Logger)
module Rails
def self.cache
@cache ||= ActiveSupport::Cache::MemoryStore.new
end
end

module LinkedData
module Client
class TestCase < Test::Unit::TestCase
Expand Down