summaryrefslogtreecommitdiff
path: root/application.rb
blob: 2742218f6bfed2329c8557de46da82b9b161c0af (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# SETUP
[ 'rubygems', 'redis', 'opentox-ruby-api-wrapper' ].each do |lib|
  require lib
end

case ENV['RACK_ENV']
when 'production'
  @@redis = Redis.new :db => 0
when 'development'
  @@redis = Redis.new :db => 1
when 'test'
  @@redis = Redis.new :db => 2
  @@redis.flush_db
end

set :default_content, :yaml

helpers do

	def not_found?
		halt 404, "Dataset \"#{params[:name]}\" not found." unless @@redis.set_member? "datasets", sanitize_key(params[:name])
	end

	def sanitize_key(k)
		k.gsub(/\s|\n/,'_')
	end

	def name(uri)
		URI.decode File.basename(uri)
	end

	def uri(name)
		uri = url_for("/", :full) + URI.escape(URI.unescape(name)) # avoid escaping escaped names
	end

	def	add_feature(name, record)
		compound = record[0]
		feature = record[1] 
		@@redis.set_add name + '::compounds', compound
		@@redis.set_add name + '::features', name + '::' + feature
		@@redis.set_add compound, name + '::' + feature
		@@redis.set_add feature, compound
	end

	def delete_dataset(name)
		# stale features/compounds under compound and name::feature ??
		@@redis.delete name + '::compounds'
		@@redis.delete name + '::features'
		@@redis.set_delete "datasets", name
	end

end

## REST API

get '/?' do
	@@redis.set_members("datasets").collect{|d| uri(d)}.join("\n")
end

get '/:name' do
  not_found?
  @dataset = {:uri => uri(params[:name]), :name => params[:name]}
  respond_to do |format|
    format.yaml { @dataset.to_yaml }
    format.xml {  builder :dataset }
  end
end

get '/:name/name' do
  not_found?
  URI.decode(params[:name])
end

get '/:name/compounds' do
  not_found?
  @@redis.set_members(URI.encode(params[:name]) + "::compounds").join("\n")
end

get '/:name/features' do
  not_found?
  @@redis.set_members(URI.encode(params[:name]) + "::features").join("\n")
end

get '/:name/:type/*/*/intersection' do
  # CHECK/TEST
  @@redis.set_intersect(params[:splat][0], params[:splat][1], URI.encode(params[:name]) + '/' + params[:type]).join("\n")
end

get '/:name/:type/*/*/union' do
  # CHECK/TEST
  @@redis.set_union(params[:splat][0], params[:splat][1], URI.encode(params[:name]) + '/' + params[:type]).join("\n")
end

get '/:name/*/features' do 
  compound = URI.encode(params[:splat].first, /[^#{URI::PATTERN::UNRESERVED}]/)
  @@redis.set_intersect(params[:name] + '::features', compound ).join("\n")
  @@redis.set_intersect(params[:name] + '::features', compound ).join("\n")
end

get '/:name/*/compounds' do 
  feature = params[:splat].first
  @@redis.set_intersect(feature, params[:name] + '::compounds').join("\n")
end

post '/?' do
  #protected!
	name = sanitize_key params[:name]
  halt 403, "Dataset \"#{name}\" exists - please choose another name." if @@redis.set_member?("datasets", name)

  @@redis.set_add "datasets", name

  if params[:file]
		File.open(params[:file][:tempfile].path).each_line do |line|
			record = line.chomp.split(/,\s*/)
			add_feature(name, record)
		end
  end
	uri name 
end

put '/:name/?' do
  #protected!
  pass if params[:finished]
  not_found?
	name = sanitize_key params[:name]
  compound =  params[:compound]
  feature = sanitize_key params[:feature] 
	add_feature(name, [compound,feature])
  name + " sucessfully updated."
end

delete '/:name/?' do
  # dangerous, because other datasets might refer to it
  #protected!
  not_found?
  name = sanitize_key params[:name]
	delete_dataset(name)
  "Successfully deleted dataset \"#{name}\"."
end