Skip to content

Commit

Permalink
add class level explorer to avoid unnecessary calls
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Jan 26, 2025
1 parent 2e02a09 commit 6608b2d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
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)
public_suffix (6.0.1)
rack (3.1.8)
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 = 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
19 changes: 14 additions & 5 deletions lib/ontologies_api_client/link_explorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def method_missing(meth, *args, &block)
explore_link(meth, *args)
elsif meth == :batch
explore_link(args)
elsif !@instance.id.blank?
link = "#{@instance.id}/#{meth}"
get_link(link, args.first)
else
super
end
Expand Down Expand Up @@ -43,10 +46,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 +56,15 @@ def combined_links

private

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 +103,4 @@ def linkable_attributes
end
end
end
end
end
Empty file added test/models/test_explore.rb
Empty file.

0 comments on commit 6608b2d

Please sign in to comment.