summaryrefslogtreecommitdiff
path: root/lib/substance.rb
blob: f4937149ae5e165e56410367dbadbbc274f397cc (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
# Get all substances
get "/substance/?" do
  substances = Substance.all
  case @accept
  when "text/uri-list"
    uri_list = substances.collect{|substance| uri("/substance/#{substance.id}")}
    return uri_list.join("\n") + "\n"
  when "application/json"
    list = substances.collect{|substance| uri("/substance/#{substance.id}")}
    substances = JSON.parse list.to_json
    return JSON.pretty_generate substances
  else
    bad_request_error "Mime type #{@accept} is not supported."
  end
end

# Get a substance by ID
get "/substance/:id/?" do
  case @accept
  when "application/json"
    mongoid = /^[a-f\d]{24}$/i
    halt 400, "Input #{params[:id]} is no valid ID.".to_json unless params[:id].match(mongoid)
    substance = Substance.find params[:id]
    if substance
      out = {"compound": {"id": substance.id, "inchi": substance.inchi, "smiles": substance.smiles, "warnings": substance.warnings}}
      response['Content-Type'] = @accept
      return JSON.pretty_generate JSON.parse(out.to_json)
    else
      halt 400, "Substance with ID #{input} not found."
    end
  else
    bad_request_error "Mime type #{@accept} is not supported."
  end
end