diff options
Diffstat (limited to 'lib/opentox.rb')
-rw-r--r-- | lib/opentox.rb | 74 |
1 files changed, 48 insertions, 26 deletions
diff --git a/lib/opentox.rb b/lib/opentox.rb index e3c1f3b..7a73d66 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -1,12 +1,17 @@ require 'sinatra/base' require "sinatra/reloader" +ENV["RACK_ENV"] ||= "production" + +logfile = File.join(ENV['HOME'], ".opentox","log","#{ENV["RACK_ENV"]}.log") +$logger = OTLogger.new(logfile) + module OpenTox # Base class for OpenTox services class Service < Sinatra::Base + include Backend - helpers Sinatra::UrlForHelper # use OpenTox error handling set :raise_errors, false set :show_exceptions, false @@ -16,36 +21,15 @@ module OpenTox register Sinatra::Reloader end - helpers do - def uri - params[:id] ? url_for("/#{params[:id]}", :full) : "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}" - end - end - before do - @accept = request.env['HTTP_ACCEPT'] - response['Content-Type'] = @accept - # TODO: A+A + request.content_type ? response['Content-Type'] = request.content_type : response['Content-Type'] = request.env['HTTP_ACCEPT'] end + # Attention: Error within tasks are catched by Task.create error do - # TODO: convert to OpenTox::Error and set URI error = request.env['sinatra.error'] - #error.uri = uri if error.respond_to? :report - # Errors are formated according to acccept-header - case @accept - when 'application/rdf+xml' - body = error.report.to_rdfxml - when /html/ - # TODO - # body = error.report.to_html - body = error.report.to_turtle - when "text/n3" - body = error.report.to_ntriples - else - body = error.report.to_turtle - end + body = error.report.to_turtle else response['Content-Type'] = "text/plain" body = error.message @@ -54,6 +38,44 @@ module OpenTox error.respond_to?(:http_code) ? code = error.http_code : code = 500 halt code, body end + + # Default methods, may be overwritten by derived services + # 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 '/?' do + FourStore.list request.env['HTTP_ACCEPT'] + end + + # Create a new resource + # TODO: handle multipart uploads + post '/?' do + rdf = request.body.read + uri = uri(SecureRandom.uuid) + FourStore.put(uri, rdf, request.content_type) unless rdf == '' + response['Content-Type'] = "text/uri-list" + uri + end + + # Get resource representation + get '/:id/?' do + FourStore.get(uri("/#{params[:id]}"), request.env['HTTP_ACCEPT']) + end + + # Modify (i.e. add rdf statments to) a resource + post '/:id/?' do + FourStore.post uri("/#{params[:id]}"), request.body.read, request.content_type + end + + # Create or updata a resource + put '/:id/?' do + FourStore.put uri("/#{params[:id]}"), request.body.read, request.content_type + end + + # Delete a resource + delete '/:id/?' do + FourStore.delete uri("/#{params[:id]}") + end + end end - |