From 3a11ba2918795821600b7113d0758415718d263a Mon Sep 17 00:00:00 2001 From: gebele Date: Mon, 11 Jun 2018 12:46:06 +0200 Subject: combine gui with rest --- lib/substance.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/substance.rb (limited to 'lib/substance.rb') diff --git a/lib/substance.rb b/lib/substance.rb new file mode 100644 index 0000000..fef1b7e --- /dev/null +++ b/lib/substance.rb @@ -0,0 +1,30 @@ +# 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 + 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 + else + bad_request_error "Mime type #{@accept} is not supported." + end +end -- cgit v1.2.3 From 9750e0309500259e9a56e267ce87984fb5bb5e53 Mon Sep 17 00:00:00 2001 From: gebele Date: Mon, 26 Nov 2018 15:29:26 +0000 Subject: clean out; better response codes; prepare for batch --- lib/substance.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'lib/substance.rb') 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 -- cgit v1.2.3 From 741701df8ff0861b3607a30e9aaf8b8a0c303cdf Mon Sep 17 00:00:00 2001 From: gebele Date: Thu, 13 Jun 2019 15:28:59 +0000 Subject: update with API --- lib/substance.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'lib/substance.rb') diff --git a/lib/substance.rb b/lib/substance.rb index f493714..5d57505 100644 --- a/lib/substance.rb +++ b/lib/substance.rb @@ -1,5 +1,5 @@ # Get all substances -get "/substance/?" do +get "/api/substance/?" do substances = Substance.all case @accept when "text/uri-list" @@ -10,25 +10,25 @@ get "/substance/?" do substances = JSON.parse list.to_json return JSON.pretty_generate substances else - bad_request_error "Mime type #{@accept} is not supported." + halt 400, "Mime type #{@accept} is not supported." end end # Get a substance by ID -get "/substance/:id/?" do +get "/api/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 + 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 #{input} not found." + halt 400, "Substance with ID #{params[:id]} not found." end else - bad_request_error "Mime type #{@accept} is not supported." + halt 400, "Mime type #{@accept} is not supported." end end -- cgit v1.2.3