myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2709] trunk: added relationships API


From: noreply
Subject: [myexperiment-hackers] [2709] trunk: added relationships API
Date: Tue, 20 Sep 2011 11:32:27 -0400 (EDT)

Revision
2709
Author
dgc
Date
2011-09-20 11:32:27 -0400 (Tue, 20 Sep 2011)

Log Message

added relationships API

Modified Paths

Diff

Modified: trunk/config/routes.rb (2708 => 2709)


--- trunk/config/routes.rb	2011-09-20 15:31:46 UTC (rev 2708)
+++ trunk/config/routes.rb	2011-09-20 15:32:27 UTC (rev 2709)
@@ -64,6 +64,9 @@
   # Ontologies
   map.resources :ontologies
 
+  # Predicates
+  map.resources :predicates
+
   # mashup
   map.resource :mashup
   

Modified: trunk/config/tables.xml


(Binary files differ)

Modified: trunk/lib/rest.rb (2708 => 2709)


--- trunk/lib/rest.rb	2011-09-20 15:31:46 UTC (rev 2708)
+++ trunk/lib/rest.rb	2011-09-20 15:32:27 UTC (rev 2709)
@@ -579,7 +579,7 @@
     when 'License';                return license_url(ob)
     when 'CurationEvent';          return nil
     when 'Ontology';               return ontology_url(ob)
-    when 'Predicate';              return nil
+    when 'Predicate';              return predicate_url(ob)
     when 'Relationship';           return nil
 
     when 'Creditation';     return nil
@@ -738,6 +738,7 @@
   return [Job, $1, is_local]            if uri.path =~ /^\/jobs\/([\d]+)$/
   return [Download, $1, is_local]       if uri.path =~ /^\/downloads\/([\d]+)$/
   return [Ontology, $1, is_local]       if uri.path =~ /^\/ontologies\/([\d]+)$/
+  return [Predicate, $1, is_local]      if uri.path =~ /^\/predicates\/([\d]+)$/
 
   nil
 
@@ -2070,6 +2071,69 @@
 def delete_predicate(opts)
   predicate_aux('destroy', opts)
 end
+
+# Relationships
+
+def relationship_aux(action, opts)
+
+  if action != "destroy"
+
+    data = ""
+
+    subject     = parse_element(data, :resource, '/relationship/subject')
+    predicate   = parse_element(data, :resource, '/relationship/predicate')
+    objekt      = parse_element(data, :resource, '/relationship/object')
+    context     = parse_element(data, :resource, '/relationship/context')
+  end
+
+  # Obtain object
+
+  case action
+    when 'create':
+      return rest_response(401, :reason => "Not authorised to create a relationship") unless Authorization.is_authorized_for_type?('create', 'Relationship', opts[:user], context)
+      ob = Relationship.new(:user => opts[:user])
+    when 'read', 'update', 'destroy':
+      ob = obtain_rest_resource('Relationship', opts[:query]['id'], opts[:query]['version'], opts[:user], action)
+    else
+      raise "Invalid action '#{action}'"
+  end
+
+  return if ob.nil? # appropriate rest response already given
+
+  if action == "destroy"
+
+    ob.destroy
+
+  else
+
+    # build it
+
+    ob.subject   = subject   if subject
+    ob.predicate = predicate if predicate
+    ob.objekt    = objekt    if objekt
+    ob.context   = context   if context
+
+    if not ob.save
+      return rest_response(400, :object => ob)
+    end
+  end
+
+  rest_get_request(ob, "relationship", opts[:user],
+      rest_resource_uri(ob), "relationship", { "id" => ob.id.to_s })
+end
+
+def post_relationship(opts)
+  relationship_aux('create', opts)
+end
+
+def put_relationship(opts)
+  relationship_aux('update', opts)
+end
+
+def delete_relationship(opts)
+  relationship_aux('destroy', opts)
+end
+
 # Call dispatcher
 
 def rest_call_request(req_uri, format, rules, user, query)

Modified: trunk/test/functional/api_controller_test.rb (2708 => 2709)


--- trunk/test/functional/api_controller_test.rb	2011-09-20 15:31:46 UTC (rev 2708)
+++ trunk/test/functional/api_controller_test.rb	2011-09-20 15:32:27 UTC (rev 2709)
@@ -11,7 +11,7 @@
 
 class ApiControllerTest < Test::Unit::TestCase
 
-  fixtures :workflows, :users, :content_types, :licenses, :ontologies
+  fixtures :workflows, :users, :content_types, :licenses, :ontologies, :predicates, :packs
 
   def setup
     @controller = ApiController.new
@@ -849,6 +849,60 @@
     assert_response(:not_found)
   end
 
+  def test_relationships
+
+    existing_relationships = Relationship.find(:all)
+
+    login_as(:john)
+
+    subject_url   = rest_resource_uri(workflows(:workflow_branch_choice))
+    predicate_url = rest_resource_uri(predicates(:test_predicate_1))
+    objekt_url    = rest_resource_uri(workflows(:workflow_dilbert))
+    context_url   = rest_resource_uri(packs(:pack_1))
+
+    # post a relationship
+
+    rest_request(:post, 'relationship', "<?xml version='1.0'?>
+      <relationship>
+        <subject resource='#{subject_url}'/>
+        <predicate resource='#{predicate_url}'/>
+        <object resource='#{objekt_url}'/>
+        <context resource='#{context_url}'/>
+      </relationship>")
+
+    assert_response(:success)
+
+    extra_relationships = Relationship.find(:all) - existing_relationships
+
+    assert_equal(extra_relationships.length, 1)
+
+    relationship = extra_relationships.first
+
+    # get the relationship
+
+    response = rest_request(:get, 'relationship', nil, "id" => relationship.id,
+        "elements" => "user,subject,predicate,object,context")
+
+    assert_response(:success)
+
+    assert_equal(subject_url,   response.find_first('/relationship/subject/*/@resource').value)
+    assert_equal(predicate_url, response.find_first('/relationship/predicate/@resource').value)
+    assert_equal(objekt_url,    response.find_first('/relationship/object/*/@resource').value)
+    assert_equal(context_url,   response.find_first('/relationship/context/*/@resource').value)
+
+    # delete the relationship
+
+    rest_request(:delete, 'relationship', nil, "id" => relationship.id)
+
+    assert_response(:success)
+
+    # try to get the deleted relationship
+
+    rest_request(:get, 'relationship', nil, "id" => relationship.id)
+
+    assert_response(:not_found)
+  end
+
   private
 
   def rest_request(method, uri, data = "" query = {})

reply via email to

[Prev in Thread] Current Thread [Next in Thread]