summaryrefslogtreecommitdiff
path: root/lib/opentox.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2012-07-12 16:38:03 +0200
committerChristoph Helma <helma@in-silico.ch>2012-07-12 16:38:03 +0200
commitbf6834445feb6f93f0a20359462dbd1e7e89f4b8 (patch)
tree2f331f445d8390f7f3309e1689587ae3024c60f4 /lib/opentox.rb
parentd8e72c6ff7ce2b147b6a11d233b80bbc8f1760d9 (diff)
all opentox-client tests pass
Diffstat (limited to 'lib/opentox.rb')
-rw-r--r--lib/opentox.rb80
1 files changed, 35 insertions, 45 deletions
diff --git a/lib/opentox.rb b/lib/opentox.rb
index b0c786a..9225bb0 100644
--- a/lib/opentox.rb
+++ b/lib/opentox.rb
@@ -14,7 +14,14 @@ module OpenTox
# @return [OpenTox] OpenTox object
def initialize uri=nil, subjectid=nil
@rdf = RDF::Graph.new
- uri ? @uri = uri.to_s.chomp : @uri = RDF::Node.uuid.to_s
+ if uri
+ @uri = uri.to_s.chomp
+ else
+ service = self.class.to_s.split('::').last.downcase
+ service_uri = eval("$#{service}[:uri]")
+ bad_request_error "$#{service}[:uri] variable not set. Please set $#{service}[:uri] or use an explicit uri as first constructor argument " unless service_uri
+ @uri = File.join service_uri, SecureRandom.uuid
+ end
append RDF.type, eval("RDF::OT."+self.class.to_s.split('::').last)
append RDF::DC.date, DateTime.now
@subjectid = subjectid
@@ -24,7 +31,6 @@ module OpenTox
# @return [Hash] Object metadata
def metadata
# return plain strings instead of RDF objects
- #puts @rdf.to_hash
@rdf.to_hash[RDF::URI.new(@uri)].inject({}) { |h, (predicate, values)| h[predicate.to_s] = values.collect{|v| v.to_s}; h }
end
@@ -32,6 +38,7 @@ module OpenTox
# @param [String] Predicate URI
# @return [Array, String] Predicate value(s)
def [](predicate)
+ return nil if metadata[predicate.to_s].nil?
metadata[predicate.to_s].size == 1 ? metadata[predicate.to_s].first : metadata[predicate.to_s]
end
@@ -55,30 +62,30 @@ module OpenTox
# Get object from webservice
def get
parse_ntriples RestClientWrapper.get(@uri,{},{:accept => "text/plain", :subjectid => @subjectid})
- rescue # fall back to rdfxml
- parse_rdfxml RestClientWrapper.get(@uri,{},{:accept => "application/rdf+xml", :subjectid => @subjectid})
+ #rescue # fall back to rdfxml
+ #parse_rdfxml RestClientWrapper.get(@uri,{},{:accept => "application/rdf+xml", :subjectid => @subjectid})
end
# Post object to webservice
def post service_uri
RestClientWrapper.post service_uri, to_ntriples, { :content_type => "text/plain", :subjectid => @subjectid}
- rescue # fall back to rdfxml
- RestClientWrapper.post service_uri, to_rdfxml, { :content_type => "application/rdf+xml", :subjectid => @subjectid}
+ #rescue # fall back to rdfxml
+ #RestClientWrapper.post service_uri, to_rdfxml, { :content_type => "application/rdf+xml", :subjectid => @subjectid}
end
# Save object at webservice
def put
append RDF::DC.modified, DateTime.now
- begin
+ #begin
RestClientWrapper.put @uri.to_s, self.to_ntriples, { :content_type => "text/plain", :subjectid => @subjectid}
- rescue # fall back to rdfxml
- RestClientWrapper.put @uri.to_s, self.to_rdfxml, { :content_type => "application/rdf+xml", :subjectid => @subjectid}
- end
+ #rescue # fall back to rdfxml
+ #RestClientWrapper.put @uri.to_s, self.to_rdfxml, { :content_type => "application/rdf+xml", :subjectid => @subjectid}
+ #end
end
# Delete object at webservice
def delete
- @response = RestClientWrapper.delete(@uri.to_s,nil,{:subjectid => @subjectid})
+ RestClientWrapper.delete(@uri.to_s,nil,{:subjectid => @subjectid})
end
RDF_FORMATS.each do |format|
@@ -100,42 +107,20 @@ module OpenTox
end
end
- # class methods
- module ClassMethods
-
- def all service_uri, subjectid=nil
- uris = RestClientWrapper.get(service_uri, {}, :accept => 'text/uri-list').split("\n").compact
- uris.collect{|uri| URI.task?(service_uri) ? from_uri(uri, subjectid, false) : from_uri(uri, subjectid)}
+ def to_turtle # redefine to use prefixes (not supported by RDF::Writer)
+ prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#"}
+ ['OT', 'DC', 'XSD', 'OLO'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") }
+ turtle = RDF::N3::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer|
+ @rdf.each{|statement| writer << statement}
end
+ end
- def from_file service_uri, filename, subjectid=nil
- file = File.new filename
- # sdf files are incorrectly detected
- file.mime_type = "chemical/x-mdl-sdfile" if File.extname(filename) == ".sdf"
- from_uri RestClientWrapper.post(service_uri, {:file => file}, {:subjectid => subjectid, :content_type => file.mime_type, :accept => "text/uri-list"}), subjectid
+ {:title => RDF::DC.title, :dexcription => RDF::DC.description}.each do |method,predicate|
+ send :define_method, method do
+ self.[](predicate)
end
-
- private
- def from_uri uri, subjectid=nil, wait=true
-
- uri.chomp!
- # TODO add waiting task
- if URI.task?(uri) and wait
- t = OpenTox::Task.new(uri)
- t.wait
- uri = t.resultURI
- end
-
- # guess class from uri, this is potentially unsafe, but polling metadata from large uri lists is way too slow (and not all service provide RDF.type in their metadata)
- result = CLASSES.collect{|s| s if uri =~ /#{s.downcase}/}.compact
- if result.size == 1
- klass = result.first
- else
- klass = OpenTox::Generic.new(uri)[RDF.type]
- internal_server_error "Cannot determine class from URI '#{uri} (Candidate classes are #{result.inspect}) or matadata." unless klass
- end
- # initialize with/without subjectid
- subjectid ? eval("#{self}.new(\"#{uri}\", \"#{subjectid}\")") : eval("#{self}.new(\"#{uri}\")")
+ send :define_method, "#{method}=" do |value|
+ self.[]=(predicate,value)
end
end
@@ -143,7 +128,12 @@ module OpenTox
CLASSES.each do |klass|
c = Class.new do
include OpenTox
- extend OpenTox::ClassMethods
+ #extend OpenTox::ClassMethods
+
+ def self.all service_uri, subjectid=nil
+ uris = RestClientWrapper.get(service_uri, {}, :accept => 'text/uri-list').split("\n").compact
+ uris.collect{|uri| self.new(uri, subjectid)}
+ end
end
OpenTox.const_set klass,c
end