diff --git a/README.rdoc b/README.rdoc index 52a37be..84a7968 100644 --- a/README.rdoc +++ b/README.rdoc @@ -37,7 +37,8 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and Neography::Rest.get_node(id) # Get a node and its properties Neography::Rest.reset_node_properties(id, {"age" => 31}) # Reset a node's 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_node_properties(id) # Get just the node properties + Neography::Rest.get_node_properties(id, ["weight","age"]) # Get some of the node 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 diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index fd8ed19..14cb840 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -28,8 +28,18 @@ def reset_node_properties(id, properties) rescue_ij { put("/node/#{id}/properties", options) } end - def get_node_properties(id) - rescue_ij { get("/node/#{id}/properties") } + def get_node_properties(id, properties = nil) + if properties.nil? + rescue_ij { get("/node/#{id}/properties") } + else + node_properties = Hash.new + properties.to_a.each do |property| + value = rescue_ij { get("/node/#{id}/properties/#{property}") } + node_properties[property] = value unless value.nil? + end + return nil if node_properties.empty? + node_properties + end end def remove_node_properties(id, properties = nil) diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index dfaa414..cdc0415 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -82,7 +82,7 @@ end describe "get_node_properties" do - it "can get a node's properties" do + it "can get all a node's properties" do new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown") new_node[:id] = new_node["self"].split('/').last node_properties = Neography::Rest.get_node_properties(new_node[:id]) @@ -90,10 +90,25 @@ node_properties["eyes"].should == "brown" end + it "can get some of a node's properties" do + new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown", "height" => "2m") + new_node[:id] = new_node["self"].split('/').last + node_properties = Neography::Rest.get_node_properties(new_node[:id], ["weight", "height"]) + node_properties["weight"].should == 200 + node_properties["height"].should == "2m" + node_properties["eyes"].should be_nil + end + 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 + 10000).should be_nil + Neography::Rest.get_node_properties(new_node[:id]).should be_nil + end + + it "returns nil if it tries to get some of 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], ["weight", "height"]).should be_nil end it "returns nil if it fails to get properties on a node that does not exist" do