summaryrefslogtreecommitdiff
path: root/application.rb
blob: 7ccaf05138923f247ef4ec0e15cf3ec8b780c813 (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
include OpenTox

require 'rack/cors'
#require_relative "helper.rb"

# add CORS support for swagger
use Rack::Cors do |config|
  config.allow do |allow|
    allow.origins '*'
    allow.resource "/#{SERVICE}/*",
      :methods => [:head, :get, :post, :put, :delete, :options],
      :headers => :any,
      :max_age => 0
  end
end
before do
  @accept = request.env['HTTP_ACCEPT']
  response['Content-Type'] = @accept
end
 
# route to swagger API file
get "/api/api.json" do
  response['Content-Type'] = "application/json"
  api_file = File.join("api", "api.json")
  bad_request_error "API Documentation in Swagger JSON is not implemented.", uri("/#{SERVICE}/api") unless File.exists?(api_file)
  File.read(api_file)
end


# Get a list of all prediction models
# @param [Header] Accept one of text/uri-list,
# @return [text/uri-list] list of all prediction models
get "/model/?" do
  @models = OpenTox::Model::Prediction.all
  uri_list = @models.collect{|model| uri("/model/#{model.model_id}")}
  case @accept
  when "text/uri-list"
    return uri_list.join("\n") + "\n"
  else
    bad_request_error "Mime type #{@accept} is not supported."
  end
end

get "/model/:id/?" do
  @model = OpenTox::Model::Lazar.find params[:id]
  return @model.to_json
end



post "/model/:id/?" do
  @identifier = params[:identifier]
  begin
    # get compound from SMILES
    compound = Compound.from_smiles @identifier
  rescue
    @error_report = "Attention, '#{params[:identifier]}' is not a valid SMILES string."
    return @error_report
  end
  model = OpenTox::Model::Lazar.find params[:id]
  prediction = model.predict(compound)
  return prediction.to_json
end


=begin
post "/model/?" do
  parse_input
  case @content_type
  when "text/csv", "text/comma-separated-values"
    model = OpenTox::Model::Prediction.from_csv_file @body
  else
    bad_request_error "Mime type #{@content_type} is not supported."
  end
  response['Content-Type'] = "text/uri-list"
  model.model_id
end

delete "model/:id/?" do
  model = OpenTox::Model::Lazar.find params[:id]
  model.delete
end
=end