From f849a30c18ed64bbaa67c2a848ac223193e98b86 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 6 Aug 2010 16:52:49 +0200 Subject: replace tabs with 2spaces, changed only one line: owl_to_yaml --- application.rb | 203 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 102 insertions(+), 101 deletions(-) diff --git a/application.rb b/application.rb index acc5aba..7c653a0 100755 --- a/application.rb +++ b/application.rb @@ -5,18 +5,19 @@ require 'opentox-ruby-api-wrapper' LOGGER.progname = File.expand_path(__FILE__) 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| @@ -24,7 +25,7 @@ class Dataset end end owl.rdf - end + end end @@ -33,44 +34,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' @@ -83,12 +84,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 @@ -101,77 +102,77 @@ 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 =begin - Dataset.all.each do |d| - begin - File.delete d.file - rescue - LOGGER.error "Cannot delete dataset file '#{d.file}'" - end - #d.destroy! - end + Dataset.all.each do |d| + begin + File.delete d.file + rescue + LOGGER.error "Cannot delete dataset file '#{d.file}'" + end + #d.destroy! + end =end Dataset.auto_migrate! - response['Content-Type'] = 'text/plain' - "All datasets deleted." + response['Content-Type'] = 'text/plain' + "All datasets deleted." end -- cgit v1.2.3