summaryrefslogtreecommitdiff
path: root/lib/opentox.rb
blob: b0ffcb11bc22514c56a69df030be8c3b5f18bc23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'rdf'
require 'rdf/raptor'
#require "parser.rb"
require "rest_client_wrapper.rb"
require "overwrite.rb"
require "error.rb"

RDF::OT =  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"]

module OpenTox

  attr_accessor :subjectid, :uri #, :service_uri
  #attr_writer :metadata

  # Initialize OpenTox object with optional subjectid
  # @param [optional, String] subjectid
  def initialize uri=nil, subjectid=nil
    @uri = uri
    @subjectid = subjectid
  end

  def metadata
    metadata = {}
    RDF::Reader.open(@uri) do |reader|
      reader.each_statement do |statement|
        metadata[statement.predicate] = statement.object if statement.subject == @uri
      end
    end
    metadata
  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.to_s, 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={}, headers={}, wait=true 
    headers[:subjectid] = @subjectid
    RestClientWrapper.post(@uri.to_s, 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={}, headers={} 
    headers[:subjectid] = @subjectid
    RestClientWrapper.put(@uri.to_s, 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.to_s,:subjectid => @subjectid)
  end

  # create default classes
  SERVICES.each { |s| eval "class #{s}; include OpenTox; end" }

=begin
  # Tools

  def uri_available?
    url = URI.parse(@uri)
    req = Net::HTTP.new(url.host,url.port)
    req['subjectid'] = @subjectid if @subjectid
    req.start(url.host, url.port) do |http|
      return http.head("#{url.request_uri}#{subjectidstr}").code == "200"
    end
  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

end