diff options
author | Christoph Helma <helma@in-silico.ch> | 2012-05-07 14:28:56 +0000 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2012-05-07 14:28:56 +0000 |
commit | 3f27c9e7d918a4a8ef7fd77eb9042d84e85b4fa5 (patch) | |
tree | 6bff82498a4c1670e38ce1549b236ee858307fea | |
parent | 1ed38ff471e9462831d4da564ab5b2001a555e3c (diff) |
code cleanup
-rw-r--r-- | lib/4store.rb | 21 | ||||
-rw-r--r-- | lib/opentox.rb | 41 |
2 files changed, 31 insertions, 31 deletions
diff --git a/lib/4store.rb b/lib/4store.rb index 361de70..7738e38 100644 --- a/lib/4store.rb +++ b/lib/4store.rb @@ -2,8 +2,6 @@ module OpenTox module Backend class FourStore - #TODO: catch 4store errors - @@mime_format = { "application/rdf+xml" => :rdfxml, "text/turtle" => :turtle, @@ -14,7 +12,6 @@ module OpenTox #"text/x-yaml" => :yaml, #"text/yaml" => :yaml, "text/html" => :html, - # TODO: forms #/sparql/ => :sparql #removed to prevent sparql injections } @@ -30,7 +27,6 @@ module OpenTox @@accept_formats = [:rdfxml, :turtle, :ntriples, :uri_list, :html] #, :json, :yaml] @@content_type_formats = [:rdfxml, :turtle, :ntriples]#, :json, :yaml] - @@rdf_formats = [:rdfxml, :turtle, :ntriples] def self.list mime_type mime_type = "text/html" if mime_type.match(%r{\*/\*}) @@ -51,16 +47,17 @@ module OpenTox query sparql, mime_type end - # TODO: add created at, modified at statements, submitter? - 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] - rdf = convert rdf, @@mime_format[mime_type], :ntriples#, uri unless mime_type == 'text/plain' + 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" + rdf = convert rdf, @@mime_format[mime_type], :ntriples + rdf = "<#{uri}> <#{RDF.type}> <#{klass}>." unless rdf # create empty resource + rdf += "\n<#{uri}> <#{RDF::DC.date}> \"#{DateTime.now}\"." unless rdf.match(%r{#{RDF::DC.date}}) RestClient.post File.join(four_store_uri,"data")+"/", :data => rdf, :graph => uri, "mime-type" => "application/x-turtle" # not very consistent in 4store 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] + bad_request_error "Reqest body empty." unless rdf # create empty resource uuid = uri.sub(/\/$/,'').split('/').last bad_request_error "'#{uri}' is not a valid URI." unless uuid =~ /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/ if !skip_rewrite @@ -68,10 +65,6 @@ module OpenTox elsif mime_type != "text/plain" # ntriples are not converted rdf = convert rdf, @@mime_format[mime_type], :ntriples end - unless rdf # create empty resource - rdf = "<#{uri}> <#{RDF.type}> <#{klass}>." - rdf += "\n<#{uri}> <#{RDF::DC.date}> \"#{DateTime.now}\"." - end RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => "application/x-turtle" # content-type not very consistent in 4store end @@ -81,13 +74,10 @@ module OpenTox def self.update sparql RestClient.post(update_uri, :update => sparql ) - #RestClient.get(update_uri, :params => { :update => sparql }) end def self.query sparql, mime_type if sparql =~ /SELECT/i - #puts sparql_uri - #puts sparql list = RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/<|>/,'').split("\n") list.shift return list unless mime_type @@ -180,6 +170,7 @@ module OpenTox end def self.four_store_uri + # TODO remove credentials from URI 9security risk in tasks) $four_store[:uri].sub(%r{//},"//#{$four_store[:user]}:#{$four_store[:password]}@") end diff --git a/lib/opentox.rb b/lib/opentox.rb index 5c3bc25..86fcd9c 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -17,7 +17,6 @@ module OpenTox set :raise_errors, false set :show_exceptions, false set :static, false - set :prefix, SERVICE configure :development do register Sinatra::Reloader @@ -25,7 +24,19 @@ module OpenTox before do request.content_type ? response['Content-Type'] = request.content_type : response['Content-Type'] = request.env['HTTP_ACCEPT'] - @prefix = "task" + parse_input if request.request_method =~ /POST|PUT/ + end + + helpers do + def parse_input + if request.content_type == "multipart/form-data" + @body = File.read(params[:file][:tempfile]) + @content_type = params[:file][:type] + else + @body = request.body.read + @content_type = request.content_type + end + end end # Attention: Error within tasks are catched by Task.create @@ -46,38 +57,36 @@ module OpenTox # see http://jcalcote.wordpress.com/2008/10/16/put-or-post-the-rest-of-the-story/ # Get a list of objects at the server - get "/#{settings.prefix}/?" do + get "/#{SERVICE}/?" do FourStore.list request.env['HTTP_ACCEPT'] end # Create a new resource - # TODO: handle multipart uploads - post "/#{settings.prefix}/?" do - rdf = request.body.read - uri = uri("/#{settings.prefix}/#{SecureRandom.uuid}") - FourStore.put(uri, rdf, request.content_type) unless rdf == '' + post "/#{SERVICE}/?" do + uri = uri("/#{SERVICE}/#{SecureRandom.uuid}") + FourStore.post(uri, @body, @content_type) response['Content-Type'] = "text/uri-list" uri end # Get resource representation - get "/#{settings.prefix}/id/?" do - FourStore.get(uri("/#{settings.prefix}/#{params[:id]}"), request.env['HTTP_ACCEPT']) + get "/#{SERVICE}/id/?" do + FourStore.get(uri("/#{SERVICE}/#{params[:id]}"), request.env['HTTP_ACCEPT']) end # Modify (i.e. add rdf statments to) a resource - post "/#{settings.prefix}/:id/?" do - FourStore.post uri("/#{settings.prefix}/#{params[:id]}"), request.body.read, request.content_type + post "/#{SERVICE}/:id/?" do + FourStore.post uri("/#{SERVICE}/#{params[:id]}"), @body, @content_type end # Create or updata a resource - put "/#{settings.prefix}/:id/?" do - FourStore.put uri("/#{settings.prefix}/#{params[:id]}"), request.body.read, request.content_type + put "/#{SERVICE}/:id/?" do + FourStore.put uri("/#{SERVICE}/#{params[:id]}"), @body, @content_type end # Delete a resource - delete "/#{settings.prefix}/:id/?" do - FourStore.delete uri("/#{settings.prefix}/#{params[:id]}") + delete "/#{SERVICE}/:id/?" do + FourStore.delete uri("/#{SERVICE}/#{params[:id]}") end end |