Skip to content

Commit

Permalink
Merge pull request #2 from ncbo/master
Browse files Browse the repository at this point in the history
Sync: merge releases 2.2.0 and 2.2.1 from ncbo
  • Loading branch information
syphax-bouazzouni authored Jun 24, 2023
2 parents fb74964 + 2f27e47 commit 9d47f83
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 71 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ source 'https://rubygems.org'
gemspec

gem 'rake'
gem 'rubocop', '~> 1.43'
gem 'pry'
gem 'test-unit'
85 changes: 56 additions & 29 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
PATH
remote: .
specs:
ontologies_api_client (2.0.3)
activesupport (= 6.0.4.1)
ontologies_api_client (2.2.2)
activesupport (= 6.1.7.3)
addressable (~> 2.8)
excon
faraday
faraday-excon (~> 2.0.0)
faraday-excon
faraday-multipart
lz4-ruby
multi_json
Expand All @@ -15,56 +16,82 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.4.1)
activesupport (6.1.7.3)
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)
zeitwerk (~> 2.3)
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
coderay (1.1.3)
concurrent-ruby (1.1.9)
excon (0.91.0)
faraday (2.0.1)
faraday-net_http (~> 2.0)
concurrent-ruby (1.2.2)
excon (0.100.0)
faraday (2.7.6)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-excon (2.0.0)
faraday-excon (2.1.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 (~> 2.0)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.0.2)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
lz4-ruby (0.3.3)
method_source (1.0.0)
minitest (5.15.0)
minitest (5.18.1)
multi_json (1.15.0)
multipart-post (2.1.1)
oj (3.13.11)
power_assert (2.0.1)
pry (0.14.1)
multipart-post (2.3.0)
oj (3.15.0)
parallel (1.22.1)
parser (3.2.0.0)
ast (~> 2.4.1)
power_assert (2.0.3)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.0.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.6.2)
rexml (3.2.5)
rubocop (1.44.1)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.24.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.24.1)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
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.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
zeitwerk (2.6.8)

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

DEPENDENCIES
ontologies_api_client!
pry
rake
rubocop (~> 1.43)
test-unit

BUNDLED WITH
2.1.4
2.3.22
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
39 changes: 26 additions & 13 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 @@ -83,7 +86,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 @@ -94,12 +97,12 @@ 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 = {})
Expand All @@ -108,11 +111,12 @@ def self.post(path, obj, options = {})
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

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

Expand All @@ -122,7 +126,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 @@ -132,13 +137,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 @@ -170,9 +179,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 @@ -203,7 +214,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 @@ -233,7 +244,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 @@ -260,6 +271,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 @@ -270,6 +282,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
5 changes: 3 additions & 2 deletions lib/ontologies_api_client/link_explorer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'uri'
require 'addressable/uri'
require_relative 'http'

module LinkedData
Expand Down Expand Up @@ -58,10 +58,11 @@ def combined_links

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}]"))
Addressable::URI.encode_component(values.shift, Addressable::URI::CharacterClasses::UNRESERVED)
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/ontologies_api_client/models/class.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'addressable/uri'
require "uri"
require_relative "../base"

Expand Down Expand Up @@ -47,8 +48,10 @@ def to_jsonld
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}]"))}"
encoded_id = Addressable::URI.encode_component(self.id, Addressable::URI::CharacterClasses::UNRESERVED)
"#{LinkedData::Client.settings.purl_prefix}/#{ont.acronym}?conceptid=#{encoded_id}"
end

def ontology
Expand All @@ -57,7 +60,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.single_class(id)
end

def self.search(*args)
Expand Down
10 changes: 6 additions & 4 deletions lib/ontologies_api_client/models/mapping.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "uri"
require 'addressable/template'
require_relative "../base"

module LinkedData
Expand All @@ -11,11 +11,13 @@ class Mapping < LinkedData::Client::Base
@media_type = "http://data.bioontology.org/metadata/Mapping"

def self.find(id, params = {})
HTTP.get(mappings_url_prefix + URI.escape(id, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")), params)
template = Addressable::Template.new("#{mappings_url_prefix}/{mapping}")
HTTP.get(template.expand({mapping: id}), params)
end

def delete
HTTP.delete(mappings_url_prefix + URI.escape(self.id, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")))
template = Addressable::Template.new("#{mappings_url_prefix}/{mapping}")
HTTP.delete(template.expand({mapping: self.id}))
end

private
Expand All @@ -24,7 +26,7 @@ def delete
# This is in a method because the settings are configured after
# the VM initialization, so the rest_url could be null or wrong
def self.mappings_url_prefix
LinkedData::Client.settings.rest_url + "/mappings/"
LinkedData::Client.settings.rest_url + "/mappings"
end

def mappings_url_prefix
Expand Down
Loading

0 comments on commit 9d47f83

Please sign in to comment.