Skip to content

Commit 23ae179

Browse files
Feature: Add class level explorer to avoid unnecessary calls (#25)
* add class level explorer to avoid unnecessary calls * add link explorer tests * add link explorer forwarding * optimize the top level links call to be done only once * fix test after making top_level_links class variable
1 parent 2e02a09 commit 23ae179

File tree

6 files changed

+97
-22
lines changed

6 files changed

+97
-22
lines changed

Gemfile.lock

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ PATH
1717
GEM
1818
remote: https://rubygems.org/
1919
specs:
20-
activesupport (7.0.8.6)
20+
activesupport (7.0.8.7)
2121
concurrent-ruby (~> 1.0, >= 1.0.2)
2222
i18n (>= 1.6, < 2)
2323
minitest (>= 5.1)
2424
tzinfo (~> 2.0)
2525
addressable (2.8.7)
2626
public_suffix (>= 2.0.2, < 7.0)
27-
bigdecimal (3.1.8)
27+
bigdecimal (3.1.9)
2828
coderay (1.1.3)
29-
concurrent-ruby (1.3.4)
29+
concurrent-ruby (1.3.5)
3030
crack (1.0.0)
3131
bigdecimal
3232
rexml
@@ -37,35 +37,35 @@ GEM
3737
faraday-excon (2.0.0)
3838
excon (>= 0.27.4)
3939
faraday (~> 2.0.0.alpha.pre.2)
40-
faraday-multipart (1.0.4)
41-
multipart-post (~> 2)
40+
faraday-multipart (1.1.0)
41+
multipart-post (~> 2.0)
4242
faraday-net_http (2.1.0)
43-
hashdiff (1.1.1)
44-
i18n (1.14.6)
43+
hashdiff (1.1.2)
44+
i18n (1.14.7)
4545
concurrent-ruby (~> 1.0)
4646
lz4-ruby (0.3.3)
4747
method_source (1.1.0)
48-
minitest (5.25.1)
48+
minitest (5.25.4)
4949
multi_json (1.15.0)
5050
multipart-post (2.4.1)
51-
oj (3.16.6)
51+
oj (3.16.9)
5252
bigdecimal (>= 3.0)
5353
ostruct (>= 0.2)
54-
ostruct (0.6.0)
54+
ostruct (0.6.1)
5555
parallel (1.26.3)
56-
power_assert (2.0.4)
57-
pry (0.14.2)
56+
power_assert (2.0.5)
57+
pry (0.15.2)
5858
coderay (~> 1.1)
5959
method_source (~> 1.0)
6060
public_suffix (5.1.1)
61-
rack (3.1.8)
61+
rack (3.1.9)
6262
rake (13.2.1)
6363
request_store (1.7.0)
6464
rack (>= 1.4)
65-
rexml (3.3.9)
65+
rexml (3.4.0)
6666
ruby2_keywords (0.0.5)
6767
spawnling (2.1.5)
68-
test-unit (3.6.2)
68+
test-unit (3.6.7)
6969
power_assert
7070
tzinfo (2.0.6)
7171
concurrent-ruby (~> 1.0)

lib/ontologies_api_client/base.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def type
6666
@type
6767
end
6868

69+
def self.explore(id)
70+
path = self.respond_to?(:collection_path) ? collection_path : ''
71+
id = "#{path}/#{id}" unless id.include?(path)
72+
inst = self.new(values: {id: id})
73+
LinkedData::Client::LinkExplorer.new({}, inst)
74+
end
75+
6976
##
7077
# Retrieve a set of data using a link provided on an object
7178
# This instantiates an instance of this class and uses

lib/ontologies_api_client/collection.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ def method_missing(meth, *args, &block)
2929
##
3030
# Get all top-level links for the API
3131
def top_level_links(link = LinkedData::Client.settings.rest_url)
32-
HTTP.get(link)
32+
@top_level_links ||= {}
33+
@top_level_links[link] ||= HTTP.get(link)
3334
end
3435

3536
##
3637
# Return a link given an object (with links) and a media type
3738
def uri_from_context(object, media_type)
3839
object.links.each do |type, link|
39-
return link if link.media_type && link.media_type.downcase.eql?(media_type.downcase)
40+
return link.dup if link.media_type && link.media_type.downcase.eql?(media_type.downcase)
4041
end
4142
end
4243

lib/ontologies_api_client/link_explorer.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ def initialize(links, instance)
1111
@instance = instance
1212
end
1313

14+
15+
def get(params = {})
16+
get_link(@instance.id, params)
17+
end
18+
1419
def method_missing(meth, *args, &block)
1520
if combined_links.key?(meth.to_s)
1621
explore_link(meth, *args)
1722
elsif meth == :batch
1823
explore_link(args)
24+
elsif !@instance.id.blank?
25+
forward_explore(meth, *args)
1926
else
2027
super
2128
end
@@ -43,10 +50,7 @@ def explore_link(*args)
4350
ids = link.map {|l| l.to_s}
4451
value_cls.where {|o| ids.include?(o.id)}
4552
else
46-
url = replace_template_elements(link.to_s, replacements)
47-
value_cls = LinkedData::Client::Base.class_for_type(link.media_type)
48-
params[:include] ||= value_cls.attributes(full_attributes)
49-
HTTP.get(url, params)
53+
get_link(link, params, replacements, full_attributes)
5054
end
5155
end
5256

@@ -56,6 +60,22 @@ def combined_links
5660

5761
private
5862

63+
def forward_explore(meth, *args)
64+
sub_id = Array(args).find { |x| x.is_a?(String) } || ''
65+
link = "#{@instance.id}/#{meth}/#{CGI.escape(sub_id)}".chomp('/')
66+
@instance.id = link
67+
LinkExplorer.new(@links, @instance)
68+
end
69+
70+
def get_link(link, params, replacements = [], full_attributes = {})
71+
url = replace_template_elements(link.to_s, replacements)
72+
if link.respond_to? :media_type
73+
value_cls = LinkedData::Client::Base.class_for_type(link.media_type)
74+
params[:include] ||= value_cls.attributes(full_attributes)
75+
end
76+
HTTP.get(url, params)
77+
end
78+
5979
def replace_template_elements(url, values = [])
6080
return url if values.nil? || values.empty?
6181
values = values.dup
@@ -94,4 +114,4 @@ def linkable_attributes
94114
end
95115
end
96116
end
97-
end
117+
end

mise.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tools]
2+
ruby = "2.7.8"

test/models/test_explore.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require_relative '../test_case'
2+
require 'pry'
3+
4+
module Models
5+
def self.method_missing
6+
binding.pry
7+
end
8+
end
9+
class LinkExploreTest < LinkedData::Client::TestCase
10+
11+
def test_explore
12+
sub_direct_explore = LinkedData::Client::Models::Ontology.explore('MEDDRA')
13+
.latest_submission
14+
.get(include: 'all')
15+
16+
sub_indirect_explore = LinkedData::Client::Models::Ontology.find('MEDDRA').explore.latest_submission
17+
sub_direct_explore.to_hash.each do |key, value|
18+
value_to_compare = sub_indirect_explore[key]
19+
if value.class.ancestors.include?(LinkedData::Client::Base)
20+
value = value.to_hash
21+
value_to_compare = value_to_compare.to_hash
22+
end
23+
assert_equal value, value_to_compare
24+
end
25+
end
26+
27+
def test_explore_class
28+
29+
id = 'http://purl.org/sig/ont/fma/fma62955'
30+
cls = LinkedData::Client::Models::Ontology.explore('FMA')
31+
.classes(id)
32+
.children
33+
.get
34+
35+
assert_not_empty cls.collection
36+
37+
cls = LinkedData::Client::Models::Ontology.explore('FMA')
38+
.classes(id)
39+
.children
40+
.get(include: 'prefLabel')
41+
42+
assert_not_empty cls.collection
43+
end
44+
45+
end

0 commit comments

Comments
 (0)