diff options
author | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-23 18:48:47 +0200 |
---|---|---|
committer | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-23 18:48:47 +0200 |
commit | f590f571911b83dbab20020688e0af0bb6c70a9f (patch) | |
tree | 33874141efa679d73d9d849133d82fc95bf4ce83 | |
parent | 441de0ec5ac7e18a6dd6134d68b166272e73dcc2 (diff) |
adjust task to api
-rw-r--r-- | application.rb | 67 |
1 files changed, 49 insertions, 18 deletions
diff --git a/application.rb b/application.rb index 8ee164c..abb63cf 100644 --- a/application.rb +++ b/application.rb @@ -11,10 +11,14 @@ class Task property :parent_id, Integer property :pid, Integer property :uri, String, :length => 255 - property :resource, String, :length => 255 - property :status, String, :default => "created" - property :created_at, DateTime - property :finished_at, DateTime + property :created_at, DateTime + property :finished_at, DateTime + + property :resultURI, String, :length => 255 + property :percentageCompleted, Float, :default => 0 + property :hasStatus, String, :default => "Running" #possible states are: "Cancelled", "Completed", "Running", "Error" + property :title, String, :length => 255 + property :creator, String, :length => 255 property :description, Text is :tree, :order => :created_at @@ -28,9 +32,28 @@ get '/?' do end get '/:id/?' do - response['Content-Type'] = 'application/x-yaml' - task = Task.get(params[:id]) - task.to_yaml + task = Task.get(params[:id]) + halt 404, "Task #{params[:id]} not found." unless task + + case request.env['HTTP_ACCEPT'] + #when /text\/x-yaml|\*\/\*/ # matches 'text/x-yaml', '*/*' + # response['Content-Type'] = 'text/x-yaml' + # task.to_yaml + when /application\/rdf\+xml|\*\/\*/ + response['Content-Type'] = 'application/rdf+xml' + owl = OpenTox::Owl.create 'Task', task.uri + owl.set("creator",task.creator) + owl.set("title",task.title) + owl.set("date",task.created_at.to_s) + owl.set("hasStatus",task.hasStatus) + owl.set("resultURI",task.resultURI) + owl.set("percentageCompleted",task.percentageCompleted) + owl.set("description",task.description) + owl.rdf + else + #TODO implement to_owl + halt 400, "MIME type '"+request.env['HTTP_ACCEPT'].to_s+"' not supported, valid Accept-Headers are \"application/rdf+xml\" and \"text/x-yaml\"." + end end # dynamic access to Task properties @@ -50,15 +73,18 @@ post '/?' do task.uri + "\n" end -put '/:id/:status/?' do +put '/:id/:hasStatus/?' do + task = Task.get(params[:id]) - task.status = params[:status] unless /pid|parent/ =~ params[:status] + halt 404,"Task #{params[:id]} not found." unless task + task.hasStatus = params[:hasStatus] unless /pid|parent/ =~ params[:hasStatus] task.description = params[:description] if params[:description] - case params[:status] - when "completed" - + + case params[:hasStatus] + when "Completed" LOGGER.debug "Task " + params[:id].to_s + " completed" - task.resource = params[:resource] + halt 402,"Param resultURI when completing task" unless params[:resultURI] + task.resultURI = params[:resultURI] task.finished_at = DateTime.now task.pid = nil when "pid" @@ -66,20 +92,25 @@ put '/:id/:status/?' do task.pid = params[:pid] when "parent" task.parent = Task.first(:uri => params[:uri]) - when /cancelled|failed/ + when /Cancelled|Error/ Process.kill(9,task.pid) unless task.pid.nil? task.pid = nil - RestClient.put url_for("/#{task.parent.id}/#{params[:status]}"), {} unless task.parent.nil? # recursevly kill parent tasks - end - task.save + RestClient.put url_for("/#{task.parent.id}/#{params[:hasStatus]}"),{} unless task.parent.nil? # recursevly kill parent tasks + else + halt 402,"Invalid value for hasStatus: '"+params[:hasStatus].to_s+"'" + end + + halt 500,"could not save task" unless task.save + end delete '/:id/?' do task = Task.get(params[:id]) + halt 404, "Task #{params[:id]} not found." unless task begin Process.kill(9,task.pid) unless task.pid.nil? rescue - "Cannot kill task with pid #{task.pid}" + halt 500,"Cannot kill task with pid #{task.pid}" end task.destroy! response['Content-Type'] = 'text/plain' |