Skip to content

Commit

Permalink
Implemented and tested GET related nodes and PUT node properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kleppmann committed Jun 17, 2009
1 parent 4c363fb commit c3d9162
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 10 additions & 4 deletions lib/rest/rest.rb
Expand Up @@ -78,7 +78,14 @@ def self.included(c)
content_type :json
Neo4j::Transaction.run do
node = Neo4j.load(params[:id])
{params[:prop]=>node.get_property(params[:prop])}.to_json
return 404, "Can't find node with id #{params[:id]}" if node.nil?
prop = params[:prop].to_sym
if node.class.relationships_info.keys.include?(prop)
rels = node.send(prop) || []
rels.map{|rel| rel.props}.to_json
else
{prop => node.get_property(prop)}.to_json
end
end
end

Expand Down Expand Up @@ -107,7 +114,7 @@ def self.included(c)
return 400, "Wrong type id '#{to_node_id}' expected '#{to_clazz}' got '#{other_node.class.to_s}'"
end

rel_obj = node.instance_eval "#{rel}.new(other_node)" # TODO use send method instead
rel_obj = node.send(rel).new(other_node)

return 400, "Can't create relationship to #{to_clazz}" if rel_obj.nil?

Expand All @@ -117,7 +124,6 @@ def self.included(c)
end



Sinatra::Application.put("/nodes/#{classname}/:id/:prop") do
content_type :json
Neo4j::Transaction.run do
Expand Down Expand Up @@ -147,7 +153,7 @@ def self.included(c)
body = request.body.read
data = JSON.parse(body)
node = Neo4j.load(params[:id])
node.update(data)
node.update(data, true)
response = node.props.to_json
response
end
Expand Down
24 changes: 21 additions & 3 deletions test/rest/rest_spec.rb
Expand Up @@ -96,20 +96,36 @@ class Foo
it "should create a relationship on POST /nodes/Person/friends" do
adam = Person.new
adam.name = 'adam'

bertil = Person.new
bertil.name = 'bertil'

bertil.friends << Person.new

# when
post "/nodes/Person/#{adam.neo_node_id}/friends", { :uri => bertil._uri }.to_json

# then
status.should == 201
response.location.should == "/relations/2"
response.location.should == "/relations/1" # starts counting from 0
adam.friends.should include(bertil)
end

it "should list related nodes on GET /nodes/Person/friends" do
adam = Person.new
adam.name = 'adam'
bertil = Person.new
bertil.name = 'bertil'
adam.friends << bertil

# when
get "/nodes/Person/#{adam.neo_node_id}/friends"

# then
status.should == 200
body = JSON.parse(response.body)
body.size.should == 1
body[0]['id'].should == bertil.neo_node_id
end

it "should be possible to load a relationship on GET /relations/<id>" do
adam = Person.new
bertil = Person.new
Expand Down Expand Up @@ -175,6 +191,7 @@ class Foo
# given
p = Person.new
p.name = 'sune123'
p[:some_property] = 'foo'

# when
data = {:name => 'blah', :dynamic_property => 'cool stuff'}
Expand All @@ -183,6 +200,7 @@ class Foo
# then
status.should == 200
p.name.should == 'blah'
p.props['some_property'].should be_nil
p.props['dynamic_property'].should == 'cool stuff'
end

Expand Down

0 comments on commit c3d9162

Please sign in to comment.