From 9817e9e9ad8ec1776ba7ee3d03635401f0ad0d2c Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 25 Aug 2010 14:49:33 +0200 Subject: opentox-api-wrapper bumped to 1.6.6 --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index a2cd182..e7bf635 100755 --- a/application.rb +++ b/application.rb @@ -1,5 +1,5 @@ require 'rubygems' -gem "opentox-ruby-api-wrapper", "= 1.6.5" +gem "opentox-ruby-api-wrapper", "= 1.6.6" require 'opentox-ruby-api-wrapper' class Dataset -- cgit v1.2.3 From 971155a2f06266ffb5fa1234d4901a174dac5a3a Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 3 Sep 2010 17:33:34 +0200 Subject: add metadata functionality --- application.rb | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 7494748..fea6755 100755 --- a/application.rb +++ b/application.rb @@ -36,7 +36,11 @@ get '/?' do Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n" end -get '/:id' do +# provides dataset in various formats +# +# only_metadata=true => compounds and features are removed +# +def get_dataset( params, request, response, only_metadata=false ) accept = request.env['HTTP_ACCEPT'] accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? # workaround for browser links @@ -59,14 +63,27 @@ get '/:id' do 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 + + if only_metadata # remove compounds and feature data from yaml + d = YAML.load(dataset.yaml) + def d.to_yaml_properties + super - [ "@compounds", "@features", "@data"] + end + dataset.yaml = d.to_yaml + end + 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 + if only_metadata + dataset.to_owl + else + unless dataset.owl # lazy owl creation + dataset.owl = dataset.to_owl + dataset.save + end + dataset.owl end - dataset.owl when /yaml/ response['Content-Type'] = 'application/x-yaml' dataset.yaml @@ -104,6 +121,14 @@ get '/:id' do end end +get '/:id' do + get_dataset(params, request, response) +end + +get '/:id/metadata/?' do + get_dataset(params, request, response, true) +end + get '/:id/features/:feature_id/?' do OpenTox::Dataset.find(url_for("/#{params[:id]}", :full)).feature(params[:feature_id]) end -- cgit v1.2.3 From abb1d628ec5109dc7e74bafb4de1d575dcf3d075 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 7 Sep 2010 13:02:05 +0200 Subject: owl cache temporarily deactivated --- application.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 2ace1db..c361a5d 100755 --- a/application.rb +++ b/application.rb @@ -13,15 +13,18 @@ class Dataset def to_owl data = YAML.load(yaml) +# nt = "<#{uri}> <#{data.title}> .\n" +# nt += "<#{uri}> <creator> <#{data.creator}> .\n" owl = OpenTox::Owl.create 'Dataset', uri owl.set "title", data.title - owl.set "creator", data.creator + owl.set "creator", data.creator if data.creator if data.compounds data.compounds.each do |compound| owl.add_data_entries compound,data.data[compound] end end owl.rdf +# nt end end @@ -77,11 +80,12 @@ def get_dataset( params, request, response, only_metadata=false ) if only_metadata dataset.to_owl else - unless dataset.owl # lazy owl creation - dataset.owl = dataset.to_owl - dataset.save - end - dataset.owl + #unless dataset.owl # lazy owl creation + #dataset.owl = dataset.to_owl + #dataset.save + #end + #dataset.owl + dataset.to_owl end when /yaml/ response['Content-Type'] = 'application/x-yaml' -- cgit v1.2.3 From 57d792d260aec3e4220f5195ae3a7f1b108ab2fe Mon Sep 17 00:00:00 2001 From: Christoph Helma <helma@in-silico.ch> Date: Mon, 13 Sep 2010 17:09:37 +0200 Subject: intermediary commit for new owl serializer --- application.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index c361a5d..2bdc716 100755 --- a/application.rb +++ b/application.rb @@ -13,18 +13,18 @@ class Dataset def to_owl data = YAML.load(yaml) -# nt = "<#{uri}> <title> <#{data.title}> .\n" -# nt += "<#{uri}> <creator> <#{data.creator}> .\n" - owl = OpenTox::Owl.create 'Dataset', uri - owl.set "title", data.title - owl.set "creator", data.creator if data.creator - if data.compounds - data.compounds.each do |compound| - owl.add_data_entries compound,data.data[compound] - end - end - owl.rdf -# nt + beginning = Time.now + owl = OpenTox::OwlSerializer.create 'Dataset', uri + owl.annotate "title", data.title + owl.annotate "creator", data.creator if data.creator +# if data.compounds +# data.compounds.each do |compound| +# owl.add_data_entries compound,data.data[compound] +# end +# end + nt = owl.rdf + LOGGER.debug "OWL creation took #{Time.now - beginning} seconds" + nt end end -- cgit v1.2.3 From 0ef88c5d416134dbc909c08186ebdd4bc9343035 Mon Sep 17 00:00:00 2001 From: Christoph Helma <helma@in-silico.ch> Date: Fri, 22 Oct 2010 17:42:12 +0200 Subject: new API with support for external services (initial version) --- application.rb | 267 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 152 insertions(+), 115 deletions(-) mode change 100755 => 100644 application.rb (limited to 'application.rb') diff --git a/application.rb b/application.rb old mode 100755 new mode 100644 index 139c867..079614e --- a/application.rb +++ b/application.rb @@ -1,42 +1,21 @@ require 'rubygems' gem "opentox-ruby-api-wrapper", "= 1.6.6" require 'opentox-ruby-api-wrapper' +require 'parser' class Dataset + include DataMapper::Resource property :id, Serial property :uri, 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) - beginning = Time.now - owl = OpenTox::OwlSerializer.create 'Dataset', uri - owl.annotation_property uri, DC.title, data.title, XSD.string - owl.annotation_property uri, DC.creator, data.creator, XSD.string if data.creator - if data.compounds - data.compounds.each do |compound| - owl.object_property uri, OT.compound, compound, XSD.anyUri - end - end - if data.features - data.features.each do |feature| - owl.object_property uri, OT.feature, feature, XSD.anyUri - end - end - #TODO: add data entries - nt = owl.rdf - LOGGER.debug "OWL creation took #{Time.now - beginning} seconds" - nt - end - end DataMapper.auto_upgrade! + ## REST API get '/?' do @@ -44,142 +23,199 @@ get '/?' do Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n" end -# provides dataset in various formats -# -# only_metadata=true => compounds and features are removed -# -def get_dataset( params, request, response, only_metadata=false ) +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]) - halt 404, "Dataset #{params[:id]} not found." unless dataset + dataset = OpenTox::Dataset.from_yaml(Dataset.get(params[:id]).yaml) + halt 404, "Dataset #{params[:id]} not found." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu rescue => e - raise e.message + e.backtrace + LOGGER.error e.message + LOGGER.info 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 dataset can be returned, but if this happens stale processes keep runing at 100% cpu - - if only_metadata # remove compounds and feature data from yaml - d = YAML.load(dataset.yaml) - def d.to_yaml_properties - super - [ "@compounds", "@features", "@data"] - end - dataset.yaml = d.to_yaml - end case accept + when /rdf/ # redland sends text/rdf instead of application/rdf+xml - response['Content-Type'] = 'application/rdf+xml' - if only_metadata - dataset.to_owl + file = "public/#{params[:id]}.rdfxml" + if File.exists? file + response['Content-Type'] = 'application/rdf+xml' + #redirect url_for("/#{params[:id]}",:full)+ ".rdfxml" # confuses curl (needs -L flag) + File.read(file) else - #unless dataset.owl # lazy owl creation - #dataset.owl = dataset.to_owl - #dataset.save - #end - #dataset.owl - dataset.to_owl + task_uri = OpenTox::Task.as_task("Converting dataset to OWL-DL (RDF/XML)", url_for(params[:id],:full)) do + File.open(file,"w+") { |f| f.puts dataset.rdfxml } + url_for("/#{params[:id]}",:full)+ ".rdfxml" + end + response['Content-Type'] = 'text/uri-list' + halt 202,task_uri.to_s+"\n" end + when /yaml/ response['Content-Type'] = 'application/x-yaml' dataset.yaml + + when "text/csv" + response['Content-Type'] = 'text/csv' + dataset.csv + when /ms-excel/ - require 'spreadsheet' - response['Content-Type'] = 'application/vnd.ms-excel' - Spreadsheet.client_encoding = 'UTF-8' - book = Spreadsheet::Workbook.new - tmp = Tempfile.new('opentox-feature-xls') - sheet = book.create_worksheet :name => 'Training Data' - sheet.column(0).width = 100 - i = 0 - 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 - i+=1 - rescue + file = "public/#{params[:id]}.xls" + if File.exists? file + response['Content-Type'] = 'application/ms-excel' + File.read(file) + else + task_uri = OpenTox::Task.as_task("Converting dataset to Excel", url_for(params[:id],:full)) do + dataset.excel.write(file) + url_for("/#{params[:id]}",:full)+ ".xls" end + response['Content-Type'] = 'text/uri-list' + halt 202,task_uri.to_s+"\n" end - begin - book.write tmp.path - return tmp - rescue - - end - tmp.close! + else - halt 400, "Unsupported MIME type '#{accept}'" + halt 404, "Content-type #{accept} not supported." end end -get '/:id' do - get_dataset(params, request, response) -end - get '/:id/metadata/?' do - get_dataset(params, request, response, true) + + metadata = YAML.load(Dataset.get(params[:id]).yaml).metadata + accept = request.env['HTTP_ACCEPT'] + accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? + + case accept + when /rdf/ # redland sends text/rdf instead of application/rdf+xml + response['Content-Type'] = 'application/rdf+xml' + serializer = OpenTox::Serializer::Owl.new + serializer.add_metadata url_for(params[:id],:full), "Dataset", metadata + serializer.rdfxml + when /yaml/ + response['Content-Type'] = 'application/x-yaml' + metadata.to_yaml + end + end -get '/:id/features/:feature_id/?' do - OpenTox::Dataset.find(url_for("/#{params[:id]}", :full)).feature(params[:feature_id]) +get %r{/(\d+)/feature/(.*)$} do |id,feature| + + feature_uri = url_for("/#{id}/feature/#{feature}",:full) + dataset = OpenTox::Dataset.from_yaml(Dataset.get(id).yaml) + metadata = dataset.features[feature_uri] + + accept = request.env['HTTP_ACCEPT'] + accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? + + case accept + when /rdf/ # redland sends text/rdf instead of application/rdf+xml + response['Content-Type'] = 'application/rdf+xml' + serializer = OpenTox::Serializer::Owl.new + serializer.add_feature feature_uri, metadata + serializer.rdfxml + when /yaml/ + response['Content-Type'] = 'application/x-yaml' + metadata.to_yaml + end + end get '/:id/features/?' do - YAML.load(Dataset.get(params[:id]).yaml).features.join("\n") + "\n" + response['Content-Type'] = 'text/uri-list' + YAML.load(Dataset.get(params[:id]).yaml).features.keys.join("\n") + "\n" end get '/:id/compounds/?' do + response['Content-Type'] = 'text/uri-list' YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n" end -post '/?' do +post '/?' do # create an empty dataset + response['Content-Type'] = 'text/uri-list' + dataset = Dataset.create + dataset.update(:uri => url_for("/#{dataset.id}", :full)) + dataset.update(:yaml => OpenTox::Dataset.new(url_for("/#{dataset.id}", :full)).to_yaml) + "#{dataset.uri}\n" +end + +post '/:id/?' do # insert data into a dataset + + begin + dataset = Dataset.get(params[:id]) + halt 404, "Dataset #{params[:id]} not found." unless dataset + data = request.env["rack.input"].read - 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 + + case content_type + when /yaml/ - dataset.yaml = request.env["rack.input"].read + dataset.update(:yaml => data) + when "application/rdf+xml" - dataset.yaml = OpenTox::Dataset.owl_to_yaml(request.env["rack.input"].read,dataset.uri) + dataset.update(:yaml => OpenTox::Dataset.from_rdfxml(data).yaml) + + when /multipart\/form-data/ # for file uploads + + case params[:file][:type] + + when /yaml/ + dataset.update(:yaml => params[:file][:tempfile].read) + + when "application/rdf+xml" + dataset.update(:yaml => OpenTox::Dataset.from_rdfxml(params[:file][:tempfile]).yaml) + + when "text/csv" + metadata = {DC.title => File.basename(params[:file][:filename],".csv"), OT.hasSource => File.basename(params[:file][:filename])} + d = OpenTox::Dataset.from_csv(File.open(params[:file][:tempfile]).read) + d.add_metadata metadata + dataset.update(:yaml => d.yaml, :uri => d.uri) + + when /ms-excel/ + extension = File.extname(params[:file][:filename]) + metadata = {DC.title => File.basename(params[:file][:filename],extension), OT.hasSource => File.basename(params[:file][:filename])} + case extension + when ".xls" + xls = params[:file][:tempfile].path + ".xls" + File.rename params[:file][:tempfile].path, xls # roo needs these endings + book = Excel.new xls + when ".xlsx" + xlsx = params[:file][:tempfile].path + ".xlsx" + File.rename params[:file][:tempfile].path, xlsx # roo needs these endings + book = Excel.new xlsx + else + halt 404, "#{params[:file][:filename]} is not a valid Excel input file." + end + d = OpenTox::Dataset.from_spreadsheet(book) + d.add_metadata metadata + dataset.update(:yaml => d.yaml, :uri => d.uri) + + else + halt 404, "MIME type \"#{params[:file][:type]}\" not supported." + end + else - halt 404, "MIME type \"#{request.content_type}\" not supported." + halt 404, "MIME type \"#{content_type}\" not supported." end - begin - 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" + + FileUtils.rm Dir["public/#{params[:id]}.*"] # delete all serialization files, will be recreated at next reques + response['Content-Type'] = 'text/uri-list' + "#{dataset.uri}\n" + + rescue => e + LOGGER.error e.message + LOGGER.info e.backtrace + halt 500, "Could not save dataset #{dataset.uri}." + end end delete '/:id/?' do begin dataset = Dataset.get(params[:id]) + FileUtils.rm Dir["public/#{params[:id]}.*"] dataset.destroy! response['Content-Type'] = 'text/plain' "Dataset #{params[:id]} deleted." @@ -189,6 +225,7 @@ delete '/:id/?' do end delete '/?' do + Dataset.all {|d| FileUtils.rm Dir["public/#{d.id}.*"] } Dataset.auto_migrate! response['Content-Type'] = 'text/plain' "All datasets deleted." -- cgit v1.2.3 From 9f489392359ab7d5fc3f80354acee5007cc89c55 Mon Sep 17 00:00:00 2001 From: Christoph Helma <helma@in-silico.ch> Date: Wed, 10 Nov 2010 17:44:03 +0100 Subject: Adapted to new wrapper version, yard documentation added. --- application.rb | 343 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 213 insertions(+), 130 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 079614e..1d49ffb 100644 --- a/application.rb +++ b/application.rb @@ -11,88 +11,169 @@ class Dataset property :yaml, Text, :length => 2**32-1 property :created_at, DateTime + def load(params,request) + + data = request.env["rack.input"].read + content_type = request.content_type + content_type = "application/rdf+xml" if content_type.nil? + dataset = OpenTox::Dataset.new + + case content_type + + when /yaml/ + dataset.load_yaml(data) + + when "application/rdf+xml" + dataset.load_rdfxml(data) + + when /multipart\/form-data/ # file uploads + + case params[:file][:type] + + when /yaml/ + dataset.load_yaml(params[:file][:tempfile].read) + + when "application/rdf+xml" + dataset.load_rdfxml_file(params[:file][:tempfile]) + + when "text/csv" + dataset = OpenTox::Dataset.new @uri + dataset.load_csv(params[:file][:tempfile].read) + dataset.add_metadata({ + DC.title => File.basename(params[:file][:filename],".csv"), + OT.hasSource => File.basename(params[:file][:filename]) + }) + + when /ms-excel/ + extension = File.extname(params[:file][:filename]) + case extension + when ".xls" + xls = params[:file][:tempfile].path + ".xls" + File.rename params[:file][:tempfile].path, xls # roo needs these endings + book = Excel.new xls + when ".xlsx" + xlsx = params[:file][:tempfile].path + ".xlsx" + File.rename params[:file][:tempfile].path, xlsx # roo needs these endings + book = Excel.new xlsx + else + raise "#{params[:file][:filename]} is not a valid Excel input file." + end + dataset.load_spreadsheet(book) + dataset.add_metadata({ + DC.title => File.basename(params[:file][:filename],extension), + OT.hasSource => File.basename(params[:file][:filename]) + }) + + else + raise "MIME type \"#{params[:file][:type]}\" not supported." + end + + else + raise "MIME type \"#{@content_type}\" not supported." + end + + dataset.uri = @uri # update uri (also in metdata) + dataset.features.keys.each { |f| dataset.features[f][OT.hasSource] = dataset.metadata[OT.hasSource] unless dataset.features[f][OT.hasSource]} + update(:yaml => dataset.to_yaml) + end + + def create_representations + dataset = YAML.load yaml + ["rdfxml","xls"].each do |extension| + file = "public/#{@id}.#{extension}" + LOGGER.debug file + FileUtils.rm Dir["public/#{file}"] if File.exists? file + File.open(file,"w+") { |f| f.puts eval("dataset.to_#{extension}") } + end + end + end DataMapper.auto_upgrade! +before do + @accept = request.env['HTTP_ACCEPT'] + @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil? +end ## REST API +# Get a list of available datasets +# @return [text/uri-list] List of available datasets get '/?' do response['Content-Type'] = 'text/uri-list' Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n" end +# Get a dataset representation +# @param [Header] Accept one of `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` (default application/rdf+xml) +# @return [application/rdf+xml, application-x-yaml, text/csv, application/ms-excel] Dataset representation get '/:id' do - accept = request.env['HTTP_ACCEPT'] - accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? - - begin - dataset = OpenTox::Dataset.from_yaml(Dataset.get(params[:id]).yaml) - halt 404, "Dataset #{params[:id]} not found." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu - rescue => e - LOGGER.error e.message - LOGGER.info e.backtrace - halt 404, "Dataset #{params[:id]} not found." + extension = File.extname(params[:id]).sub(/\./,'') + unless extension.empty? + params[:id].sub!(/\.#{extension}$/,'') + case extension + when "yaml" + @accept = 'application/x-yaml' + when "csv" + @accept = 'text/csv' + when "rdfxml" + @accept = 'application/rdf+xml' + when "xls" + @accept = 'application/ms-excel' + else + halt 404, "File format #{extension} not supported." + end end + + #begin + dataset = OpenTox::Dataset.new + dataset.load_yaml(Dataset.get(params[:id]).yaml) + halt 404, "Dataset #{params[:id]} empty." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu + #rescue => e + #LOGGER.error e.message + #LOGGER.info e.backtrace + #halt 404, "Dataset #{params[:id]} not found." + #end - case accept + case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml file = "public/#{params[:id]}.rdfxml" - if File.exists? file - response['Content-Type'] = 'application/rdf+xml' - #redirect url_for("/#{params[:id]}",:full)+ ".rdfxml" # confuses curl (needs -L flag) - File.read(file) - else - task_uri = OpenTox::Task.as_task("Converting dataset to OWL-DL (RDF/XML)", url_for(params[:id],:full)) do - File.open(file,"w+") { |f| f.puts dataset.rdfxml } - url_for("/#{params[:id]}",:full)+ ".rdfxml" - end - response['Content-Type'] = 'text/uri-list' - halt 202,task_uri.to_s+"\n" - end + response['Content-Type'] = 'application/rdf+xml' + File.open(file).read when /yaml/ response['Content-Type'] = 'application/x-yaml' - dataset.yaml + dataset.to_yaml when "text/csv" response['Content-Type'] = 'text/csv' - dataset.csv + dataset.to_csv when /ms-excel/ file = "public/#{params[:id]}.xls" - if File.exists? file - response['Content-Type'] = 'application/ms-excel' - File.read(file) - else - task_uri = OpenTox::Task.as_task("Converting dataset to Excel", url_for(params[:id],:full)) do - dataset.excel.write(file) - url_for("/#{params[:id]}",:full)+ ".xls" - end - response['Content-Type'] = 'text/uri-list' - halt 202,task_uri.to_s+"\n" - end + response['Content-Type'] = 'application/ms-excel' + File.open(file).read else - halt 404, "Content-type #{accept} not supported." + halt 404, "Content-type #{@accept} not supported." end end -get '/:id/metadata/?' do +# Get metadata of the dataset +# @return [application/rdf+xml] Metadata OWL-DL +get '/:id/metadata' do metadata = YAML.load(Dataset.get(params[:id]).yaml).metadata - accept = request.env['HTTP_ACCEPT'] - accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? - case accept + case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml response['Content-Type'] = 'application/rdf+xml' serializer = OpenTox::Serializer::Owl.new - serializer.add_metadata url_for(params[:id],:full), "Dataset", metadata - serializer.rdfxml + serializer.add_metadata url_for("/#{params[:id]}",:full), metadata + serializer.to_rdfxml when /yaml/ response['Content-Type'] = 'application/x-yaml' metadata.to_yaml @@ -100,21 +181,24 @@ get '/:id/metadata/?' do end +# Get a dataset feature +# @param [Header] Accept one of `application/rdf+xml or application-x-yaml` (default application/rdf+xml) +# @return [application/rdf+xml,application/x-yaml] Feature metadata get %r{/(\d+)/feature/(.*)$} do |id,feature| +#get '/:id/feature/:feature_name/?' do - feature_uri = url_for("/#{id}/feature/#{feature}",:full) - dataset = OpenTox::Dataset.from_yaml(Dataset.get(id).yaml) + #feature_uri = url_for("/#{params[:id]}/feature/#{URI.encode(params[:feature_name])}",:full) # work around racks internal uri decoding + #dataset = YAML.load(Dataset.get(params[:id]).yaml) + feature_uri = url_for("/#{id}/feature/#{URI.encode(feature)}",:full) # work around racks internal uri decoding + dataset = YAML.load(Dataset.get(id).yaml) metadata = dataset.features[feature_uri] - - accept = request.env['HTTP_ACCEPT'] - accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? - case accept + case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml response['Content-Type'] = 'application/rdf+xml' serializer = OpenTox::Serializer::Owl.new serializer.add_feature feature_uri, metadata - serializer.rdfxml + serializer.to_rdfxml when /yaml/ response['Content-Type'] = 'application/x-yaml' metadata.to_yaml @@ -122,97 +206,93 @@ get %r{/(\d+)/feature/(.*)$} do |id,feature| end -get '/:id/features/?' do - response['Content-Type'] = 'text/uri-list' - YAML.load(Dataset.get(params[:id]).yaml).features.keys.join("\n") + "\n" +# Get a list of all features +# @param [Header] Accept one of `application/rdf+xml, application-x-yaml, text/uri-list` (default application/rdf+xml) +# @return [application/rdf+xml, application-x-yaml, text/uri-list] Feature list +get '/:id/features' do + + features = YAML.load(Dataset.get(params[:id]).yaml).features + + case @accept + when /rdf/ # redland sends text/rdf instead of application/rdf+xml + response['Content-Type'] = 'application/rdf+xml' + serializer = OpenTox::Serializer::Owl.new + features.each { |feature,metadata| serializer.add_feature feature, metadata } + serializer.to_rdfxml + when /yaml/ + response['Content-Type'] = 'application/x-yaml' + features.to_yaml + when "text/uri-list" + response['Content-Type'] = 'text/uri-list' + YAML.load(Dataset.get(params[:id]).yaml).features.keys.join("\n") + "\n" + end end -get '/:id/compounds/?' do +# Get a list of all compounds +# @return [text/uri-list] Feature list +get '/:id/compounds' do response['Content-Type'] = 'text/uri-list' YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n" end -post '/?' do # create an empty dataset +# Create a new dataset. +# +# Posting without parameters creates and saves an empty dataset (with assigned URI). +# Posting with parameters creates and saves a new dataset. +# Data can be submitted either +# - in the message body with the appropriate Content-type header or +# - as file uploads with Content-type:multipart/form-data and a specified file type +# @example +# curl -X POST -F "file=@training.csv;type=text/csv" http://webservices.in-silico.ch/dataset +# @param [Header] Content-type one of `application/x-yaml, application/rdf+xml, multipart/form-data/` +# @param [BODY] - string with data in selected Content-type +# @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` +# @return [text/uri-list] Task ID or dataset ID (empty datasets without params) +post '/?' do + @dataset = Dataset.create response['Content-Type'] = 'text/uri-list' - dataset = Dataset.create - dataset.update(:uri => url_for("/#{dataset.id}", :full)) - dataset.update(:yaml => OpenTox::Dataset.new(url_for("/#{dataset.id}", :full)).to_yaml) - "#{dataset.uri}\n" -end - -post '/:id/?' do # insert data into a dataset - - begin - dataset = Dataset.get(params[:id]) - halt 404, "Dataset #{params[:id]} not found." unless dataset - data = request.env["rack.input"].read - - content_type = request.content_type - content_type = "application/rdf+xml" if content_type.nil? - - case content_type - - when /yaml/ - dataset.update(:yaml => data) - - when "application/rdf+xml" - dataset.update(:yaml => OpenTox::Dataset.from_rdfxml(data).yaml) - - when /multipart\/form-data/ # for file uploads - - case params[:file][:type] - - when /yaml/ - dataset.update(:yaml => params[:file][:tempfile].read) - - when "application/rdf+xml" - dataset.update(:yaml => OpenTox::Dataset.from_rdfxml(params[:file][:tempfile]).yaml) - - when "text/csv" - metadata = {DC.title => File.basename(params[:file][:filename],".csv"), OT.hasSource => File.basename(params[:file][:filename])} - d = OpenTox::Dataset.from_csv(File.open(params[:file][:tempfile]).read) - d.add_metadata metadata - dataset.update(:yaml => d.yaml, :uri => d.uri) - - when /ms-excel/ - extension = File.extname(params[:file][:filename]) - metadata = {DC.title => File.basename(params[:file][:filename],extension), OT.hasSource => File.basename(params[:file][:filename])} - case extension - when ".xls" - xls = params[:file][:tempfile].path + ".xls" - File.rename params[:file][:tempfile].path, xls # roo needs these endings - book = Excel.new xls - when ".xlsx" - xlsx = params[:file][:tempfile].path + ".xlsx" - File.rename params[:file][:tempfile].path, xlsx # roo needs these endings - book = Excel.new xlsx - else - halt 404, "#{params[:file][:filename]} is not a valid Excel input file." - end - d = OpenTox::Dataset.from_spreadsheet(book) - d.add_metadata metadata - dataset.update(:yaml => d.yaml, :uri => d.uri) - - else - halt 404, "MIME type \"#{params[:file][:type]}\" not supported." - end - - else - halt 404, "MIME type \"#{content_type}\" not supported." + @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) + if params.empty? and request.env["rack.input"].read.empty? + ot_dataset = OpenTox::Dataset.new(@dataset.uri) + @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) + @dataset.create_representations + @dataset.uri + else + task_uri = OpenTox::Task.as_task("Converting and saving dataset ", @dataset.uri) do + @dataset.load params, request + @dataset.create_representations + @dataset.uri end + halt 202,task_uri.to_s+"\n" + end +end - FileUtils.rm Dir["public/#{params[:id]}.*"] # delete all serialization files, will be recreated at next reques - response['Content-Type'] = 'text/uri-list' - "#{dataset.uri}\n" - - rescue => e - LOGGER.error e.message - LOGGER.info e.backtrace - halt 500, "Could not save dataset #{dataset.uri}." +# Save a dataset, will overwrite all existing data +# +# Data can be submitted either +# - in the message body with the appropriate Content-type header or +# - as file uploads with Content-type:multipart/form-data and a specified file type +# @example +# curl -X POST -F "file=@training.csv;type=text/csv" http://webservices.in-silico.ch/dataset/1 +# @param [Header] Content-type one of `application/x-yaml, application/rdf+xml, multipart/form-data/` +# @param [BODY] - string with data in selected Content-type +# @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` +# @return [text/uri-list] Task ID +post '/:id' do + @dataset = Dataset.get(params[:id]) + halt 404, "Dataset #{params[:id]} not found." unless @dataset + response['Content-Type'] = 'text/uri-list' + task_uri = OpenTox::Task.as_task("Converting and saving dataset ", @dataset.uri) do + @dataset.load params, request + @dataset.create_representations + @dataset.uri end + halt 202,task_uri.to_s+"\n" end -delete '/:id/?' do +# Delete a dataset +# @return [text/plain] Status message +delete '/:id' do begin dataset = Dataset.get(params[:id]) FileUtils.rm Dir["public/#{params[:id]}.*"] @@ -224,8 +304,11 @@ delete '/:id/?' do end end +# Delete all datasets +# @return [text/plain] Status message delete '/?' do - Dataset.all {|d| FileUtils.rm Dir["public/#{d.id}.*"] } + FileUtils.rm Dir["public/*.rdfxml"] + FileUtils.rm Dir["public/*.xls"] Dataset.auto_migrate! response['Content-Type'] = 'text/plain' "All datasets deleted." -- cgit v1.2.3 From 7a5dcbc4e0ca0cc7f18a3435c3a2ad6071d5afd6 Mon Sep 17 00:00:00 2001 From: Christoph Helma <helma@in-silico.ch> Date: Fri, 19 Nov 2010 14:42:29 +0100 Subject: lazar predictions and toxcreate are working --- application.rb | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 1d49ffb..50826d9 100644 --- a/application.rb +++ b/application.rb @@ -1,7 +1,7 @@ require 'rubygems' gem "opentox-ruby-api-wrapper", "= 1.6.6" require 'opentox-ruby-api-wrapper' -require 'parser' +#require 'parser' class Dataset @@ -77,15 +77,16 @@ class Dataset update(:yaml => dataset.to_yaml) end +=begin def create_representations dataset = YAML.load yaml ["rdfxml","xls"].each do |extension| file = "public/#{@id}.#{extension}" - LOGGER.debug file FileUtils.rm Dir["public/#{file}"] if File.exists? file File.open(file,"w+") { |f| f.puts eval("dataset.to_#{extension}") } end end +=end end @@ -127,20 +128,15 @@ get '/:id' do end end - #begin - dataset = OpenTox::Dataset.new - dataset.load_yaml(Dataset.get(params[:id]).yaml) - halt 404, "Dataset #{params[:id]} empty." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu - #rescue => e - #LOGGER.error e.message - #LOGGER.info e.backtrace - #halt 404, "Dataset #{params[:id]} not found." - #end + dataset = OpenTox::Dataset.new + dataset.load_yaml(Dataset.get(params[:id]).yaml) + halt 404, "Dataset #{params[:id]} empty." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml file = "public/#{params[:id]}.rdfxml" + File.open(file,"w+") { |f| f.puts dataset.to_rdfxml } unless File.exists? file # lazy rdfxml generation response['Content-Type'] = 'application/rdf+xml' File.open(file).read @@ -154,6 +150,7 @@ get '/:id' do when /ms-excel/ file = "public/#{params[:id]}.xls" + dataset.to_xls.write(file) unless File.exists? file # lazy xls generation response['Content-Type'] = 'application/ms-excel' File.open(file).read @@ -235,6 +232,11 @@ get '/:id/compounds' do YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n" end +post '/test' do + #request.env.to_yaml + @accept +end + # Create a new dataset. # # Posting without parameters creates and saves an empty dataset (with assigned URI). @@ -255,15 +257,15 @@ post '/?' do if params.empty? and request.env["rack.input"].read.empty? ot_dataset = OpenTox::Dataset.new(@dataset.uri) @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) - @dataset.create_representations + #@dataset.create_representations @dataset.uri else - task_uri = OpenTox::Task.as_task("Converting and saving dataset ", @dataset.uri) do + #task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do @dataset.load params, request - @dataset.create_representations + #@dataset.create_representations @dataset.uri - end - halt 202,task_uri.to_s+"\n" + #end + #halt 202,task.uri+"\n" end end @@ -282,12 +284,13 @@ post '/:id' do @dataset = Dataset.get(params[:id]) halt 404, "Dataset #{params[:id]} not found." unless @dataset response['Content-Type'] = 'text/uri-list' - task_uri = OpenTox::Task.as_task("Converting and saving dataset ", @dataset.uri) do + #task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do @dataset.load params, request - @dataset.create_representations + FileUtils.rm Dir["public/#{params[:id]}.*"] + #@dataset.create_representations @dataset.uri - end - halt 202,task_uri.to_s+"\n" + #end + #halt 202,task.uri.to_s+"\n" end # Delete a dataset -- cgit v1.2.3 From a9537281ff418049095a32a0be56e61e041b51d1 Mon Sep 17 00:00:00 2001 From: Christoph Helma <helma@in-silico.ch> Date: Wed, 24 Nov 2010 11:48:32 +0100 Subject: status 503 for rejected tasks --- application.rb | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 50826d9..b751ae9 100644 --- a/application.rb +++ b/application.rb @@ -1,7 +1,6 @@ require 'rubygems' gem "opentox-ruby-api-wrapper", "= 1.6.6" require 'opentox-ruby-api-wrapper' -#require 'parser' class Dataset @@ -232,11 +231,6 @@ get '/:id/compounds' do YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n" end -post '/test' do - #request.env.to_yaml - @accept -end - # Create a new dataset. # # Posting without parameters creates and saves an empty dataset (with assigned URI). @@ -249,23 +243,21 @@ end # @param [Header] Content-type one of `application/x-yaml, application/rdf+xml, multipart/form-data/` # @param [BODY] - string with data in selected Content-type # @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` -# @return [text/uri-list] Task ID or dataset ID (empty datasets without params) +# @return [text/uri-list] Task URI or Dataset URI (empty datasets) post '/?' do @dataset = Dataset.create response['Content-Type'] = 'text/uri-list' @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) if params.empty? and request.env["rack.input"].read.empty? - ot_dataset = OpenTox::Dataset.new(@dataset.uri) @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) - #@dataset.create_representations @dataset.uri else - #task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do + task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do @dataset.load params, request - #@dataset.create_representations @dataset.uri - #end - #halt 202,task.uri+"\n" + end + halt 503,task.uri+"\n" if task.status == "Cancelled" + halt 202,task.uri+"\n" end end @@ -284,13 +276,13 @@ post '/:id' do @dataset = Dataset.get(params[:id]) halt 404, "Dataset #{params[:id]} not found." unless @dataset response['Content-Type'] = 'text/uri-list' - #task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do + task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do @dataset.load params, request FileUtils.rm Dir["public/#{params[:id]}.*"] - #@dataset.create_representations @dataset.uri - #end - #halt 202,task.uri.to_s+"\n" + end + halt 503,task.uri+"\n" if task.status == "Cancelled" + halt 202,task.uri.to_s+"\n" end # Delete a dataset -- cgit v1.2.3 From f8a0a29276829ab046a61dbbda6277087f4ecb7c Mon Sep 17 00:00:00 2001 From: Christoph Helma <helma@in-silico.ch> Date: Wed, 24 Nov 2010 13:10:52 +0100 Subject: opentox-ruby-api-wrapper renamed to opentox-ruby --- application.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index b751ae9..6518ea8 100644 --- a/application.rb +++ b/application.rb @@ -1,6 +1,6 @@ require 'rubygems' -gem "opentox-ruby-api-wrapper", "= 1.6.6" -require 'opentox-ruby-api-wrapper' +gem "opentox-ruby", "~> 0" +require 'opentox-ruby' class Dataset -- cgit v1.2.3 From 6bdab49bab96cd5f498a36a0b76deb70cb945a21 Mon Sep 17 00:00:00 2001 From: mr <mr@mrautenberg.de> Date: Thu, 2 Dec 2010 11:56:52 +0100 Subject: merge with helma/development --- application.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 6518ea8..9f1841d 100644 --- a/application.rb +++ b/application.rb @@ -8,8 +8,11 @@ class Dataset property :id, Serial property :uri, String, :length => 255 property :yaml, Text, :length => 2**32-1 + property :token_id, String, :length => 255 property :created_at, DateTime + after :save, :check_policy + def load(params,request) data = request.env["rack.input"].read @@ -87,6 +90,11 @@ class Dataset end =end + private + def check_policy + OpenTox::Authorization.check_policy(uri, token_id) + end + end DataMapper.auto_upgrade! @@ -246,6 +254,8 @@ end # @return [text/uri-list] Task URI or Dataset URI (empty datasets) post '/?' do @dataset = Dataset.create + @dataset.token_id = params[:token_id] if params[:token_id] + @dataset.token_id = request.env['HTTP_TOKEN_ID'] if !dataset.token_id and request.env['HTTP_TOKEN_ID'] response['Content-Type'] = 'text/uri-list' @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) if params.empty? and request.env["rack.input"].read.empty? @@ -292,6 +302,14 @@ delete '/:id' do dataset = Dataset.get(params[:id]) FileUtils.rm Dir["public/#{params[:id]}.*"] dataset.destroy! + if params[:token_id] and !Dataset.get(params[:id]) and uri + begin + aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:token_id]) + LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{aa}" + rescue + LOGGER.warn "Policy delete error for Dataset URI: #{uri}" + end + end response['Content-Type'] = 'text/plain' "Dataset #{params[:id]} deleted." rescue -- cgit v1.2.3 From 1feb6b5f6d13f344e11e64beb41c3e82ae0ab39e Mon Sep 17 00:00:00 2001 From: mr <mr@mrautenberg.de> Date: Mon, 13 Dec 2010 13:47:11 +0100 Subject: a&a --- application.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 9f1841d..fe13f20 100644 --- a/application.rb +++ b/application.rb @@ -19,7 +19,7 @@ class Dataset content_type = request.content_type content_type = "application/rdf+xml" if content_type.nil? dataset = OpenTox::Dataset.new - + case content_type when /yaml/ @@ -28,7 +28,7 @@ class Dataset when "application/rdf+xml" dataset.load_rdfxml(data) - when /multipart\/form-data/ # file uploads + when /multipart\/form-data/ , "application/x-www-form-urlencoded" # file uploads case params[:file][:type] @@ -71,7 +71,7 @@ class Dataset end else - raise "MIME type \"#{@content_type}\" not supported." + raise "MIME type \"#{content_type}\" not supported." end dataset.uri = @uri # update uri (also in metdata) @@ -254,11 +254,12 @@ end # @return [text/uri-list] Task URI or Dataset URI (empty datasets) post '/?' do @dataset = Dataset.create - @dataset.token_id = params[:token_id] if params[:token_id] - @dataset.token_id = request.env['HTTP_TOKEN_ID'] if !dataset.token_id and request.env['HTTP_TOKEN_ID'] response['Content-Type'] = 'text/uri-list' @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) - if params.empty? and request.env["rack.input"].read.empty? + @dataset.update(:token_id => params[:token_id]) if params[:token_id] + @dataset.update(:token_id => request.env['HTTP_TOKEN_ID']) if !@dataset.token_id and request.env['HTTP_TOKEN_ID'] + + if params.size < 2 # and request.env["rack.input"].read.empty? # mr to fix @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) @dataset.uri else @@ -300,6 +301,7 @@ end delete '/:id' do begin dataset = Dataset.get(params[:id]) + uri = dataset.uri FileUtils.rm Dir["public/#{params[:id]}.*"] dataset.destroy! if params[:token_id] and !Dataset.get(params[:id]) and uri -- cgit v1.2.3 From 52991752e31108582cd4e9b3439447abb5f7020e Mon Sep 17 00:00:00 2001 From: mr <mr@mrautenberg.de> Date: Tue, 14 Dec 2010 12:34:38 +0100 Subject: remove token_id from datasets --- application.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index fe13f20..900c3dd 100644 --- a/application.rb +++ b/application.rb @@ -8,11 +8,13 @@ class Dataset property :id, Serial property :uri, String, :length => 255 property :yaml, Text, :length => 2**32-1 - property :token_id, String, :length => 255 property :created_at, DateTime - after :save, :check_policy + attr_accessor :token_id + @token_id = nil + after :save, :check_policy + def load(params,request) data = request.env["rack.input"].read @@ -255,9 +257,10 @@ end post '/?' do @dataset = Dataset.create response['Content-Type'] = 'text/uri-list' + @dataset.token_id = params[:token_id] if params[:token_id] + @dataset.token_id = request.env['HTTP_TOKEN_ID'] if !@dataset.token_id and request.env['HTTP_TOKEN_ID'] + @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) - @dataset.update(:token_id => params[:token_id]) if params[:token_id] - @dataset.update(:token_id => request.env['HTTP_TOKEN_ID']) if !@dataset.token_id and request.env['HTTP_TOKEN_ID'] if params.size < 2 # and request.env["rack.input"].read.empty? # mr to fix @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) -- cgit v1.2.3 From c70f28183923285065271f62e9c2f666d2d86376 Mon Sep 17 00:00:00 2001 From: mr <mr@mrautenberg.de> Date: Tue, 14 Dec 2010 15:01:07 +0100 Subject: rename token_id to subjectid --- application.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 900c3dd..f6cfa14 100644 --- a/application.rb +++ b/application.rb @@ -10,8 +10,8 @@ class Dataset property :yaml, Text, :length => 2**32-1 property :created_at, DateTime - attr_accessor :token_id - @token_id = nil + attr_accessor :subjectid + @subjectid = nil after :save, :check_policy @@ -94,7 +94,7 @@ class Dataset private def check_policy - OpenTox::Authorization.check_policy(uri, token_id) + OpenTox::Authorization.check_policy(uri, subjectid) end end @@ -257,12 +257,12 @@ end post '/?' do @dataset = Dataset.create response['Content-Type'] = 'text/uri-list' - @dataset.token_id = params[:token_id] if params[:token_id] - @dataset.token_id = request.env['HTTP_TOKEN_ID'] if !@dataset.token_id and request.env['HTTP_TOKEN_ID'] - + @dataset.subjectid = params[:subjectid] if params[:subjectid] + @dataset.subjectid = request.env['HTTP_SUBJECTID'] if !@dataset.subjectid and request.env['HTTP_SUBJECTID'] @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) - if params.size < 2 # and request.env["rack.input"].read.empty? # mr to fix + token_present = params.member?("subjectid") ? 1 : 0 + if params.size - token_present < 1 # and request.env["rack.input"].read.empty? # mr to fix @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) @dataset.uri else @@ -307,9 +307,9 @@ delete '/:id' do uri = dataset.uri FileUtils.rm Dir["public/#{params[:id]}.*"] dataset.destroy! - if params[:token_id] and !Dataset.get(params[:id]) and uri + if params[:subjectid] and !Dataset.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:token_id]) + aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:subjectid]) LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{aa}" rescue LOGGER.warn "Policy delete error for Dataset URI: #{uri}" -- cgit v1.2.3 From e752dc0646a48e01acddf0d710d25d82d44e8df5 Mon Sep 17 00:00:00 2001 From: mr <mr@mrautenberg.de> Date: Wed, 15 Dec 2010 11:14:54 +0100 Subject: get subjectid also out of request headers for delete --- application.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index f6cfa14..84babae 100644 --- a/application.rb +++ b/application.rb @@ -307,10 +307,12 @@ delete '/:id' do uri = dataset.uri FileUtils.rm Dir["public/#{params[:id]}.*"] dataset.destroy! - if params[:subjectid] and !Dataset.get(params[:id]) and uri + subjectid = params[:subjectid] if params[:subjectid] + subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] + if subjectid and !Dataset.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:subjectid]) - LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{aa}" + res = OpenTox::Authorization.delete_policies_from_uri(uri, subjectid) + LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{res}" rescue LOGGER.warn "Policy delete error for Dataset URI: #{uri}" end -- cgit v1.2.3 From e5cb5d03a733775caa3fa4cdf01f76c58dc46edd Mon Sep 17 00:00:00 2001 From: mr <mr@mrautenberg.de> Date: Mon, 10 Jan 2011 16:52:43 +0100 Subject: A&A --- application.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index f6cfa14..23a7197 100644 --- a/application.rb +++ b/application.rb @@ -112,6 +112,7 @@ end # @return [text/uri-list] List of available datasets get '/?' do response['Content-Type'] = 'text/uri-list' + params.delete_if{|k,v| k=="subjectid"} Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n" end @@ -137,7 +138,7 @@ get '/:id' do end end - dataset = OpenTox::Dataset.new + dataset = OpenTox::Dataset.new(nil, params[:subjectid]) dataset.load_yaml(Dataset.get(params[:id]).yaml) halt 404, "Dataset #{params[:id]} empty." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu @@ -307,10 +308,12 @@ delete '/:id' do uri = dataset.uri FileUtils.rm Dir["public/#{params[:id]}.*"] dataset.destroy! - if params[:subjectid] and !Dataset.get(params[:id]) and uri + subjectid = params[:subjectid] ? params[:subjectid] : nil + subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] + if subjectid and !Dataset.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:subjectid]) - LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{aa}" + res = OpenTox::Authorization.delete_policies_from_uri(uri, subjectid) + LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{res}" rescue LOGGER.warn "Policy delete error for Dataset URI: #{uri}" end -- cgit v1.2.3 From 95a0c849f593936ef7d92226330d7193cd83b1f8 Mon Sep 17 00:00:00 2001 From: mguetlein <martin.guetlein@gmail.com> Date: Thu, 13 Jan 2011 13:04:56 +0100 Subject: add html support for get dataset --- application.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 84babae..802cb88 100644 --- a/application.rb +++ b/application.rb @@ -16,7 +16,7 @@ class Dataset after :save, :check_policy def load(params,request) - + data = request.env["rack.input"].read content_type = request.content_type content_type = "application/rdf+xml" if content_type.nil? @@ -124,6 +124,8 @@ get '/:id' do unless extension.empty? params[:id].sub!(/\.#{extension}$/,'') case extension + when "html" + @accept = 'text/html' when "yaml" @accept = 'application/x-yaml' when "csv" @@ -152,7 +154,11 @@ get '/:id' do when /yaml/ response['Content-Type'] = 'application/x-yaml' dataset.to_yaml - + + when /html/ + response['Content-Type'] = 'text/html' + OpenTox.text_to_html dataset.to_yaml + when "text/csv" response['Content-Type'] = 'text/csv' dataset.to_csv @@ -255,6 +261,7 @@ end # @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` # @return [text/uri-list] Task URI or Dataset URI (empty datasets) post '/?' do + @dataset = Dataset.create response['Content-Type'] = 'text/uri-list' @dataset.subjectid = params[:subjectid] if params[:subjectid] -- cgit v1.2.3 From fe6be7ddb591cee17f3e10a9ccb962cfb34e3fb9 Mon Sep 17 00:00:00 2001 From: mr <mr@mrautenberg.de> Date: Tue, 18 Jan 2011 11:06:26 +0100 Subject: get subjectid from api-wrapper helper --- application.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 23a7197..3c21d3b 100644 --- a/application.rb +++ b/application.rb @@ -20,7 +20,7 @@ class Dataset data = request.env["rack.input"].read content_type = request.content_type content_type = "application/rdf+xml" if content_type.nil? - dataset = OpenTox::Dataset.new + dataset = OpenTox::Dataset.new(nil, params[:subjectid]) case content_type @@ -138,7 +138,7 @@ get '/:id' do end end - dataset = OpenTox::Dataset.new(nil, params[:subjectid]) + dataset = OpenTox::Dataset.new(nil, @subjectid) dataset.load_yaml(Dataset.get(params[:id]).yaml) halt 404, "Dataset #{params[:id]} empty." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu @@ -258,8 +258,7 @@ end post '/?' do @dataset = Dataset.create response['Content-Type'] = 'text/uri-list' - @dataset.subjectid = params[:subjectid] if params[:subjectid] - @dataset.subjectid = request.env['HTTP_SUBJECTID'] if !@dataset.subjectid and request.env['HTTP_SUBJECTID'] + @dataset.subjectid = @subjectid @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) token_present = params.member?("subjectid") ? 1 : 0 @@ -308,11 +307,9 @@ delete '/:id' do uri = dataset.uri FileUtils.rm Dir["public/#{params[:id]}.*"] dataset.destroy! - subjectid = params[:subjectid] ? params[:subjectid] : nil - subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] - if subjectid and !Dataset.get(params[:id]) and uri + if @subjectid and !Dataset.get(params[:id]) and uri begin - res = OpenTox::Authorization.delete_policies_from_uri(uri, subjectid) + res = OpenTox::Authorization.delete_policies_from_uri(uri, @subjectid) LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{res}" rescue LOGGER.warn "Policy delete error for Dataset URI: #{uri}" -- cgit v1.2.3 From e70b3178489abbdb13cb11518a7cbbe1c19adf00 Mon Sep 17 00:00:00 2001 From: mguetlein <martin.guetlein@gmail.com> Date: Wed, 19 Jan 2011 14:13:14 +0100 Subject: do not try to save dataset unless no id given --- application.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 802cb88..57e064f 100644 --- a/application.rb +++ b/application.rb @@ -11,7 +11,6 @@ class Dataset property :created_at, DateTime attr_accessor :subjectid - @subjectid = nil after :save, :check_policy @@ -94,7 +93,7 @@ class Dataset private def check_policy - OpenTox::Authorization.check_policy(uri, subjectid) + OpenTox::Authorization.check_policy(uri, subjectid) if id end end @@ -201,7 +200,7 @@ get %r{/(\d+)/feature/(.*)$} do |id,feature| #feature_uri = url_for("/#{params[:id]}/feature/#{URI.encode(params[:feature_name])}",:full) # work around racks internal uri decoding #dataset = YAML.load(Dataset.get(params[:id]).yaml) - feature_uri = url_for("/#{id}/feature/#{URI.encode(feature)}",:full) # work around racks internal uri decoding + feature_uri = url_for("/#{id}/feature/#{URI.encode(feature)}",:full) # work around racks internal uri decoding dataset = YAML.load(Dataset.get(id).yaml) metadata = dataset.features[feature_uri] @@ -309,6 +308,7 @@ end # Delete a dataset # @return [text/plain] Status message delete '/:id' do + LOGGER.debug "deleting dataset with id "+params[:id].to_s begin dataset = Dataset.get(params[:id]) uri = dataset.uri -- cgit v1.2.3 From d5e00cf1cdd7a5029e7ecedda2508a14cf4657e0 Mon Sep 17 00:00:00 2001 From: mguetlein <martin.guetlein@gmail.com> Date: Fri, 11 Feb 2011 10:47:35 +0100 Subject: fix correct setting of subjecit and read rack.input only once --- application.rb | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 61889c1..b5d8cf7 100644 --- a/application.rb +++ b/application.rb @@ -14,20 +14,22 @@ class Dataset after :save, :check_policy - def load(params,request) + # subjectid ist stored as memeber variable, not in params + def load(params,content_type,input_data) + + raise "store subject-id in dataset-object, not in params" if + params.has_key?(:subjectid) and @subjectid==nil - data = request.env["rack.input"].read - content_type = request.content_type content_type = "application/rdf+xml" if content_type.nil? - dataset = OpenTox::Dataset.new(nil, params[:subjectid]) + dataset = OpenTox::Dataset.new(nil, @subjectid) case content_type when /yaml/ - dataset.load_yaml(data) + dataset.load_yaml(input_data) - when "application/rdf+xml" - dataset.load_rdfxml(data) + when /application\/rdf\+xml/ + dataset.load_rdfxml(input_data) when /multipart\/form-data/ , "application/x-www-form-urlencoded" # file uploads @@ -266,14 +268,17 @@ post '/?' do response['Content-Type'] = 'text/uri-list' @dataset.subjectid = @subjectid @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) - - token_present = params.member?("subjectid") ? 1 : 0 - if params.size - token_present < 1 # and request.env["rack.input"].read.empty? # mr to fix + + # it could be that the read function works only once!, store in varible + input_data = request.env["rack.input"].read + # make sure subjectid is not included in params, subjectid is set as member variable + params.delete(:subjectid) + if params.size == 0 and input_data.size==0 @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) @dataset.uri else task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do - @dataset.load params, request + @dataset.load params, request.content_type, input_data @dataset.uri end halt 503,task.uri+"\n" if task.status == "Cancelled" @@ -294,10 +299,11 @@ end # @return [text/uri-list] Task ID post '/:id' do @dataset = Dataset.get(params[:id]) + @dataset.subjectid = @subjectid halt 404, "Dataset #{params[:id]} not found." unless @dataset response['Content-Type'] = 'text/uri-list' task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do - @dataset.load params, request + @dataset.load params, request.content_type, request.env["rack.input"].read FileUtils.rm Dir["public/#{params[:id]}.*"] @dataset.uri end -- cgit v1.2.3 From 163b4d7462de84aad63a5dd46a951438c79a7654 Mon Sep 17 00:00:00 2001 From: root <root@ot-dev.in-silico.ch> Date: Wed, 23 Feb 2011 12:27:53 +0000 Subject: datamapper removed, file based storage --- application.rb | 314 +++++++++++++++++++++++++++------------------------------ 1 file changed, 149 insertions(+), 165 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index b5d8cf7..1b84309 100644 --- a/application.rb +++ b/application.rb @@ -2,109 +2,120 @@ require 'rubygems' gem "opentox-ruby", "~> 0" require 'opentox-ruby' -class Dataset - - include DataMapper::Resource - property :id, Serial - property :uri, String, :length => 255 - property :yaml, Text, :length => 2**32-1 - property :created_at, DateTime - - attr_accessor :subjectid - - after :save, :check_policy - - # subjectid ist stored as memeber variable, not in params - def load(params,content_type,input_data) - - raise "store subject-id in dataset-object, not in params" if - params.has_key?(:subjectid) and @subjectid==nil - - content_type = "application/rdf+xml" if content_type.nil? - dataset = OpenTox::Dataset.new(nil, @subjectid) - - case content_type - - when /yaml/ - dataset.load_yaml(input_data) - - when /application\/rdf\+xml/ - dataset.load_rdfxml(input_data) - - when /multipart\/form-data/ , "application/x-www-form-urlencoded" # file uploads - - case params[:file][:type] - - when /yaml/ - dataset.load_yaml(params[:file][:tempfile].read) - - when "application/rdf+xml" - dataset.load_rdfxml_file(params[:file][:tempfile]) - - when "text/csv" - dataset = OpenTox::Dataset.new @uri - dataset.load_csv(params[:file][:tempfile].read) - dataset.add_metadata({ - DC.title => File.basename(params[:file][:filename],".csv"), - OT.hasSource => File.basename(params[:file][:filename]) - }) - - when /ms-excel/ - extension = File.extname(params[:file][:filename]) - case extension - when ".xls" - xls = params[:file][:tempfile].path + ".xls" - File.rename params[:file][:tempfile].path, xls # roo needs these endings - book = Excel.new xls - when ".xlsx" - xlsx = params[:file][:tempfile].path + ".xlsx" - File.rename params[:file][:tempfile].path, xlsx # roo needs these endings - book = Excel.new xlsx - else - raise "#{params[:file][:filename]} is not a valid Excel input file." +helpers do + def next_id + Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last + 1 end - dataset.load_spreadsheet(book) - dataset.add_metadata({ - DC.title => File.basename(params[:file][:filename],extension), - OT.hasSource => File.basename(params[:file][:filename]) - }) - - else - raise "MIME type \"#{params[:file][:type]}\" not supported." - end - - else - raise "MIME type \"#{content_type}\" not supported." - end - - dataset.uri = @uri # update uri (also in metdata) - dataset.features.keys.each { |f| dataset.features[f][OT.hasSource] = dataset.metadata[OT.hasSource] unless dataset.features[f][OT.hasSource]} - update(:yaml => dataset.to_yaml) - end - -=begin - def create_representations - dataset = YAML.load yaml - ["rdfxml","xls"].each do |extension| - file = "public/#{@id}.#{extension}" - FileUtils.rm Dir["public/#{file}"] if File.exists? file - File.open(file,"w+") { |f| f.puts eval("dataset.to_#{extension}") } - end - end -=end - private - def check_policy - OpenTox::Authorization.check_policy(uri, subjectid) if id - end + def uri(id) + url_for "/#{id}", :full + end + # subjectid ist stored as memeber variable, not in params + def load_dataset(id, params,content_type,input_data) + + @uri = uri id + raise "store subject-id in dataset-object, not in params" if params.has_key?(:subjectid) and @subjectid==nil + + content_type = "application/rdf+xml" if content_type.nil? + dataset = OpenTox::Dataset.new(nil, @subjectid) + + case content_type + + when /yaml/ + dataset.load_yaml(input_data) + + when /application\/rdf\+xml/ + dataset.load_rdfxml(input_data) + + when /multipart\/form-data/ , "application/x-www-form-urlencoded" # file uploads + + case params[:file][:type] + + when /yaml/ + dataset.load_yaml(params[:file][:tempfile].read) + + when "application/rdf+xml" + dataset.load_rdfxml_file(params[:file][:tempfile]) + + when "text/csv" + dataset = OpenTox::Dataset.new @uri + dataset.load_csv(params[:file][:tempfile].read) + dataset.add_metadata({ + DC.title => File.basename(params[:file][:filename],".csv"), + OT.hasSource => File.basename(params[:file][:filename]) + }) + + when /ms-excel/ + extension = File.extname(params[:file][:filename]) + case extension + when ".xls" + xls = params[:file][:tempfile].path + ".xls" + File.rename params[:file][:tempfile].path, xls # roo needs these endings + book = Excel.new xls + when ".xlsx" + xlsx = params[:file][:tempfile].path + ".xlsx" + File.rename params[:file][:tempfile].path, xlsx # roo needs these endings + book = Excel.new xlsx + else + raise "#{params[:file][:filename]} is not a valid Excel input file." + end + dataset.load_spreadsheet(book) + dataset.add_metadata({ + DC.title => File.basename(params[:file][:filename],extension), + OT.hasSource => File.basename(params[:file][:filename]) + }) + + else + raise "MIME type \"#{params[:file][:type]}\" not supported." + end + + else + raise "MIME type \"#{content_type}\" not supported." + end + + dataset.uri = @uri # update uri (also in metdata) + dataset.features.keys.each { |f| dataset.features[f][OT.hasSource] = dataset.metadata[OT.hasSource] unless dataset.features[f][OT.hasSource]} + File.open("public/#{@id}.yaml","w+"){|f| f.puts dataset.to_yaml} + end end -DataMapper.auto_upgrade! - before do @accept = request.env['HTTP_ACCEPT'] @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil? + LOGGER.debug "PARAMS" + @id = request.path_info.match(/^\/\d+/) + LOGGER.debug @id + unless @id.nil? + @id = @id.to_s.sub(/\//,'').to_i + + @uri = uri @id + @yaml_file = "public/#{@id}.yaml" + halt 404, "Dataset #{@id} not found." unless File.exists? @yaml_file + + extension = File.extname(request.path_info) + #extension = File.extname(params[:id]).sub(/\./,'') + unless extension.empty? + #request.path_info.sub!(/\.#{extension}$/,'') + case extension + when "html" + @accept = 'text/html' + when "yaml" + @accept = 'application/x-yaml' + when "csv" + @accept = 'text/csv' + when "rdfxml" + @accept = 'application/rdf+xml' + when "xls" + @accept = 'application/ms-excel' + else + halt 404, "File format #{extension} not supported." + end + end + end + + # make sure subjectid is not included in params, subjectid is set as member variable + params.delete(:subjectid) end ## REST API @@ -113,61 +124,40 @@ end # @return [text/uri-list] List of available datasets get '/?' do response['Content-Type'] = 'text/uri-list' - params.delete_if{|k,v| k=="subjectid"} - Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n" + Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" end # Get a dataset representation # @param [Header] Accept one of `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` (default application/rdf+xml) # @return [application/rdf+xml, application-x-yaml, text/csv, application/ms-excel] Dataset representation get '/:id' do - - extension = File.extname(params[:id]).sub(/\./,'') - unless extension.empty? - params[:id].sub!(/\.#{extension}$/,'') - case extension - when "html" - @accept = 'text/html' - when "yaml" - @accept = 'application/x-yaml' - when "csv" - @accept = 'text/csv' - when "rdfxml" - @accept = 'application/rdf+xml' - when "xls" - @accept = 'application/ms-excel' - else - halt 404, "File format #{extension} not supported." - end - end - - dataset = OpenTox::Dataset.new(nil, @subjectid) - dataset.load_yaml(Dataset.get(params[:id]).yaml) - halt 404, "Dataset #{params[:id]} empty." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml file = "public/#{params[:id]}.rdfxml" - File.open(file,"w+") { |f| f.puts dataset.to_rdfxml } unless File.exists? file # lazy rdfxml generation + unless File.exists? file # lazy rdfxml generation + dataset = YAML.load_file(@yaml_file) + File.open(file,"w+") { |f| f.puts dataset.to_rdfxml } + end response['Content-Type'] = 'application/rdf+xml' - File.open(file).read + File.read(file) when /yaml/ response['Content-Type'] = 'application/x-yaml' - dataset.to_yaml + File.read(@yaml_file) when /html/ response['Content-Type'] = 'text/html' - OpenTox.text_to_html dataset.to_yaml + OpenTox.text_to_html File.read(@yaml_file) when "text/csv" response['Content-Type'] = 'text/csv' - dataset.to_csv + YAML.load_file(@yaml_file).to_csv when /ms-excel/ file = "public/#{params[:id]}.xls" - dataset.to_xls.write(file) unless File.exists? file # lazy xls generation + YAML.load_file(@yaml_file).to_xls.write(file) unless File.exists? file # lazy xls generation response['Content-Type'] = 'application/ms-excel' File.open(file).read @@ -180,7 +170,7 @@ end # @return [application/rdf+xml] Metadata OWL-DL get '/:id/metadata' do - metadata = YAML.load(Dataset.get(params[:id]).yaml).metadata + metadata = YAML.load_file(@yaml_file).metadata case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml @@ -199,13 +189,12 @@ end # @param [Header] Accept one of `application/rdf+xml or application-x-yaml` (default application/rdf+xml) # @return [application/rdf+xml,application/x-yaml] Feature metadata get %r{/(\d+)/feature/(.*)$} do |id,feature| -#get '/:id/feature/:feature_name/?' do - #feature_uri = url_for("/#{params[:id]}/feature/#{URI.encode(params[:feature_name])}",:full) # work around racks internal uri decoding - #dataset = YAML.load(Dataset.get(params[:id]).yaml) - feature_uri = url_for("/#{id}/feature/#{URI.encode(feature)}",:full) # work around racks internal uri decoding - dataset = YAML.load(Dataset.get(id).yaml) - metadata = dataset.features[feature_uri] + @id = id + @uri = uri @id + @yaml_file = "public/#{@id}.yaml" + feature_uri = url_for("/#{@id}/feature/#{URI.encode(feature)}",:full) # work around racks internal uri decoding + metadata = YAML.load_file(@yaml_file).features[feature_uri] case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml @@ -225,7 +214,7 @@ end # @return [application/rdf+xml, application-x-yaml, text/uri-list] Feature list get '/:id/features' do - features = YAML.load(Dataset.get(params[:id]).yaml).features + features = YAML.load_file(@yaml_file).features case @accept when /rdf/ # redland sends text/rdf instead of application/rdf+xml @@ -238,7 +227,7 @@ get '/:id/features' do features.to_yaml when "text/uri-list" response['Content-Type'] = 'text/uri-list' - YAML.load(Dataset.get(params[:id]).yaml).features.keys.join("\n") + "\n" + features.keys.join("\n") + "\n" end end @@ -246,7 +235,7 @@ end # @return [text/uri-list] Feature list get '/:id/compounds' do response['Content-Type'] = 'text/uri-list' - YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n" + YAML.load_file(@yaml_file).compounds.join("\n") + "\n" end # Create a new dataset. @@ -264,22 +253,22 @@ end # @return [text/uri-list] Task URI or Dataset URI (empty datasets) post '/?' do - @dataset = Dataset.create response['Content-Type'] = 'text/uri-list' - @dataset.subjectid = @subjectid - @dataset.update(:uri => url_for("/#{@dataset.id}", :full)) # it could be that the read function works only once!, store in varible input_data = request.env["rack.input"].read - # make sure subjectid is not included in params, subjectid is set as member variable - params.delete(:subjectid) + @id = next_id + @uri = uri @id + @yaml_file = "public/#{@id}.yaml" if params.size == 0 and input_data.size==0 - @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml) - @dataset.uri + File.open(@yaml_file,"w+"){|f| f.puts OpenTox::Dataset.new(@uri).to_yaml} + OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @yaml_file + @uri else - task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do - @dataset.load params, request.content_type, input_data - @dataset.uri + task = OpenTox::Task.create("Converting and saving dataset ", @uri) do + load_dataset @id, params, request.content_type, input_data + OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @yaml_file + @uri end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri+"\n" @@ -298,14 +287,12 @@ end # @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` # @return [text/uri-list] Task ID post '/:id' do - @dataset = Dataset.get(params[:id]) - @dataset.subjectid = @subjectid - halt 404, "Dataset #{params[:id]} not found." unless @dataset + LOGGER.debug @uri response['Content-Type'] = 'text/uri-list' - task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do - @dataset.load params, request.content_type, request.env["rack.input"].read - FileUtils.rm Dir["public/#{params[:id]}.*"] - @dataset.uri + task = OpenTox::Task.create("Converting and saving dataset ", @uri) do + FileUtils.rm Dir["public/#{@id}.*"] + load_dataset @id, params, request.content_type, request.env["rack.input"].read + @uri end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri.to_s+"\n" @@ -314,24 +301,21 @@ end # Delete a dataset # @return [text/plain] Status message delete '/:id' do - LOGGER.debug "deleting dataset with id "+params[:id].to_s + LOGGER.debug "deleting dataset with id "+@id.to_s begin - dataset = Dataset.get(params[:id]) - uri = dataset.uri - FileUtils.rm Dir["public/#{params[:id]}.*"] - dataset.destroy! - if @subjectid and !Dataset.get(params[:id]) and uri + FileUtils.rm Dir["public/#{@id}.*"] + if @subjectid and !File.exists? @yaml_file and @uri begin - res = OpenTox::Authorization.delete_policies_from_uri(uri, @subjectid) - LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{res}" + res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid) + LOGGER.debug "Policy deleted for Dataset URI: #{@uri} with result: #{res}" rescue - LOGGER.warn "Policy delete error for Dataset URI: #{uri}" + LOGGER.warn "Policy delete error for Dataset URI: #{@uri}" end end response['Content-Type'] = 'text/plain' - "Dataset #{params[:id]} deleted." + "Dataset #{@id} deleted." rescue - halt 404, "Dataset #{params[:id]} does not exist." + halt 404, "Dataset #{@id} does not exist." end end @@ -340,7 +324,7 @@ end delete '/?' do FileUtils.rm Dir["public/*.rdfxml"] FileUtils.rm Dir["public/*.xls"] - Dataset.auto_migrate! + FileUtils.rm Dir["public/*.yaml"] response['Content-Type'] = 'text/plain' "All datasets deleted." end -- cgit v1.2.3 From c54019052854db2c0d2b7ae24e85959ad1c3e708 Mon Sep 17 00:00:00 2001 From: root <root@ot-dev.in-silico.ch> Date: Thu, 24 Feb 2011 11:01:02 +0000 Subject: next_id fixed --- application.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 1b84309..de11313 100644 --- a/application.rb +++ b/application.rb @@ -2,9 +2,13 @@ require 'rubygems' gem "opentox-ruby", "~> 0" require 'opentox-ruby' +set :lock, true + helpers do def next_id - Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last + 1 + id = Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last + id = 0 if id.nil? + id + 1 end def uri(id) @@ -83,9 +87,7 @@ end before do @accept = request.env['HTTP_ACCEPT'] @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil? - LOGGER.debug "PARAMS" @id = request.path_info.match(/^\/\d+/) - LOGGER.debug @id unless @id.nil? @id = @id.to_s.sub(/\//,'').to_i -- cgit v1.2.3 From 966e56b7d3dc5472f288157420f1b4ac109f7ab9 Mon Sep 17 00:00:00 2001 From: Christoph Helma <helma@in-silico.ch> Date: Wed, 9 Mar 2011 12:00:09 +0100 Subject: version bumped to 1.0.0 --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index de11313..feac9f2 100644 --- a/application.rb +++ b/application.rb @@ -1,5 +1,5 @@ require 'rubygems' -gem "opentox-ruby", "~> 0" +gem "opentox-ruby", "~> 1" require 'opentox-ruby' set :lock, true -- cgit v1.2.3