summaryrefslogtreecommitdiff
path: root/lib/opentox.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2012-02-23 17:56:46 +0000
committerChristoph Helma <helma@in-silico.ch>2012-02-23 17:56:46 +0000
commitfa9069e13fb6b1c8bb4ebcdf82f1cf1c04ad71ca (patch)
treeefe8bff6c3538c856805cadac832531aa6b0821e /lib/opentox.rb
parentb651c4d199a7b4d6a06cdefb281601bddd2fc885 (diff)
(partially) switched back to RestClientWrapper
Diffstat (limited to 'lib/opentox.rb')
-rw-r--r--lib/opentox.rb67
1 files changed, 13 insertions, 54 deletions
diff --git a/lib/opentox.rb b/lib/opentox.rb
index 2fbf9dc..f81ae10 100644
--- a/lib/opentox.rb
+++ b/lib/opentox.rb
@@ -4,45 +4,6 @@ RDF::OT1 = RDF::Vocabulary.new 'http://www.opentox.org/api/1.1#'
RDF::OTA = RDF::Vocabulary.new 'http://www.opentox.org/algorithmTypes.owl#'
SERVICES = ["Compound", "Feature", "Dataset", "Algorithm", "Model", "Validation", "Task", "Investigation"]
-class String
- def underscore
- self.gsub(/::/, '/').
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
- tr("-", "_").
- downcase
- end
-end
-
-module URI
-
- def self.task? uri
- uri =~ /task/ and URI.valid? uri
- end
-
- def self.dataset? uri, subjectid=nil
- uri =~ /dataset/ and URI.accessible? uri, subjectid=nil
- end
-
- def self.model? uri, subjectid=nil
- uri =~ /model/ and URI.accessible? uri, subjectid=nil
- end
-
- def self.accessible? uri, subjectid=nil
- Net::HTTP.get_response(URI.parse(uri))
- true
- rescue
- false
- end
-
- def self.valid? uri
- u = URI::parse(uri)
- u.scheme!=nil and u.host!=nil
- rescue URI::InvalidURIError
- false
- end
-end
-
# defaults to stderr, may be changed to file output
$logger = OTLogger.new(STDERR) # no rotation
$logger.level = Logger::DEBUG
@@ -59,13 +20,15 @@ module OpenTox
# Ruby interface
-
# override to read all error codes
def metadata reload=true
- if reload
+ if reload or @metadata.empty?
@metadata = {}
# ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader
- RestClient.get(@uri) do |response, request, result, &block|
+ # TODO: convert to RestClientWrapper
+ kind_of?(OpenTox::Dataset) ? uri = File.join(@uri,"metadata") : uri = @uri
+ RestClient.get(uri) do |response, request, result|
+ #response = RestClientWrapper.get(@uri) #do |response, request, result|
$logger.warn "#{@uri} returned #{result}" unless response.code == 200 or response.code == 202 or URI.task? @uri
RDF::Reader.for(:rdfxml).new(response) do |reader|
reader.each_statement do |statement|
@@ -74,6 +37,7 @@ module OpenTox
end
end
end
+ #puts @metadata.inspect
@metadata
end
@@ -88,46 +52,41 @@ module OpenTox
def get params={}
params[:subjectid] ||= @subjectid
params[:accept] ||= 'application/rdf+xml'
- @response = RestClient.get @uri, params
+ @response = RestClientWrapper.get @uri, params
end
def post payload={}, params={}
params[:subjectid] ||= @subjectid
params[:accept] ||= 'application/rdf+xml'
- @response = RestClient.post(@uri.to_s, payload, params)
- begin
- @response.to_s.to_object
- rescue
- @response
- end
+ @response = RestClientWrapper.post(@uri.to_s, payload, params)
end
def put payload={}, params={}
params[:subjectid] ||= @subjectid
params[:accept] ||= 'application/rdf+xml'
- @response = RestClient.put(@uri.to_s, payload, params)
+ @response = RestClientWrapper.put(@uri.to_s, payload, params)
end
def delete params={}
params[:subjectid] ||= @subjectid
params[:accept] ||= 'application/rdf+xml'
- @response = RestClient.delete(@uri.to_s,:subjectid => @subjectid)
+ @response = RestClientWrapper.delete(@uri.to_s,:subjectid => @subjectid)
end
# class methods
module ClassMethods
def create service_uri, subjectid=nil
- uri = RestClient.post(service_uri, {}, :subjectid => subjectid).chomp
+ uri = RestClientWrapper.post(service_uri, {}, :subjectid => subjectid).chomp
subjectid ? eval("#{self}.new(\"#{uri}\", #{subjectid})") : eval("#{self}.new(\"#{uri}\")")
end
def from_file service_uri, file, subjectid=nil
- RestClient.post(service_uri, :file => File.new(file), :subjectid => subjectid).chomp.to_object
+ RestClientWrapper.post(service_uri, :file => File.new(file), :subjectid => subjectid).chomp.to_object
end
def all service_uri, subjectid=nil
- uris = RestClient.get(service_uri, {:accept => 'text/uri-list'}).split("\n").compact
+ uris = RestClientWrapper.get(service_uri, {:accept => 'text/uri-list'}).split("\n").compact
uris.collect{|uri| subjectid ? eval("#{self}.new(\"#{uri}\", #{subjectid})") : eval("#{self}.new(\"#{uri}\")")}
end