Skip to content

Commit

Permalink
Spliting off basic Rest wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 17, 2010
1 parent 9a9b24a commit ef9f673
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 9 deletions.
24 changes: 18 additions & 6 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ Use the defaults (shown above) or create neography.rb in your config/initializer

=== Documentation

Neography::Node.new # Create an empty node
Neography::Node.new(:age => 31, :name => "Max") # Create a node with some properties
Neography::Node.load(id) # Get a node and its properties
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

... and a work in progress more rubyish layer that's not quite ready for use yet.

Neography::Node.new # Create an empty node
Neography::Node.new(:age => 31, :name => "Max") # Create a node with some properties
Neography::Node.load(id) # Get a node and its properties
Neography::Node.set_properties(3, {:age => 31, :name => "Max"} ) # Deletes any existing properties with the passed hash
Neography::Node.properties(3) # Returns a hash of a node's properties or nil
Neography::Node.remove_property(3, :age) # Deletes the existing property
Neography::Node.del(3) # Deletes the node
Neography::Node.properties(3) # Returns a hash of a node's properties or nil
Neography::Node.remove_property(3, :age) # Deletes the existing property
Neography::Node.del(3) # Deletes a node without any relationships
Neography::Node.del!(3) # Deletes a node and all its relationships


=== License
Expand Down
3 changes: 2 additions & 1 deletion lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ def evaluate_response(response)
require 'httparty'
require 'json'
require 'logger'
require 'net-http-spy'
#require 'net-http-spy'

#Net::HTTP.http_logger_options = {:verbose => true}
#Net::HTTP.http_logger_options = {:body => true}

require 'neography/config'
require 'neography/rest'
require 'neography/neo'
require 'neography/node'
require 'neography/relationship'
Expand Down
50 changes: 50 additions & 0 deletions lib/neography/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def new(*args)
end
end

# create is the same as new
alias_method :create, :new

def load(id)
begin
response = get("/node/#{id}")
Expand Down Expand Up @@ -62,6 +65,38 @@ def del(id)
response.parsed_response
end

def del!(id)
relationships = rels(id)
relationships.each {|r| Relationship.del(r[:rel_id])}
response = delete("/node/#{id}")
evaluate_response(response)
response.parsed_response
end

def exists?(id)
load(id).nil? == false
end

def rels(id, dir=nil, types=nil)
case dir
when :incoming
dir = "in"
when :outgoing
dir = "out"
else
dir = "all"
end

if types.nil?
response = get("/node/#{id}/relationships/#{dir}")
else
response = get("/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}")
end
evaluate_response(response)
build_rels(response)
end


private

def build_node(response)
Expand All @@ -74,6 +109,21 @@ def build_node(response)
node
end

def build_rels(response)
begin
rels = response.parsed_response
rels.each do |r|
r[:rel_id] = r["self"].split('/').last
r[:start_node] = r["start"].split('/').last
r[:end_node] = r["end"].split('/').last
end
rescue
rels = Array.new
end
rels
end


end
end
end
37 changes: 37 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Neography
class Rest
include HTTParty
base_uri 'http://localhost:9999'
format :json

class << self

def get_root
rescue_404(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))
else
rescue_404(post("/node"))
end
end


private

def rescue_404(response)
begin
response = response.parsed_response
rescue
response = nil
end
response
end

end

end
end
33 changes: 31 additions & 2 deletions spec/integration/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe Neography::Node do
it "can create an empty node" do
Neography::Node.new.should include(:neo_id)
node1 = Neography::Node.new
node1.should include(:neo_id)
Neography::Node.exists?(node1[:neo_id]).should be_true
end

it "can create a node with one property" do
Expand All @@ -13,6 +15,10 @@
Neography::Node.new(:age => 31, :name => "Max").should include("age"=>31, "name"=>"Max")
end

it "can tell if a node does not exist" do
Neography::Node.exists?(999).should be_false
end

it "can find a node by its id" do
Neography::Node.load(2).should include(:neo_id=>"2")
end
Expand Down Expand Up @@ -80,12 +86,35 @@
Neography::Node.del(newnode[:neo_id]).should be_nil
end

it "returns nil if it tries to delete a node that has existing relationships" do
it "does not delete a node that has existing relationships" do
node1 = Neography::Node.new
node2 = Neography::Node.new
Neography::Relationship.new(:friends, node1, node2)
Neography::Node.del(node1[:neo_id]).should be_nil
Neography::Node.del(node2[:neo_id]).should be_nil
Neography::Node.exists?(node1[:neo_id]).should be_true
Neography::Node.exists?(node2[:neo_id]).should be_true
end

it "does delete! a node that has existing relationships" do
node1 = Neography::Node.new
node2 = Neography::Node.new
Neography::Relationship.new(:friends, node1, node2)
Neography::Node.del!(node1[:neo_id]).should be_nil
Neography::Node.del!(node2[:neo_id]).should be_nil
Neography::Node.exists?(node1[:neo_id]).should be_false
Neography::Node.exists?(node2[:neo_id]).should be_false
end

it "can find get a node's existing relationships" do
node1 = Neography::Node.new
node2 = Neography::Node.new
Neography::Relationship.new(:friends, node1, node2)
Neography::Node.rels(node1[:neo_id]).to_s.should include("rel_id")
Neography::Node.rels(node1[:neo_id])[0][:rel_id].should_not be_nil

end



end
30 changes: 30 additions & 0 deletions spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Neography::Rest do

describe "get_root" do
it "can get the root node" do
root_node = Neography::Rest.get_root
root_node.should have_key("reference_node")
end
end

describe "create_node" do
it "can create an empty node" do
new_node = Neography::Rest.create_node
new_node.should_not be_nil
end

it "can create a node with one property" do
new_node = Neography::Rest.create_node("name" => "Max")
new_node["data"]["name"].should == "Max"
end

it "can create a node with more than one property" do
new_node = Neography::Rest.create_node("age" => 31, "name" => "Max")
new_node["data"]["name"].should == "Max"
new_node["data"]["age"].should == 31
end
end

end

0 comments on commit ef9f673

Please sign in to comment.