summaryrefslogtreecommitdiff
path: root/lib/dataset.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2019-09-03 13:45:36 +0200
committerChristoph Helma <helma@in-silico.ch>2019-09-03 13:45:36 +0200
commitd1032e4f40d9fbb212e85e0db4f0ecd2e8ac9a88 (patch)
tree48922d60d750839dacd5d0a4a6e50ea3fe68da63 /lib/dataset.rb
parent5bb4c24c6cfc1ddfae14eb9543b283baae2d75be (diff)
parenta84d9eabf1b921086a688f81df28b0f21ba4df19 (diff)
development merged, git links in FAQ.md fixed1.4.0
Diffstat (limited to 'lib/dataset.rb')
-rw-r--r--lib/dataset.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/dataset.rb b/lib/dataset.rb
new file mode 100644
index 0000000..51407ca
--- /dev/null
+++ b/lib/dataset.rb
@@ -0,0 +1,33 @@
+# Get all datasets
+get "/api/dataset/?" do
+ datasets = Dataset.all #.limit(100)
+ case @accept
+ when "application/json"
+ list = datasets.collect{|dataset| uri("/api/dataset/#{dataset.id}")}.to_json
+ return list
+ else
+ halt 400, "Mime type #{@accept} is not supported."
+ end
+end
+
+# Get a dataset
+get "/api/dataset/:id/?" do
+ dataset = Dataset.find :id => params[:id]
+ halt 400, "Dataset with id: #{params[:id]} not found." unless dataset
+ case @accept
+ when "text/csv", "application/csv"
+ return File.read dataset.source
+ else
+ bad_request_error "Mime type #{@accept} is not supported."
+ end
+end
+
+# Get a dataset attribute. One of compounds, nanoparticles, substances, features
+get "/api/dataset/:id/:attribute/?" do
+ dataset = Dataset.find :id => params[:id]
+ halt 400, "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