diff options
author | Christoph Helma <helma@in-silico.ch> | 2012-07-12 16:38:30 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2012-07-12 16:38:30 +0200 |
commit | 57b2a61d551010875fd428f0c2666a12abbb3c32 (patch) | |
tree | 80e116652df9eee181d5ede5f355fbcfb0dd17d0 | |
parent | 6d46c0d15550498bfc84413ea3f0831f26e0e631 (diff) |
all opentox-client tests pass
-rw-r--r-- | lib/4store.rb | 48 | ||||
-rw-r--r-- | lib/opentox-server.rb | 1 | ||||
-rw-r--r-- | lib/opentox.rb | 24 | ||||
-rw-r--r-- | opentox-server.gemspec | 1 |
4 files changed, 28 insertions, 46 deletions
diff --git a/lib/4store.rb b/lib/4store.rb index c733f51..24eec80 100644 --- a/lib/4store.rb +++ b/lib/4store.rb @@ -2,43 +2,23 @@ module OpenTox module Backend class FourStore - # TODO: simplify - @@mime_format = { - "application/rdf+xml" => :rdfxml, - "text/turtle" => :turtle, - "text/plain" => :ntriples, - "text/uri-list" => :uri_list, - "text/html" => :html, - 'application/sparql-results+xml' => :sparql - } - - @@format_mime = { - :rdfxml => "application/rdf+xml", - :turtle => "text/turtle", - :ntriples => "text/plain", - :uri_list => "text/uri-list", - :html => "text/html", - :sparql => 'application/sparql-results+xml' - } - - @@accept_formats = [:rdfxml, :turtle, :ntriples, :uri_list, :html, :sparql] - @@content_type_formats = [:rdfxml, :turtle, :ntriples] - - def self.list mime_type + @@accept_formats = [ "application/rdf+xml", "text/turtle", "text/plain", "text/uri-list", "text/html", 'application/sparql-results+xml' ] + @@content_type_formats = [ "application/rdf+xml", "text/turtle", "text/plain" ] + + def self.list service_uri, mime_type mime_type = "text/html" if mime_type.match(%r{\*/\*}) + bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.join(", ")} in the Accept Header." unless @@accept_formats.include? mime_type if mime_type =~ /uri-list/ - sparql = "SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o} }" - elsif mime_type =~ /turtle|html|rdf|plain/ - sparql = "CONSTRUCT {?s ?p ?o.} WHERE {?s <#{RDF.type}> <#{klass}>; ?p ?o. }" - else - bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.collect{|f| @@format_mime[f]}.join(", ")} in the Accept Header." #unless @@accept_formats.include? @@mime_format[mime_type] + sparql = "SELECT DISTINCT ?g WHERE {GRAPH ?g {?g <#{RDF.type}> <#{klass}>; ?p ?o. } }" + else + sparql = "CONSTRUCT {?s ?p ?o.} FROM WHERE {?s <#{RDF.type}> <#{klass}>; ?p ?o. }" end query sparql, mime_type end def self.get uri, mime_type mime_type = "text/html" if mime_type.match(%r{\*/\*}) - bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.collect{|f| @@format_mime[f]}.join(", ")} in the Accept Header." unless @@accept_formats.include? @@mime_format[mime_type] + bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.join(", ")} in the Accept Header." unless @@accept_formats.include? mime_type sparql = "CONSTRUCT {?s ?p ?o.} FROM <#{uri}> WHERE { ?s ?p ?o. }" rdf = query sparql, mime_type not_found_error "#{uri} not found." if rdf.empty? @@ -46,17 +26,17 @@ module OpenTox end def self.post uri, rdf, mime_type - bad_request_error "'#{mime_type}' is not a supported content type. Please use one of #{@@content_type_formats.collect{|f| @@format_mime[f]}.join(", ")}." unless @@content_type_formats.include? @@mime_format[mime_type] or mime_type == "multipart/form-data" + bad_request_error "'#{mime_type}' is not a supported content type. Please use one of #{@@content_type_formats.join(", ")}." unless @@content_type_formats.include? mime_type or mime_type == "multipart/form-data" bad_request_error "Reqest body empty." unless rdf mime_type = "application/x-turtle" if mime_type == "text/plain" # ntriples is turtle in 4store RestClient.post File.join(four_store_uri,"data")+"/", :data => rdf, :graph => uri, "mime-type" => mime_type end - def self.put uri, rdf, mime_type, skip_rewrite=false - bad_request_error "'#{mime_type}' is not a supported content type. Please use one of #{@@content_type_formats.collect{|f| @@format_mime[f]}.join(", ")}." unless @@content_type_formats.include? @@mime_format[mime_type] + def self.put uri, rdf, mime_type + bad_request_error "'#{mime_type}' is not a supported content type. Please use one of #{@@content_type_formats.join(", ")}." unless @@content_type_formats.include? mime_type bad_request_error "Reqest body empty." unless rdf mime_type = "application/x-turtle" if mime_type == "text/plain" - RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => mime_type # content-type not very consistent in 4store + RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => mime_type end def self.delete uri @@ -90,7 +70,7 @@ module OpenTox reader.each_statement { |statement| rdf << statement } end prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"} - ['OT', 'DC', 'XSD'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") } + ['OT', 'DC', 'XSD', 'OLO'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") } turtle = RDF::N3::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| rdf.each{|statement| writer << statement} end diff --git a/lib/opentox-server.rb b/lib/opentox-server.rb index 15cb0cd..3f04acc 100644 --- a/lib/opentox-server.rb +++ b/lib/opentox-server.rb @@ -3,7 +3,6 @@ require 'rack' require 'rack/contrib' require 'sinatra' require 'roo' -require 'rdf/n3' #require File.join(File.dirname(__FILE__),"environment.rb") require File.join(File.dirname(__FILE__),"4store.rb") require File.join(File.dirname(__FILE__),"opentox.rb") diff --git a/lib/opentox.rb b/lib/opentox.rb index 6bca6bb..3d1092a 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -28,12 +28,18 @@ module OpenTox @accept = request.env['HTTP_ACCEPT'] end + before "/#{SERVICE}/:id" do + @uri = uri("/#{SERVICE}/#{params[:id]}") + end + helpers do def parse_input case request.content_type when /multipart/ @body = params[:file][:tempfile].read + # sdf files are incorrectly detected @content_type = params[:file][:type] + @content_type = "chemical/x-mdl-sdfile" if File.extname(params[:file][:filename]) == ".sdf" else @body = request.body.read @content_type = request.content_type @@ -60,37 +66,35 @@ module OpenTox # Get a list of objects at the server get "/#{SERVICE}/?" do - FourStore.list @accept + FourStore.list uri("/#{SERVICE}"), @accept end # Create a new resource post "/#{SERVICE}/?" do - uri = uri("/#{SERVICE}/#{SecureRandom.uuid}") - FourStore.put(uri, @body, @content_type) + @uri = uri("/#{SERVICE}/#{SecureRandom.uuid}") + FourStore.put(@uri, @body, @content_type) response['Content-Type'] = "text/uri-list" - uri + @uri end # Get resource representation get "/#{SERVICE}/:id/?" do - FourStore.get(uri("/#{SERVICE}/#{params[:id]}"), @accept) + FourStore.get(@uri, @accept) end # Modify (i.e. add rdf statments to) a resource post "/#{SERVICE}/:id/?" do - FourStore.post uri("/#{SERVICE}/#{params[:id]}"), @body, @content_type + FourStore.post @uri, @body, @content_type end # Create or updata a resource put "/#{SERVICE}/:id/?" do - #puts @body - #puts @content_type - FourStore.put uri("/#{SERVICE}/#{params[:id]}"), @body, @content_type + FourStore.put @uri, @body, @content_type end # Delete a resource delete "/#{SERVICE}/:id/?" do - FourStore.delete uri("/#{SERVICE}/#{params[:id]}") + FourStore.delete @uri end end diff --git a/opentox-server.gemspec b/opentox-server.gemspec index 199a67a..8628c5e 100644 --- a/opentox-server.gemspec +++ b/opentox-server.gemspec @@ -27,5 +27,4 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'roo' s.add_runtime_dependency 'spreadsheet', "= 0.6.8" # cannot load 0.7.0 s.add_runtime_dependency 'unicorn' - s.add_runtime_dependency 'rdf-n3' end |