From 442aa6f6647756d10d57cd7869cb3d27c87b24a8 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 10 Jan 2012 13:04:01 +0100 Subject: untested update --- lib/opentox-ruby.rb | 2 +- lib/opentox.rb | 143 +++++++++++++++++++++++++++++++-------------- lib/parser.rb | 6 +- lib/rest_client_wrapper.rb | 2 +- lib/task.rb | 2 +- 5 files changed, 107 insertions(+), 48 deletions(-) (limited to 'lib') diff --git a/lib/opentox-ruby.rb b/lib/opentox-ruby.rb index 16abee9..63935fa 100644 --- a/lib/opentox-ruby.rb +++ b/lib/opentox-ruby.rb @@ -1,5 +1,5 @@ #['rubygems', 'sinatra', 'sinatra/url_for', 'ohm', 'rest_client', 'yaml', 'cgi', 'spork', 'error', 'overwrite', 'environment'].each do |lib| -['rubygems', 'rest_client', 'yaml', 'cgi', 'spork', 'error', 'overwrite', 'environment'].each do |lib| +['rubygems', 'rest_client', 'yaml', 'cgi', 'error', 'overwrite', 'environment'].each do |lib| require lib end diff --git a/lib/opentox.rb b/lib/opentox.rb index 6250d86..dbe2360 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -1,51 +1,70 @@ +require "./parser.rb" +require "./rest_client_wrapper.rb" +require "./error.rb" + +SERVICES = ["Compound", "Feature", "Dataset", "Algorithm", "Model", "Validation", "Task"] + module OpenTox - attr_reader :uri - attr_accessor :metadata + attr_accessor :subjectid, :uri + attr_writer :metadata - # Initialize OpenTox object with optional uri - # @param [optional, String] URI - def initialize(uri=nil,subjectid=nil) - @metadata = {} + # Initialize OpenTox object with optional subjectid + # @param [optional, String] subjectid + def initialize uri=nil, subjectid=nil + @uri = uri @subjectid = subjectid - self.uri = uri if uri end - # Set URI - # @param [String] URI - def uri=(uri) - @uri = uri - @metadata[XSD.anyURI] = uri - end - - # Get all objects from a service - # @return [Array] List of available URIs - def self.all(uri, subjectid=nil) - RestClientWrapper.get(uri,:accept => "text/uri-list", :subjectid => subjectid).to_s.split(/\n/) - end - - # Load (and return) metadata from object URI - # @return [Hash] Metadata - def load_metadata - @metadata = Parser::Owl::Generic.new(@uri).load_metadata - @metadata - end - - # Add/modify metadata, existing entries will be overwritten - # @example - # dataset.add_metadata({DC.title => "any_title", DC.creator => "my_email"}) - # @param [Hash] metadata Hash mapping predicate_uris to values - def add_metadata(metadata) - metadata.each do |k,v| - if v.is_a? Array - @metadata[k] = [] unless @metadata[k] - @metadata[k] << v - else - @metadata[k] = v - end - end + def metadata + @metadata ||= Parser::Owl::Generic.from_rdf get(:accept => "application/rdf+xml") + end + + # REST API + # returns OpenTox::WrapperResult, not OpenTox objects + + # perfoms a GET REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # per default: waits for Task to finish and returns result URI of Task + # @param [optional,Hash] headers contains params like accept-header + # @param [wait,Boolean] wait set to false to NOT wait for task if result is task + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def get headers={}, wait=true + headers[:subjectid] = @subjectid + RestClientWrapper.get(@uri, headers, nil, wait).chomp end + # performs a POST REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # per default: waits for Task to finish and returns result URI of Task + # @param [optional,String] payload data posted to the service + # @param [optional,Hash] headers contains params like accept-header + # @param [wait,Boolean] wait set to false to NOT wait for task if result is task + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def post payload=nil, headers={}, wait=true + headers[:subjectid] = @subjectid + RestClientWrapper.post(@uri, payload, headers, nil, wait).chomp + end + + # performs a PUT REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # @param [optional,String] payload data put to the service + # @param [optional,Hash] headers contains params like accept-header + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def put payload=nil, headers={} + headers[:subjectid] = @subjectid + RestClientWrapper.put(@uri, payload, headers).chomp + end + + # performs a DELETE REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def delete + RestClientWrapper.delete(@uri,:subjectid => @subjectid) + end + + # Tools + # Get OWL-DL representation in RDF/XML format # @return [application/rdf+xml] RDF/XML representation def to_rdfxml @@ -54,9 +73,47 @@ module OpenTox s.to_rdfxml end - # deletes the resource, deletion should have worked when no RestCallError raised - def delete - RestClientWrapper.delete(uri,:subjectid => @subjectid) + def uri_available? + url = URI.parse(@uri) + #TODO: move subjectid to header + subjectidstr = @subjectid ? "?subjectid=#{CGI.escape @subjectid}" : "" + Net::HTTP.start(url.host, url.port) do |http| + return http.head("#{url.request_uri}#{subjectidstr}").code == "200" + end + end + + # create default classes + SERVICES.each { |s| eval "class #{s}; include OpenTox; end" } + + module Collection + + include OpenTox + + def find + uri_available? ? object_class.new(@uri, @subjectid) : nil + end + + def create metadata + object_class.new post(service_uri, metadata.to_rdfxml, { :content_type => 'application/rdf+xml', :subjectid => subjectid}).to_s.chomp, @subject_id + end + + # Get all objects from a service + # @return [Array] List of available Objects + def all + get(:accept => "text/uri-list").to_s.split(/\n/).collect{|uri| object_class.new uri,@subjectid} + end + + def save object + object_class.new post(object.to_rdfxml, :content_type => 'application/rdf+xml').to_s, @subjectid + end + + def object_class + eval self.class.to_s.sub(/::Collection/,'') + end + + # create collection classes + SERVICES.each { |s| eval "class #{s}; include Collection; end" } + end end diff --git a/lib/parser.rb b/lib/parser.rb index a6878a2..580f6f7 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -1,5 +1,5 @@ -require 'spreadsheet' -require 'roo' +#require 'spreadsheet' +#require 'roo' class String @@ -259,6 +259,7 @@ module OpenTox end +=begin # Parser for getting spreadsheet data into a dataset class Spreadsheets @@ -440,5 +441,6 @@ module OpenTox end end +=end end end diff --git a/lib/rest_client_wrapper.rb b/lib/rest_client_wrapper.rb index 53887a2..3dd3eff 100644 --- a/lib/rest_client_wrapper.rb +++ b/lib/rest_client_wrapper.rb @@ -53,7 +53,7 @@ module OpenTox private def self.execute( rest_call, uri, payload=nil, headers={}, waiting_task=nil, wait=true ) - raise OpenTox::BadRequestError.new "uri is null" unless uri + raise OpenTox::BadRequestError.new "uri is nil" unless uri raise OpenTox::BadRequestError.new "not a uri: "+uri.to_s unless uri.to_s.uri? raise "headers are no hash: "+headers.inspect unless headers==nil or headers.is_a?(Hash) raise OpenTox::BadRequestError.new "accept should go into the headers" if payload and payload.is_a?(Hash) and payload[:accept] diff --git a/lib/task.rb b/lib/task.rb index 146a756..3815177 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -231,7 +231,7 @@ module OpenTox else raise "content type for tasks not supported: "+content_type.to_s end - raise "uri is null after loading" unless @uri and @uri.to_s.strip.size>0 + raise "uri is nil after loading" unless @uri and @uri.to_s.strip.size>0 end =end -- cgit v1.2.3