diff options
author | gebele <gebele@in-silico.ch> | 2018-11-26 15:29:26 +0000 |
---|---|---|
committer | gebele <gebele@in-silico.ch> | 2018-11-26 15:29:26 +0000 |
commit | 9750e0309500259e9a56e267ce87984fb5bb5e53 (patch) | |
tree | 17db0a8246578729e5309ef3bf4f71404c3f7479 /lib/substance.rb | |
parent | e3ae34a75ed7a503078c3c2cdf95f1500d6e2b73 (diff) |
clean out; better response codes; prepare for batch
Diffstat (limited to 'lib/substance.rb')
-rw-r--r-- | lib/substance.rb | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/lib/substance.rb b/lib/substance.rb index fef1b7e..f493714 100644 --- a/lib/substance.rb +++ b/lib/substance.rb @@ -6,24 +6,28 @@ get "/substance/?" do 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 + 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 +# Get a substance by ID get "/substance/:id/?" do case @accept when "application/json" - substance = Substance.find :id => params[:id] - not_found_error "Substance with id: #{params[:id]} not found." unless substance - substance[:URI] = uri("/substance/#{substance.id}") - return substance.to_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 |