summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2010-08-09 09:48:47 +0200
committerChristoph Helma <helma@in-silico.ch>2010-08-09 09:48:47 +0200
commit5576a7eea4bef9189846cf005cc166221a006778 (patch)
tree146553b540253075f1d7884c504145cdf6f26677
parent031901f20b8aab63d0126121efa40cc08ddcb62c (diff)
parentf849a30c18ed64bbaa67c2a848ac223193e98b86 (diff)
Merge commit 'mguetlein/test' into development
Conflicts: application.rb
-rwxr-xr-xapplication.rb187
1 files changed, 94 insertions, 93 deletions
diff --git a/application.rb b/application.rb
index 42552f1..476445e 100755
--- a/application.rb
+++ b/application.rb
@@ -3,18 +3,19 @@ gem "opentox-ruby-api-wrapper", "= 1.6.0"
require 'opentox-ruby-api-wrapper'
class Dataset
- include DataMapper::Resource
- property :id, Serial
- property :uri, String, :length => 255
- property :file, String, :length => 255
- property :yaml, Text, :length => 2**32-1
- property :owl, Text, :length => 2**32-1
- property :created_at, DateTime
+ include DataMapper::Resource
+ property :id, Serial
+ property :uri, String, :length => 255
+ property :file, String, :length => 255
+ property :yaml, Text, :length => 2**32-1
+ property :owl, Text, :length => 2**32-1
+ property :created_at, DateTime
- def to_owl
- data = YAML.load(yaml)
- owl = OpenTox::Owl.create 'Dataset', uri
- owl.set "title", data.title
+ def to_owl
+
+ data = YAML.load(yaml)
+ owl = OpenTox::Owl.create 'Dataset', uri
+ owl.set "title", data.title
owl.set "creator", data.creator
if data.compounds
data.compounds.each do |compound|
@@ -22,7 +23,7 @@ class Dataset
end
end
owl.rdf
- end
+ end
end
@@ -31,44 +32,44 @@ DataMapper.auto_upgrade!
## REST API
get '/?' do
- response['Content-Type'] = 'text/uri-list'
- Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n"
+ response['Content-Type'] = 'text/uri-list'
+ Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n"
end
get '/:id' do
- accept = request.env['HTTP_ACCEPT']
- accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil?
- # workaround for browser links
- case params[:id]
- when /.yaml$/
- params[:id].sub!(/.yaml$/,'')
- accept = 'application/x-yaml'
- when /.rdf$/
- params[:id].sub!(/.rdf$/,'')
- accept = 'application/rdf+xml'
- when /.xls$/
- params[:id].sub!(/.xls$/,'')
- accept = 'application/vnd.ms-excel'
- end
- begin
- dataset = Dataset.get(params[:id])
+ accept = request.env['HTTP_ACCEPT']
+ accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil?
+ # workaround for browser links
+ case params[:id]
+ when /.yaml$/
+ params[:id].sub!(/.yaml$/,'')
+ accept = 'application/x-yaml'
+ when /.rdf$/
+ params[:id].sub!(/.rdf$/,'')
+ accept = 'application/rdf+xml'
+ when /.xls$/
+ params[:id].sub!(/.xls$/,'')
+ accept = 'application/vnd.ms-excel'
+ end
+ begin
+ dataset = Dataset.get(params[:id])
halt 404, "Dataset #{params[:id]} not found." unless dataset
- rescue => e
- raise e.message + e.backtrace
- halt 404, "Dataset #{params[:id]} not found."
- end
- halt 404, "Dataset #{params[:id]} not found." if dataset.nil? # not sure how an empty cataset can be returned, but if this happens stale processes keep runing at 100% cpo
- case accept
- when /rdf/ # redland sends text/rdf instead of application/rdf+xml
- response['Content-Type'] = 'application/rdf+xml'
- unless dataset.owl # lazy owl creation
- dataset.owl = dataset.to_owl
- dataset.save
- end
- dataset.owl
- when /yaml/
- response['Content-Type'] = 'application/x-yaml'
- dataset.yaml
+ rescue => e
+ raise e.message + e.backtrace
+ halt 404, "Dataset #{params[:id]} not found."
+ end
+ halt 404, "Dataset #{params[:id]} not found." if dataset.nil? # not sure how an empty cataset can be returned, but if this happens stale processes keep runing at 100% cpo
+ case accept
+ when /rdf/ # redland sends text/rdf instead of application/rdf+xml
+ response['Content-Type'] = 'application/rdf+xml'
+ unless dataset.owl # lazy owl creation
+ dataset.owl = dataset.to_owl
+ dataset.save
+ end
+ dataset.owl
+ when /yaml/
+ response['Content-Type'] = 'application/x-yaml'
+ dataset.yaml
when /ms-excel/
require 'spreadsheet'
response['Content-Type'] = 'application/vnd.ms-excel'
@@ -81,12 +82,12 @@ get '/:id' do
YAML.load(dataset.yaml).data.each do |line|
begin
smilestring = RestClient.get(line[0], :accept => 'chemical/x-daylight-smiles').to_s
- if line[1]
- val = line[1][0].first[1]
- LOGGER.debug val
- #line[1][0] ? val = line[1][0].first[1] #? "1" : "0" : val = ""
- sheet.update_row(i, smilestring , val)
- end
+ if line[1]
+ val = line[1][0].first[1]
+ LOGGER.debug val
+ #line[1][0] ? val = line[1][0].first[1] #? "1" : "0" : val = ""
+ sheet.update_row(i, smilestring , val)
+ end
i+=1
rescue
end
@@ -99,66 +100,66 @@ get '/:id' do
end
tmp.close!
else
- halt 400, "Unsupported MIME type '#{accept}'"
- end
+ halt 400, "Unsupported MIME type '#{accept}'"
+ end
end
get '/:id/features/:feature_id/?' do
- OpenTox::Dataset.find(url_for("/#{params[:id]}", :full)).feature(params[:feature_id])
+ OpenTox::Dataset.find(url_for("/#{params[:id]}", :full)).feature(params[:feature_id])
end
get '/:id/features/?' do
- YAML.load(Dataset.get(params[:id]).yaml).features.join("\n") + "\n"
+ YAML.load(Dataset.get(params[:id]).yaml).features.join("\n") + "\n"
end
get '/:id/compounds/?' do
- YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n"
+ YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n"
end
post '/?' do
- dataset = Dataset.new
- dataset.save
- dataset.uri = url_for("/#{dataset.id}", :full)
- content_type = request.content_type
- content_type = "application/rdf+xml" if content_type.nil?
- case request.content_type
- when /yaml/
- dataset.yaml = request.env["rack.input"].read
- #when /csv/
- #dataset.yaml = csv2yaml request.env["rack.input"].read
- #when "application/rdf+xml"
- #dataset.yaml = owl2yaml request.env["rack.input"].read
- else
- halt 404, "MIME type \"#{request.content_type}\" not supported."
- end
- begin
- #dataset.owl = d.rdf
+ dataset = Dataset.new
+ dataset.save
+ dataset.uri = url_for("/#{dataset.id}", :full)
+ content_type = request.content_type
+ content_type = "application/rdf+xml" if content_type.nil?
+ case request.content_type
+ when /yaml/
+ dataset.yaml = request.env["rack.input"].read
+# when /csv/
+# dataset.yaml = csv2yaml request.env["rack.input"].read
+ when "application/rdf+xml"
+ dataset.yaml = YAML.load(OpenTox::Dataset.owl_to_yaml(request.env["rack.input"].read,dataset.uri))
+ else
+ halt 404, "MIME type \"#{request.content_type}\" not supported."
+ end
+ begin
+ #dataset.owl = d.rdf
#dataset.uri = uri
- raise "saving failed: "+dataset.errors.inspect unless dataset.save
- rescue => e
- LOGGER.error e.message
- LOGGER.info e.backtrace
- halt 500, "Could not save dataset #{dataset.uri}."
- end
- LOGGER.debug "#{dataset.uri} saved."
- response['Content-Type'] = 'text/uri-list'
- dataset.uri + "\n"
+ raise "saving failed: "+dataset.errors.inspect unless dataset.save
+ rescue => e
+ LOGGER.error e.message
+ LOGGER.info e.backtrace
+ halt 500, "Could not save dataset #{dataset.uri}."
+ end
+ LOGGER.debug "#{dataset.uri} saved."
+ response['Content-Type'] = 'text/uri-list'
+ dataset.uri + "\n"
end
delete '/:id/?' do
- begin
- dataset = Dataset.get(params[:id])
- dataset.destroy!
- response['Content-Type'] = 'text/plain'
- "Dataset #{params[:id]} deleted."
- rescue
- halt 404, "Dataset #{params[:id]} does not exist."
- end
+ begin
+ dataset = Dataset.get(params[:id])
+ dataset.destroy!
+ response['Content-Type'] = 'text/plain'
+ "Dataset #{params[:id]} deleted."
+ rescue
+ halt 404, "Dataset #{params[:id]} does not exist."
+ end
end
delete '/?' do
Dataset.auto_migrate!
- response['Content-Type'] = 'text/plain'
- "All datasets deleted."
+ response['Content-Type'] = 'text/plain'
+ "All datasets deleted."
end