From bc1f9fc66e6dc12f1742fb660d8d9b880693fb99 Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Sat, 13 Nov 2010 23:28:44 -0800 Subject: [PATCH] starting with basic functionality --- .gitignore | 1 + Gemfile.lock | 36 +++++++++++++++++++++++++++++ lib/neography.rb | 52 ++++++++++++++++++++++++++++++++++++++++-- lib/neography/neo.rb | 12 ++++++++++ lib/neography/node.rb | 48 ++++++++++++++++++++++++++++++++++++++ neography.gemspec | 13 +++++++---- spec/neography_spec.rb | 50 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 206 insertions(+), 6 deletions(-) create mode 100644 Gemfile.lock create mode 100644 lib/neography/neo.rb create mode 100644 lib/neography/node.rb create mode 100644 spec/neography_spec.rb diff --git a/.gitignore b/.gitignore index 9f30a35..29e89a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ pkg/* *.gem .bundle +log/* \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..bbe3d3f --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,36 @@ +PATH + remote: . + specs: + neography (0.0.1) + httparty (~> 0.6.1) + json + +GEM + remote: http://rubygems.org/ + specs: + crack (0.1.8) + diff-lcs (1.1.2) + httparty (0.6.1) + crack (= 0.1.8) + json (1.4.6-java) + net-http-spy (0.2.1) + rspec (2.0.1) + rspec-core (~> 2.0.1) + rspec-expectations (~> 2.0.1) + rspec-mocks (~> 2.0.1) + rspec-core (2.0.1) + rspec-expectations (2.0.1) + diff-lcs (>= 1.1.2) + rspec-mocks (2.0.1) + rspec-core (~> 2.0.1) + rspec-expectations (~> 2.0.1) + +PLATFORMS + java + +DEPENDENCIES + httparty (~> 0.6.1) + json + neography! + net-http-spy (~> 0.2.1) + rspec (~> 2.0.0.beta.22) diff --git a/lib/neography.rb b/lib/neography.rb index 637c7e7..8db7e79 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -1,3 +1,51 @@ -module Neography - # Your code goes here... +def find_and_require_user_defined_code + extensions_path = ENV['neography_extensions'] || "~/.neography" + extensions_path = File.expand_path(extensions_path) + if File.exists?(extensions_path) + Dir.open extensions_path do |dir| + dir.entries.each do |file| + if file.split('.').size > 1 && file.split('.').last == 'rb' + extension = File.join(File.expand_path(extensions_path), file) + require(extension) && puts("Loaded Extension: #{extension}") + end + end + end + else + puts "No Extensions Found: #{extensions_path}" + end end + +def evaluate_response(response) + logger = Logger.new('log/neography.log') + + code = response.code + body = response.body + + case code + when 200 + logger.debug "OK" + when 201 + logger.debug "OK, created" + when 204 + logger.debug "OK, no content returned" + when 400 + logger.error "Invalid data sent" + when 404 + logger.error "#{body}" + end + +end + +require 'httparty' +require 'json' +require 'logger' +#require 'net-http-spy' + +#Net::HTTP.http_logger_options = {:verbose => true} +#Net::HTTP.http_logger_options = {:body => true} + +require 'neography/neo' +require 'neography/node' + + +find_and_require_user_defined_code diff --git a/lib/neography/neo.rb b/lib/neography/neo.rb new file mode 100644 index 0000000..0540bc3 --- /dev/null +++ b/lib/neography/neo.rb @@ -0,0 +1,12 @@ +module Neography + class Neo + include HTTParty + + base_uri 'http://localhost:9999' + + def self.root_node + get('/') + end + + end +end diff --git a/lib/neography/node.rb b/lib/neography/node.rb new file mode 100644 index 0000000..2dfb52e --- /dev/null +++ b/lib/neography/node.rb @@ -0,0 +1,48 @@ +module Neography + class Node + include HTTParty + base_uri 'http://localhost:9999' + format :json + + class << self + + def new(*args) + if args[0].respond_to?(:each_pair) && args[0] + headers 'Content-Type' => 'application/json' + response = post("/node", :body => args[0].to_json) + evaluate_response(response) + response.parsed_response + else + response = post("/node") + evaluate_response(response) + response.parsed_response + end + end + + def get_node(id) + begin + response = get("/node/#{id}") + evaluate_response(response) + response.parsed_response + rescue + nil + end + end + + def properties(id) + get("/node/#{id}/properties") + end + + def set_properties(id, properties) + begin + response = put("/node/#{id}/properties", :body => properties.to_json) + evaluate_response(response) + response.parsed_response + rescue + nil + end + end + + end + end +end diff --git a/neography.gemspec b/neography.gemspec index 8d3dcfd..ec90fbf 100644 --- a/neography.gemspec +++ b/neography.gemspec @@ -6,11 +6,11 @@ Gem::Specification.new do |s| s.name = "neography" s.version = Neography::VERSION s.platform = Gem::Platform::RUBY - s.authors = ["TODO: Write your name"] - s.email = ["TODO: Write your email address"] + s.authors = "Max De Marzi" + s.email = "maxdemarzi@gmail.com" s.homepage = "http://rubygems.org/gems/neography" - s.summary = %q{TODO: Write a gem summary} - s.description = %q{TODO: Write a gem description} + s.summary = "ruby wrapper to Neo4j Rest Interface" + s.description = "A Ruby wrapper to the Neo4j Rest Client Interface see http://components.neo4j.org/neo4j-rest/ for more details." s.rubyforge_project = "neography" @@ -18,4 +18,9 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] + + s.add_development_dependency "rspec", "~> 2.0.0.beta.22" + s.add_development_dependency "net-http-spy", "~> 0.2.1" + s.add_dependency "httparty", "~> 0.6.1" + s.add_dependency "json" end diff --git a/spec/neography_spec.rb b/spec/neography_spec.rb new file mode 100644 index 0000000..966dd3c --- /dev/null +++ b/spec/neography_spec.rb @@ -0,0 +1,50 @@ +require 'neography' + +describe Neography::Neo do +# it "has a root node" do +# Neography::Neo.root_node.should include("reference_node") +# end +end + +describe Neography::Node do + it "can create an empty node" do + Neography::Node.new.should include("data"=>{}) + end + + it "can create a node with one property" do + Neography::Node.new(:name => "Max").should include("data"=>{"name"=>"Max"}) + end + + it "can create a node with more than one property" do + Neography::Node.new(:age => 31, :name => "Max").should include("data"=>{"age"=>31, "name"=>"Max"}) + end + + it "can find a node by its id" do + Neography::Node.get_node(2).should include("self"=>"http://localhost:9999/node/2") + end + + it "fails to find a node that does not exist" do + Neography::Node.get_node(999).should be_nil + end + + it "can get a node's properties" do + Neography::Node.set_properties(3, {:age => 31, :name => "Max"} ).should be_nil + Neography::Node.properties(3).should include("age"=>31, "name"=>"Max") + end + + it "returns nil if a node has no properties" do + Neography::Node.properties(1).should be_nil + end + + it "can set a node's properties" do + Neography::Node.set_properties(2, {:age => 32, :name => "Tom"} ).should be_nil + Neography::Node.properties(2).should include("age"=>32, "name"=>"Tom") + end + + it "returns nil if it fails to set properties on a node that does not exist" do + Neography::Node.set_properties(999,{:age => 33, :name => "Harry"}).should be_nil + end + + + +end \ No newline at end of file