diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 53959c0..3ff3cb2 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -7,23 +7,34 @@ class Rest class << self def get_root - rescue_404(get('/')) + rescue_ij { get('/') } end def create_node(*args) if args[0].respond_to?(:each_pair) && args[0] options = { :body => args[0].to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_404(post("/node", options)) + rescue_ij { post("/node", options) } else - rescue_404(post("/node")) + rescue_ij { post("/node") } end end + def get_node(id) + rescue_ij { get("/node/#{id}") } + end + + def set_node_properties(id, properties) + options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} } + rescue_ij { put("/node/#{id}/properties", options) } + end private - def rescue_404(response) +# Rescue from Invalid JSON error thrown by Crack Gem + + def rescue_ij(&block) begin + response = yield response = response.parsed_response rescue response = nil diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index 2670c4b..67e3495 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -27,4 +27,36 @@ end end + describe "get_node" do + it "can get a node that exists" do + existing_node = Neography::Rest.get_node(1) + existing_node.should_not be_nil + existing_node.should have_key("self") + existing_node["self"].split('/').last.should == "1" + end + + it "returns nil if it tries to get a node that does not exist" do + existing_node = Neography::Rest.get_node(9999) + existing_node.should be_nil + end + end + + describe "set_node_properties" do + it "can set a node's properties" do + new_node = Neography::Rest.create_node + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.set_node_properties(new_node[:id], {"weight" => 200, "eyes" => "brown"}) + existing_node = Neography::Rest.get_node(new_node[:id]) + existing_node["data"]["weight"].should == 200 + existing_node["data"]["eyes"].should == "brown" + end + + it "returns nil if it fails to set 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.set_node_properties(new_node[:id].to_i + 1, {"weight" => 200, "eyes" => "brown"}).should be_nil + end + end + + end \ No newline at end of file