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