summaryrefslogtreecommitdiff
path: root/lib/opentox.rb
blob: dbe236076e6a2456550fcd38cf800e271a3cc973 (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
require "./parser.rb"
require "./rest_client_wrapper.rb"
require "./error.rb"

SERVICES = ["Compound", "Feature", "Dataset", "Algorithm", "Model", "Validation", "Task"]

module OpenTox

  attr_accessor :subjectid, :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 ||= 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
    s = Serializer::Owl.new
    s.add_metadata(@uri,@metadata)
    s.to_rdfxml
  end

  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