diff options
author | Martin Gütlein <martin.guetlein@gmail.com> | 2010-03-23 17:12:14 +0100 |
---|---|---|
committer | Martin Gütlein <martin.guetlein@gmail.com> | 2010-03-23 17:12:14 +0100 |
commit | 6e97b25a827d54c1b39e53f4c39196403e490d03 (patch) | |
tree | 037343113ad5f3fe6ab4c24d699137a8b3c59d74 | |
parent | 428a78d095ee383cc839ce4a15a48ecfbe8562cb (diff) | |
parent | 2d58fd80281ddc6a9c061867f3e6930a71df0ffd (diff) |
resolved confilct
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | application.rb | 97 |
2 files changed, 58 insertions, 40 deletions
@@ -3,3 +3,4 @@ api_key.rb tmp/* log/* datasets/* +public/* diff --git a/application.rb b/application.rb index 4222f3d..c83ba7f 100644 --- a/application.rb +++ b/application.rb @@ -1,23 +1,38 @@ require 'rubygems' -gem 'opentox-ruby-api-wrapper', '= 1.2.7' +gem 'opentox-ruby-api-wrapper', '= 1.4.0' require 'opentox-ruby-api-wrapper' +#require 'sinatra/respond_to' +#Sinatra::Application.register Sinatra::RespondTo + + +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 :owl, Text, :length => 1000000 + property :yaml, Text, :length => 2**32-1 + property :owl, Text, :length => 2**32-1 property :created_at, DateTime - def owl - File.read self.file + def to_owl + data = YAML.load(yaml) + owl = OpenTox::Owl.create 'Dataset', uri + ['title', 'source'].each do |method| + eval "owl.#{method} = data.#{method}" + end + if data.data + data.data.each do |compound,features| + owl.add_data_entries compound,features + end + end + owl.rdf end - def owl=(owl) - self.file = File.join(File.dirname(File.expand_path(__FILE__)),'public',"#{id}.owl") - File.open(self.file,"w+") { |f| f.write owl } - end + #def from_owl + #end + end DataMapper.auto_upgrade! @@ -26,26 +41,38 @@ DataMapper.auto_upgrade! get '/?' do response['Content-Type'] = 'text/uri-list' - Dataset.all.collect{|d| d.uri}.join("\n") + Dataset.all.collect{|d| d.uri}.join("\n") + "\n" end -get '/:id/?' do +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' + end begin dataset = Dataset.get(params[:id]) rescue => e - LOGGER.error e.message - LOGGER.warn e.backtrace + raise e.message + e.backtrace halt 404, "Dataset #{params[:id]} not found." end - 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' + unless dataset.owl # lazy owl creation + dataset.owl = dataset.to_owl + dataset.save + end dataset.owl when /yaml/ response['Content-Type'] = 'application/x-yaml' - OpenTox::Dataset.find(dataset.uri).to_yaml + dataset.yaml else halt 400, "Unsupported MIME type '#{accept}'" end @@ -56,57 +83,47 @@ get '/:id/features/:feature_id/?' do end get '/:id/features/?' do - OpenTox::Dataset.find(url_for("/#{params[:id]}", :full)).features + YAML.load(Dataset.get(params[:id]).yaml).features.join("\n") + "\n" end -post '/?' do - task = OpenTox::Task.create - pid = Spork.spork(:logger => LOGGER) do +get '/:id/compounds/?' do + YAML.load(Dataset.get(params[:id]).yaml).compounds.join("\n") + "\n" +end - task.started - LOGGER.debug "Dataset task #{task.uri} started" +post '/?' do dataset = Dataset.new dataset.save - uri = url_for("/#{dataset.id}", :full) + 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" - rdf = request.env["rack.input"].read - d= OpenTox::Dataset.new - d.rdf = rdf - d.uri = uri + dataset.yaml = owl2yaml request.env["rack.input"].read else halt 404, "MIME type \"#{request.content_type}\" not supported." end - LOGGER.debug "Saving dataset #{uri}." begin - dataset.owl = d.rdf - dataset.uri = uri + #dataset.owl = d.rdf + #dataset.uri = uri dataset.save - task.completed(uri) rescue => e LOGGER.error e.message LOGGER.info e.backtrace - halt 500, "Could not save dataset #{uri}." + halt 500, "Could not save dataset #{dataset.uri}." end LOGGER.debug "#{dataset.uri} saved." - end - task.pid = pid - #status 303 # rest client tries to redirect response['Content-Type'] = 'text/uri-list' - task.uri - - - - + dataset.uri + "\n" end delete '/:id/?' do begin dataset = Dataset.get(params[:id]) - File.delete dataset.file dataset.destroy! response['Content-Type'] = 'text/plain' "Dataset #{params[:id]} deleted." |