Skip to content

Commit

Permalink
Feature: api endpoint returns json-ld for the element with that URI
Browse files Browse the repository at this point in the history
  • Loading branch information
imadbourouche committed Feb 7, 2024
1 parent d515a94 commit 422e0a6
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 9 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
openjdk-11-jre-headless \
raptor2-utils \
wait-for-it \
libraptor2-dev \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir -p /srv/ontoportal/ontologies_api
Expand Down
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ gem 'sinatra', '~> 1.0'
gem 'sinatra-advanced-routes'
gem 'sinatra-contrib', '~> 1.0'
gem 'request_store'
gem 'rdf-raptor'


# Rack middleware
gem 'ffi'
Expand Down Expand Up @@ -44,12 +46,12 @@ gem 'haml', '~> 5.2.2' # pin see https://github.com/ncbo/ontologies_api/pull/107
gem 'redcarpet'

# NCBO gems (can be from a local dev path or from rubygems/git)
gem 'goo', git: 'https://github.com/ontoportal-lirmm/goo.git', branch: 'development'
gem 'goo', github: 'ontoportal-lirmm/goo', branch: 'development'
gem 'ncbo_annotator', git: 'https://github.com/ontoportal-lirmm/ncbo_annotator.git', branch: 'master'
gem 'ncbo_cron', git: 'https://github.com/ontoportal-lirmm/ncbo_cron.git', branch: 'master'
gem 'ncbo_ontology_recommender', git: 'https://github.com/ncbo/ncbo_ontology_recommender.git', branch: 'master'
gem 'sparql-client', github: 'ontoportal-lirmm/sparql-client', branch: 'master'
gem 'ontologies_linked_data', git: 'https://github.com/ontoportal-lirmm/ontologies_linked_data.git', branch: 'development'
gem 'ontologies_linked_data', git: 'https://github.com/ontoportal-lirmm/ontologies_linked_data.git', branch: 'feature/sparql-fetch-related-triples'

group :development do
# bcrypt_pbkdf and ed35519 is required for capistrano deployments when using ed25519 keys; see https://github.com/miloserdow/capistrano-deploy/issues/42
Expand Down
14 changes: 9 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GIT

GIT
remote: https://github.com/ontoportal-lirmm/goo.git
revision: 27a38376d7372d46cd39790dbae3eeed0fec15bb
revision: 6187c205a1310c2f8a72fe8e07fcf99477060fa9
branch: development
specs:
goo (0.0.2)
Expand Down Expand Up @@ -54,8 +54,8 @@ GIT

GIT
remote: https://github.com/ontoportal-lirmm/ontologies_linked_data.git
revision: 0de5f3b2db02a5d2c008ace73a34d586c24e67a7
branch: development
revision: 3745f308776d82bff5b346cfa6eebc143daf14bf
branch: feature/sparql-fetch-related-triples
specs:
ontologies_linked_data (0.0.1)
activesupport
Expand Down Expand Up @@ -245,7 +245,7 @@ GEM
mlanett-redis-lock (0.2.7)
redis
multi_json (1.15.0)
multipart-post (2.3.0)
multipart-post (2.4.0)
mutex_m (0.2.0)
net-http-persistent (2.9.4)
net-imap (0.4.10)
Expand Down Expand Up @@ -295,6 +295,9 @@ GEM
rake (10.5.0)
rdf (1.0.8)
addressable (>= 2.2)
rdf-raptor (1.0.1)
ffi (>= 1.0)
rdf (>= 1.0.0)
redcarpet (3.6.0)
redis (4.8.1)
redis-activesupport (5.3.0)
Expand Down Expand Up @@ -418,6 +421,7 @@ DEPENDENCIES
rack-test
rack-timeout
rake (~> 10.0)
rdf-raptor
redcarpet
redis (~> 4.8.1)
redis-activesupport
Expand All @@ -436,4 +440,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.3.23
2.4.22
4 changes: 3 additions & 1 deletion bin/ontoportal
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ test() {

local custom_command="bundle exec rake test TEST='$test_path' TESTOPTS='$test_options'"
echo "run : $custom_command"
run_command "$custom_command" "${all_arguments[@]}"
#run_command "$custom_command" "${all_arguments[@]}"
run_command "$custom_command" --old-path /home/bourouche/PFE/backend/ontologies_linked_data --goo-path /home/bourouche/PFE/backend/goo

}

# Function to handle the "run" option
Expand Down
89 changes: 89 additions & 0 deletions controllers/dereference_resource_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require_relative '../test/test_case'


class ImadController < ApplicationController

namespace '/dereference_resource' do

get do
raise error 405, "Method Not Allowd: This route must be provided via POST request with acronym, uri, output_format parameters"
end

def set_vars
@@acronym = "TST"
@@name = "Test Ontology"
@@test_file = File.expand_path("../../test/data/ontology_files/BRO_v3.1.owl", __FILE__)
@@file_params = {
name: @@name,
hasOntologyLanguage: "OWL",
administeredBy: "tim",
"file" => Rack::Test::UploadedFile.new(@@test_file, ""),
released: DateTime.now.to_s,
contact: [{name: "test_name", email: "[email protected]"}],
URI: 'https://test.com/test',
status: 'production',
description: 'ontology description'
}
@@status_uploaded = "UPLOADED"
@@status_rdf = "RDF"
end

def create_user
username = "tim"
test_user = User.new(username: username, email: "#{username}@example.org", password: "password")
test_user.save if test_user.valid?
@@user = test_user.valid? ? test_user : User.find(username).first
end

def create_onts
ont = Ontology.new(acronym: @@acronym, name: @@name, administeredBy: [@@user])
end

post do
set_vars()
create_user()
create_onts()

acronym = params[:acronym]
uri = params[:uri]
output_format = params[:output_format].presence || 'jsonld'
acronym = URI.decode_www_form_component(acronym)
uri = URI.decode_www_form_component(uri)
unless valid_url?(acronym) && valid_url?(uri)
raise error 500, "INVALID URLs"
return
end

r = Resource.new(acronym, uri)
case output_format
when 'jsonld'
content_type 'application/json'
reply JSON.parse(r.to_json)
when 'json'
content_type 'application/json'
reply JSON.parse(r.to_json)
when 'xml'
content_type 'application/xml'
reply r.to_xml
when 'turtle'
content_type 'text/turtle'
reply r.to_turtle
when 'ntriples'
content_type 'application/n-triples'
reply r.to_ntriples
else
raise error 500, "Invalid output format"
end

end

private

def valid_url?(url)
uri = URI.parse(url)
uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
rescue URI::InvalidURIError
false
end
end
end
9 changes: 8 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
x-app: &app
image: agroportal/ontologies_api:development
#image: agroportal/ontologies_api:development
build:
context: .
args:
RUBY_VERSION: '2.7'
# Increase the version number in the image tag every time Dockerfile or its arguments is changed
image: ontologies_api-dev:0.0.2

environment: &env
BUNDLE_PATH: /srv/ontoportal/bundle
# default bundle config resolves to /usr/local/bundle/config inside of the container
Expand Down
2 changes: 2 additions & 0 deletions models/simple_wrappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
ProvisionalRelation = LinkedData::Models::ProvisionalRelation

SearchHelper = Sinatra::Helpers::SearchHelper

Resource = LinkedData::Models::Resource
115 changes: 115 additions & 0 deletions test/controllers/test_dereference_resource_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
require_relative '../test_case'

class TestImadController < TestCase

def self.before_suite
#LinkedData::TestCase.backend_4s_delete
=begin
data = %(
@prefix ex: <http://example.org/> .
@prefix rdf: <#{Goo.vocabulary(:rdf)}> .
@prefix owl: <#{Goo.vocabulary(:owl)}> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ex:TestSubject1 rdf:type owl:Ontology .
ex:TestSubject1 ex:TestPredicate11 "TestObject11" .
ex:TestSubject1 ex:TestPredicate12 ex:test .
ex:TestSubject1 ex:TestPredicate13 1 .
ex:TestSubject1 ex:TestPredicate14 true .
ex:TestSubject1 ex:TestPredicate15 "1.9"^^xsd:float .
ex:TestSubject2 ex:TestPredicate2 1.9 .
)
graph = "http://example.org/test_graph"
Goo.sparql_data_client.execute_append_request(graph, data, "application/x-turtle")
=end
_set_vars
_create_user
_create_onts
end


def self._set_vars
@@acronym = "TST"
@@name = "Test Ontology"
@@test_file = File.expand_path("../../data/ontology_files/BRO_v3.1.owl", __FILE__)
@@file_params = {
name: @@name,
hasOntologyLanguage: "OWL",
administeredBy: "tim",
"file" => Rack::Test::UploadedFile.new(@@test_file, ""),
released: DateTime.now.to_s,
contact: [{name: "test_name", email: "[email protected]"}],
URI: 'https://test.com/test',
status: 'production',
description: 'ontology description'
}
@@status_uploaded = "UPLOADED"
@@status_rdf = "RDF"
end

def self._create_user
username = "tim"
test_user = User.new(username: username, email: "#{username}@example.org", password: "password")
test_user.save if test_user.valid?
@@user = test_user.valid? ? test_user : User.find(username).first
end

def self._create_onts
ont = Ontology.new(acronym: @@acronym, name: @@name, administeredBy: [@@user])
ont.save
end

def submit_ontology
post "/ontologies/#{@@acronym}/submissions", @@file_params
assert_equal(201, last_response.status, msg=get_errors(last_response))
sub = MultiJson.load(last_response.body)
get "/ontologies/#{@@acronym}"
ont = MultiJson.load(last_response.body)
assert ont["acronym"].eql?(@@acronym)
end


def test_imad_controller
submit_ontology()

post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim" }
puts
puts last_response.body
puts
assert last_response.ok?


post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "json"}
puts
puts last_response.body
puts
assert last_response.ok?


post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "xml"}
puts
puts last_response.body
puts
assert last_response.ok?


post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "ntriples"}
puts
puts last_response.body
puts
assert last_response.ok?


post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "turtle"}
puts
puts last_response.body
puts
assert last_response.ok?

# Cleanup
#delete "/ontologies/#{@@acronym}/submissions/#{sub['submissionId']}"
#assert_equal(204, last_response.status, msg=get_errors(last_response))

end

end

0 comments on commit 422e0a6

Please sign in to comment.