diff options
author | Christoph Helma <helma@in-silico.ch> | 2019-09-03 13:45:36 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2019-09-03 13:45:36 +0200 |
commit | d1032e4f40d9fbb212e85e0db4f0ecd2e8ac9a88 (patch) | |
tree | 48922d60d750839dacd5d0a4a6e50ea3fe68da63 /lib/substance.rb | |
parent | 5bb4c24c6cfc1ddfae14eb9543b283baae2d75be (diff) | |
parent | a84d9eabf1b921086a688f81df28b0f21ba4df19 (diff) |
development merged, git links in FAQ.md fixed1.4.0
Diffstat (limited to 'lib/substance.rb')
-rw-r--r-- | lib/substance.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/substance.rb b/lib/substance.rb new file mode 100644 index 0000000..5d57505 --- /dev/null +++ b/lib/substance.rb @@ -0,0 +1,34 @@ +# Get all substances +get "/api/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 + halt 400, "Mime type #{@accept} is not supported." + end +end + +# Get a substance by ID +get "/api/substance/:id/?" do + case @accept + when "application/json" + substance = Substance.find params[:id] + if substance + out = {"compound": {"id": substance.id, + "inchi": substance.inchi, + "smiles": substance.smiles + }} + return JSON.pretty_generate JSON.parse(out.to_json) + else + halt 400, "Substance with ID #{params[:id]} not found." + end + else + halt 400, "Mime type #{@accept} is not supported." + end +end |