Skip to content

Commit

Permalink
Merge pull request #1 from lifewatch-eric/ecoportal-ontoportal-reset
Browse files Browse the repository at this point in the history
Ontoportal align: Ecoportal ontoportal reset
  • Loading branch information
syphax-bouazzouni authored Sep 23, 2023
2 parents 1b0fce6 + 90bb0bc commit d0e5d81
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 106 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ name: Ruby

on:
push:
pull_request:
pull_request_target:
types: [opened, reopened]

jobs:
test:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby-version: ['2.7', '3.0']
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.bundle/

*.gem
.DS_Store
config/config.rb
Expand Down
48 changes: 22 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
ontologies_api_client (2.0.3)
activesupport (= 6.0.4.1)
ontologies_api_client (2.2.0)
activesupport
excon
faraday
faraday-excon (~> 2.0.0)
Expand All @@ -15,50 +15,46 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.4.1)
activesupport (7.0.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
coderay (1.1.3)
concurrent-ruby (1.1.9)
excon (0.91.0)
concurrent-ruby (1.1.10)
excon (0.95.0)
faraday (2.0.1)
faraday-net_http (~> 2.0)
ruby2_keywords (>= 0.0.4)
faraday-excon (2.0.0)
excon (>= 0.27.4)
faraday (~> 2.0.0.alpha.pre.2)
faraday-multipart (1.0.3)
multipart-post (>= 1.2, < 3)
faraday-net_http (2.0.1)
i18n (1.10.0)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (2.1.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
lz4-ruby (0.3.3)
method_source (1.0.0)
minitest (5.15.0)
minitest (5.16.3)
multi_json (1.15.0)
multipart-post (2.1.1)
oj (3.13.11)
power_assert (2.0.1)
multipart-post (2.2.3)
oj (3.13.23)
power_assert (2.0.2)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
rake (13.0.6)
ruby2_keywords (0.0.5)
spawnling (2.1.5)
test-unit (3.5.3)
test-unit (3.5.7)
power_assert
thread_safe (0.3.6)
tzinfo (1.2.10)
thread_safe (~> 0.1)
zeitwerk (2.5.4)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)

PLATFORMS
ruby
x86_64-darwin-16
x86_64-darwin-17
x86_64-darwin-21
x86_64-linux

DEPENDENCIES
ontologies_api_client!
Expand All @@ -67,4 +63,4 @@ DEPENDENCIES
test-unit

BUNDLED WITH
2.1.4
2.3.23
3 changes: 2 additions & 1 deletion config/config.test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# config.rb is required for testing
# unit test makes calls to bioportal api so it needs a valid API key which can
# be set via ENV variable UT_APIKEY
abort('env variable UT_APIKEY is not set. Canceling tests...') unless ENV.include?('UT_APIKEY')
abort('UT_APIKEY env variable is not set. Canceling tests') unless ENV.include?('UT_APIKEY')
abort('UT_APIKEY env variable is set to an empty value. Canceling tests') unless ENV['UT_APIKEY'].size > 5

LinkedData::Client.config do |config|
config.rest_url = 'https://data.bioontology.org'
Expand Down
17 changes: 14 additions & 3 deletions lib/ontologies_api_client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ class Base
attr_accessor :context, :links

class << self
attr_accessor :media_type, :act_as_media_type, :include_attrs, :include_attrs_full, :attrs_always_present
attr_accessor :act_as_media_type, :include_attrs, :include_attrs_full, :attrs_always_present
def media_types
Array(@media_type)
end

def media_type
media_types.first
end

end

##
Expand All @@ -31,8 +39,11 @@ def self.map_classes
map = {}
classes = LinkedData::Client::Models.constants.map {|c| LinkedData::Client::Models.const_get(c)}
classes.each do |media_type_cls|
next if map[media_type_cls] || !media_type_cls.respond_to?(:media_type) || !media_type_cls.ancestors.include?(LinkedData::Client::Base)
map[media_type_cls.media_type] = media_type_cls
next if map[media_type_cls] || !media_type_cls.respond_to?(:media_types) || !media_type_cls.ancestors.include?(LinkedData::Client::Base)
media_type_cls.media_types.each do |type|
next if map[type]
map[type] = media_type_cls
end
media_type_cls.act_as_media_type.each {|mt| map[mt] = media_type_cls} if media_type_cls.act_as_media_type
end
return map
Expand Down
58 changes: 33 additions & 25 deletions lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def members
def length
@table.keys.length
end

alias :size :length

def to_a
Expand All @@ -37,7 +38,9 @@ def values_at(*selectors)
module LinkedData
module Client
module HTTP
class Link < String; attr_accessor :media_type; end
class Link < String
attr_accessor :media_type;
end

def self.conn
unless LinkedData::Client.connection_configured?
Expand All @@ -53,7 +56,7 @@ def self.conn
def self.get(path, params = {}, options = {})
headers = options[:headers] || {}
raw = options[:raw] || false # return the unparsed body of the request
params = params.delete_if {|k,v| v == nil || v.to_s.empty?}
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

Expand Down Expand Up @@ -85,7 +88,7 @@ def self.get(path, params = {}, options = {})
else
obj = recursive_struct(load_json(response.body))
end
rescue Exception => e
rescue StandardError => e
puts "Problem getting #{path}" if $DEBUG
raise e
end
Expand All @@ -96,30 +99,26 @@ def self.get_batch(paths, params = {})
responses = []
if conn.in_parallel?
conn.in_parallel do
paths.each {|p| responses << conn.get(p, params) }
paths.each { |p| responses << conn.get(p, params) }
end
else
responses = threaded_request(paths, params)
end
return responses
responses
end

def self.post(path, obj, options = {})
begin
file, file_attribute = params_file_handler(obj)
response = conn.post do |req|
req.url path
custom_req(obj, file, file_attribute, req)
end
raise Exception, response.body if response.status >= 500
if options[:raw] || false # return the unparsed body of the request
return response.body
else
return recursive_struct(load_json(response.body))
end
rescue => e
LOGGER.debug "\n\n\nRUBY API - LinkedData::Client::HTTP ->post: - ECCEZIONE: #{e.message}\n#{e.backtrace.join("\n")}"
raise e
file, file_attribute = params_file_handler(obj)
response = conn.post do |req|
req.url path
custom_req(obj, file, file_attribute, req)
end
raise StandardError, response.body if response.status >= 500

if options[:raw] || false # return the unparsed body of the request
response.body
else
recursive_struct(load_json(response.body))
end

end
Expand All @@ -130,7 +129,8 @@ def self.put(path, obj)
req.url path
custom_req(obj, file, file_attribute, req)
end
raise Exception, response.body if response.status >= 500
raise StandardError, response.body if response.status >= 500

recursive_struct(load_json(response.body))
end

Expand All @@ -140,13 +140,17 @@ def self.patch(path, obj)
req.url path
custom_req(obj, file, file_attribute, req)
end
raise Exception, response.body if response.status >= 500
raise StandardError, response.body if response.status >= 500

response
end

def self.delete(id)
puts "Deleting #{id}" if $DEBUG
response = conn.delete id
raise Exception, response.body if response.status >= 500
raise StandardError, response.body if response.status >= 500

response
end

def self.object_from_json(json)
Expand Down Expand Up @@ -178,9 +182,11 @@ def self.custom_req(obj, file, file_attribute, req)

def self.params_file_handler(params)
return if params.nil?

file, return_attribute = nil, nil
params.dup.each do |attribute, value|
next unless value.is_a?(File) || value.is_a?(Tempfile) || value.is_a?(ActionDispatch::Http::UploadedFile)

filename = value.original_filename
file = Faraday::UploadIO.new(value.path, "text/plain", filename)
return_attribute = attribute
Expand Down Expand Up @@ -211,7 +217,7 @@ def self.recursive_struct(json_obj)
context = json_obj.delete("@context") # strip context

# Create a struct with the left-over attributes to store data
attributes = json_obj.keys.map {|k| k.to_sym}
attributes = json_obj.keys.map { |k| k.to_sym }
attributes_always_present = value_cls.attrs_always_present || [] rescue []
attributes = (attributes + attributes_always_present).uniq

Expand Down Expand Up @@ -241,7 +247,7 @@ def self.recursive_struct(json_obj)
end
else
# Get the struct class
recursive_obj_hash = {links: nil, context: nil}
recursive_obj_hash = { links: nil, context: nil }
json_obj.each do |key, value|
recursive_obj_hash[key] = recursive_struct(value)
end
Expand All @@ -268,6 +274,7 @@ def self.prep_links(obj)

context = links.delete("@context")
return if context.nil?

links.keys.each do |link_type|
link = Link.new(links[link_type])
link.media_type = context[link_type]
Expand All @@ -278,6 +285,7 @@ def self.prep_links(obj)

def self.load_json(json)
return if json.nil? || json.empty?

begin
MultiJson.load(json)
rescue Exception => e
Expand Down
10 changes: 8 additions & 2 deletions lib/ontologies_api_client/link_explorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ def replace_template_elements(url, values = [])
return url if values.nil? || values.empty?
values = values.dup
values = [values] unless values.is_a?(Array)
return url.gsub(/(\{.*?\})/) do
URI.escape(values.shift, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
escaped_value = CGI.escape(values.shift)
if url.match(/(\{.*?\})/)
url.gsub(/(\{.*?\})/) do
escaped_value
end
else
url + '/' + escaped_value
end

end

def linkable_attributes
Expand Down
14 changes: 14 additions & 0 deletions lib/ontologies_api_client/models/agent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require_relative "../base"

module LinkedData
module Client
module Models
class Agent < LinkedData::Client::Base
include LinkedData::Client::Collection
include LinkedData::Client::ReadWrite

@media_type = "http://xmlns.com/foaf/0.1/Agent"
end
end
end
end
15 changes: 7 additions & 8 deletions lib/ontologies_api_client/models/class.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
require "uri"
require "cgi"
require_relative "../base"

module LinkedData
module Client
module Models
class Class < LinkedData::Client::Base
HTTP = LinkedData::Client::HTTP
@media_type = "http://www.w3.org/2002/07/owl#Class"
@include_attrs = "prefLabel,definition,synonym,obsolete,hasChildren"
@include_attrs_full = "prefLabel,definition,synonym,obsolete,properties,hasChildren,children"
@attrs_always_present = :prefLabel, :definition, :synonym, :obsolete, :properties, :hasChildren, :children

@media_type = %w[http://www.w3.org/2002/07/owl#Class http://www.w3.org/2004/02/skos/core#Concept]
@include_attrs = "prefLabel,definition,synonym,obsolete,hasChildren,inScheme,memberOf"
@include_attrs_full = "prefLabel,definition,synonym,obsolete,properties,hasChildren,childre,inScheme,memberOf"
@attrs_always_present = :prefLabel, :definition, :synonym, :obsolete, :properties, :hasChildren, :children, :inScheme, :memberOf
alias :fullId :id

# triple store predicate is <http://www.w3.org/2002/07/owl#deprecated>
Expand Down Expand Up @@ -48,7 +47,7 @@ def purl
return "" if self.links.nil?
return self.id if self.id.include?("purl.")
ont = self.explore.ontology
"#{LinkedData::Client.settings.purl_prefix}/#{ont.acronym}?conceptid=#{URI.escape(self.id, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
"#{LinkedData::Client.settings.purl_prefix}/#{ont.acronym}?conceptid=#{CGI.escape(self.id)}"
end

def ontology
Expand All @@ -57,7 +56,7 @@ def ontology

def self.find(id, ontology, params = {})
ontology = HTTP.get(ontology, params)
ontology.explore.class(URI.escape(id, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")))
ontology.explore.class(CGI.escape(id))
end

def self.search(*args)
Expand Down
12 changes: 12 additions & 0 deletions lib/ontologies_api_client/models/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require_relative "../base"

module LinkedData
module Client
module Models
class Collection < LinkedData::Client::Base
include LinkedData::Client::Collection
@media_type = "http://www.w3.org/2004/02/skos/core#Collection"
end
end
end
end
Loading

0 comments on commit d0e5d81

Please sign in to comment.