summaryrefslogtreecommitdiff
path: root/application.rb
blob: 7d3de388480403e4273bff22efc91cebca8146e3 (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
require 'rubygems'
gem "opentox-ruby", "~> 2"
require 'opentox-ruby'

set :lock, true

class PredictionCache < Ohm::Model
  attribute :compound_uri
  attribute :model_uri
  attribute :dataset_uri

  index :compound_uri
  index :model_uri
end

before do
  @accept = request.env['HTTP_ACCEPT']
  @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil?
  response['Content-Type'] = @accept
  @id = request.path_info.match(/^\/\d+/)
  unless @id.nil?
    @id = @id.to_s.sub(/\//,'').to_i

    @uri = uri @id
    @yaml_file = "public/#{@id}.yaml"
    raise OpenTox::NotFoundError.new "Model #{@id} not found." unless File.exists? @yaml_file
  end

  # make sure subjectid is not included in params, subjectid is set as member variable
  params.delete(:subjectid) 
end

require 'lazar.rb'

helpers do

  def next_id
    id = Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last
    id = 0 if id.nil?
    id + 1
  end

  def uri(id)
    url_for "/#{id}", :full
  end

  def activity(a)
    case a.to_s
    when "true"
      act = "active"
    when "false"
      act = "inactive"
    else
      act = "not available"
    end
    act
  end
end

get '/?' do # get index of models
  response['Content-Type'] = 'text/uri-list'
  Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n"
end

delete '/:id/?' do
  LOGGER.debug "Deleting model with id "+@id.to_s
  begin
    FileUtils.rm @yaml_file
    if @subjectid and !File.exists? @yaml_file and @uri
      begin
        res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid)
        LOGGER.debug "Policy deleted for Model URI: #{@uri} with result: #{res}"
      rescue
        LOGGER.warn "Policy delete error for Model URI: #{@uri}"
      end
    end
    response['Content-Type'] = 'text/plain'
    "Model #{@id} deleted."
  rescue
    raise OpenTox::NotFoundError.new "Model #{@id} does not exist."
  end
end


delete '/?' do
  # TODO delete datasets
  FileUtils.rm Dir["public/*.yaml"]
  PredictionCache.all.each {|cache| cache.delete }
  response['Content-Type'] = 'text/plain'
  "All models and cached predictions deleted."
end