Skip to content

Commit

Permalink
adding get node and set node properties
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 17, 2010
1 parent ef9f673 commit 7f57fcb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7f57fcb

Please sign in to comment.