summaryrefslogtreecommitdiff
path: root/lib/dataset.rb
blob: 7c74f391e909ed676b12573c61e977641d9cf3d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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