summaryrefslogtreecommitdiff
path: root/lib/substance.rb
blob: 1116e65b832673b59f0e9c7f2b5021fd03219378 (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
# 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"
    substances = JSON.parse substances.to_json
    substances.each_index do |idx|
      substances[idx][:URI] = uri("/substance/#{substances[idx]["_id"]["$oid"]}")
    end
    return substances.to_json
  else
    bad_request_error "Mime type #{@accept} is not supported."
  end
end

# Get a substance
get "/substance/:id/?" do
  substance = Substance.find :id => params[:id]
  resource_not_found_error "Substance with id: #{params[:id]} not found." unless substance
  substance[:URI] = uri("/substance/#{substance.id}")
  return substance.to_json
end