diff options
author | gebele <gebele@in-silico.ch> | 2018-06-11 12:46:06 +0200 |
---|---|---|
committer | gebele <gebele@in-silico.ch> | 2018-06-11 12:46:06 +0200 |
commit | 3a11ba2918795821600b7113d0758415718d263a (patch) | |
tree | 2c03bdf1ed63582b8bb865c858ce225dee791a34 /lib/dataset.rb | |
parent | 409c8a5a353235c91cbf965a885dd12da699d993 (diff) |
combine gui with rest
Diffstat (limited to 'lib/dataset.rb')
-rw-r--r-- | lib/dataset.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/dataset.rb b/lib/dataset.rb new file mode 100644 index 0000000..7c74f39 --- /dev/null +++ b/lib/dataset.rb @@ -0,0 +1,46 @@ +# Get all datasets +get "/dataset/?" do + datasets = Dataset.all + case @accept + when "text/uri-list" + uri_list = datasets.collect{|dataset| uri("/dataset/#{dataset.id}")} + return uri_list.join("\n") + "\n" + when "application/json" + datasets = JSON.parse datasets.to_json + list = [] + datasets.each{|d| list << uri("/dataset/#{d["_id"]["$oid"]}")} + return list.to_json + else + bad_request_error "Mime type #{@accept} is not supported." + end +end + +# Get a dataset +get "/dataset/:id/?" do + dataset = Dataset.find :id => params[:id] + not_found_error "Dataset with id: #{params[:id]} not found." unless dataset + case @accept + when "application/json" + dataset.data_entries.each do |k, v| + dataset.data_entries[k][:URI] = uri("/substance/#{k}") + end + dataset[:URI] = uri("/dataset/#{dataset.id}") + dataset[:substances] = uri("/dataset/#{dataset.id}/substances") + dataset[:features] = uri("/dataset/#{dataset.id}/features") + return dataset.to_json + when "text/csv", "application/csv" + return dataset.to_csv + else + bad_request_error "Mime type #{@accept} is not supported." + end +end + +# Get a dataset attribute. One of compounds, nanoparticles, substances, features +get "/dataset/:id/:attribute/?" do + dataset = Dataset.find :id => params[:id] + not_found_error "Dataset with id: #{params[:id]} not found." unless dataset + attribs = ["compounds", "nanoparticles", "substances", "features"] + return "Attribute '#{params[:attribute]}' is not available. Choose one of #{attribs.join(', ')}." unless attribs.include? params[:attribute] + out = dataset.send("#{params[:attribute]}") + return out.to_json +end |