diff --git a/README.rdoc b/README.rdoc index e2df69b..d8a55b6 100644 --- a/README.rdoc +++ b/README.rdoc @@ -31,12 +31,15 @@ There are two ways to use Neography: A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and returns JSON or Nil: - Neography::Rest.get_root # Get the root node - Neography::Rest.create_node # Create an empty node - Neography::Rest.create_node("age" => 31, "name" => "Max") # Create a node with some properties - Neography::Rest.get_node(id) # Get a node and its properties - Neography::Rest.set_node_properties(id, {"weight" => 200}) # Set a node's properties - Neography::Rest.get_node_properties(id) # Get just the properties + Neography::Rest.get_root # Get the root node + Neography::Rest.create_node # Create an empty node + Neography::Rest.create_node("age" => 31, "name" => "Max") # Create a node with some properties + Neography::Rest.get_node(id) # Get a node and its properties + Neography::Rest.set_node_properties(id, {"weight" => 200}) # Set a node's properties + Neography::Rest.get_node_properties(id) # Get just the properties + Neography::Rest.remove_node_properties(id) # Remove all properties of a node + Neography::Rest.remove_node_properties(id, "weight") # Remove one property of a node + Neography::Rest.remove_node_properties(id, ["weight","age"]) # Remove multiple properties of a node ... and a work in progress more rubyish layer that's not quite ready for use yet. diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 280c591..4927aa9 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -32,6 +32,18 @@ def get_node_properties(id) rescue_ij { get("/node/#{id}/properties") } end + def remove_node_properties(id, properties = nil) + if properties.nil? + rescue_ij { delete("/node/#{id}/properties") } + else + properties.to_a.each do |property| + rescue_ij { delete("/node/#{id}/properties/#{property}") } + end + end + end + + + private # Rescue from Invalid JSON error thrown by Crack Gem diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index 702228e..704120a 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -70,15 +70,49 @@ it "returns nil if it gets the properties on a node that does not have any" do new_node = Neography::Rest.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.get_node_properties(new_node[:id].to_i + 1).should be_nil + Neography::Rest.get_node_properties(new_node[:id].to_i + 10000).should be_nil end it "returns nil if it fails to get properties on a node that does not exist" do new_node = Neography::Rest.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.get_node_properties(new_node[:id].to_i + 1).should be_nil + Neography::Rest.get_node_properties(new_node[:id].to_i + 10000).should be_nil end end + describe "remove_node_properties" do + it "can remove a node's properties" do + new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown") + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.remove_node_properties(new_node[:id]) + Neography::Rest.get_node_properties(new_node[:id]).should be_nil + end + + it "returns nil if it fails to remove the properties of a node that does not exist" do + new_node = Neography::Rest.create_node + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.remove_node_properties(new_node[:id].to_i + 10000).should be_nil + end + + it "can remove a specific node property" do + new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown") + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.remove_node_properties(new_node[:id], "weight") + node_properties = Neography::Rest.get_node_properties(new_node[:id]) + node_properties["weight"].should be_nil + node_properties["eyes"].should == "brown" + end + + it "can remove more than one property" do + new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown", "height" => "2m") + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.remove_node_properties(new_node[:id], ["weight", "eyes"]) + node_properties = Neography::Rest.get_node_properties(new_node[:id]) + node_properties["weight"].should be_nil + node_properties["eyes"].should be_nil + end + + end + end \ No newline at end of file