From fa9069e13fb6b1c8bb4ebcdf82f1cf1c04ad71ca Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 23 Feb 2012 17:56:46 +0000 Subject: (partially) switched back to RestClientWrapper --- lib/opentox.rb | 67 ++++++++++++---------------------------------------------- 1 file changed, 13 insertions(+), 54 deletions(-) (limited to 'lib/opentox.rb') 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 -- cgit v1.2.3