From c88b20abc7ab99812a1296b23855c9b894851280 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Sat, 7 Jan 2012 02:04:35 +0000 Subject: [PATCH] adding parameters to cypher calls --- Gemfile.lock | 3 +-- README.rdoc | 6 +++++- lib/neography/rest.rb | 7 ++++++- neography.gemspec | 4 ++-- spec/integration/rest_plugin_spec.rb | 28 ++++++++++++++++++++++++---- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8f478aa..e42f8fb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - neography (0.0.17) + neography (0.0.19) httparty (= 0.7.8) json os @@ -15,7 +15,6 @@ GEM httparty (0.7.8) crack (= 0.1.8) json (1.6.4) - json (1.6.4-java) net-http-spy (0.2.1) os (0.9.5) rake (0.8.7) diff --git a/README.rdoc b/README.rdoc index f2cfcbd..0208327 100644 --- a/README.rdoc +++ b/README.rdoc @@ -30,6 +30,8 @@ in order to access the functionality. === Dependencies for use: + os + rake json httparty @@ -133,8 +135,10 @@ To Use: @neo.get_relationship_index(index, key, value) # exact query of the relationship index with the given key/value pair @neo.find_relationship_index(index, key, value) # advanced query of the relationship index with the given key/value pair @neo.find_relationship_index(index, query) # advanced query of the relationship index with the given query - @neo.execute_script("g.v(0)") # sends a Groovy script(through the Gremlin plugin) + @neo.execute_script("g.v(0)") # sends a Groovy script (through the Gremlin plugin) + @neo.execute_script("g.v(id)", {:id => 3}) # sends a parameterized Groovy script (optimized for repeated calls) @neo.execute_query("start n=node(0) return n") # sends a Cypher query (through the Cypher plugin) + @neo.execute_query("start n=node(id) return n", {:id => 3}) # sends a parameterized Cypher query (optimized for repeated calls) @neo.get_path(node1, node2, relationships, depth=4, algorithm="shortestPath") # finds the shortest path between two nodes @neo.get_paths(node1, node2, relationships, depth=3, algorithm="allPaths") # finds all paths between two nodes diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index d593366..b6e8cc5 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -341,10 +341,15 @@ def get_paths(from, to, relationships, depth=1, algorithm="allPaths") paths = post("/node/#{get_id(from)}/paths", options) || Array.new end - def execute_query(query) + def execute_query_old(query) options = { :body => {:query => query}.to_json, :headers => {'Content-Type' => 'application/json'} } result = post("/ext/CypherPlugin/graphdb/execute_query", options) end + + def execute_query(query, params = {}) + options = { :body => {:query => query, :params => params}.to_json, :headers => {'Content-Type' => 'application/json'} } + result = post("/ext/CypherPlugin/graphdb/execute_query", options) + end def execute_script(script, params = {}) options = { :body => {:script => script, :params => params}.to_json , :headers => {'Content-Type' => 'application/json'} } diff --git a/neography.gemspec b/neography.gemspec index 6652c13..5891cbc 100644 --- a/neography.gemspec +++ b/neography.gemspec @@ -21,8 +21,8 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec" s.add_development_dependency "net-http-spy", "0.2.1" - s.add_development_dependency "rake", "~> 0.8.7" - s.add_dependency "httparty", "0.7.8" + s.add_development "rake", ">= 0.8.7" + s.add_dependency "httparty", "~> 0.7.8" s.add_dependency "json" s.add_dependency "os" s.add_dependency "rubyzip" diff --git a/spec/integration/rest_plugin_spec.rb b/spec/integration/rest_plugin_spec.rb index 2254256..ec75df7 100644 --- a/spec/integration/rest_plugin_spec.rb +++ b/spec/integration/rest_plugin_spec.rb @@ -24,14 +24,11 @@ it "can get the a node with a variable" do new_node = @neo.create_node id = new_node["self"].split('/').last - existing_node = @neo.execute_script("g.v(id)", {:id => id}) + existing_node = @neo.execute_script("g.v(id)", {:id => id.to_i}) existing_node.should_not be_nil existing_node.should have_key("self") existing_node["self"].split('/').last.should == id end - - - end describe "execute cypher query" do @@ -42,6 +39,29 @@ root_node["data"][0][0].should have_key("self") root_node["data"][0][0]["self"].split('/').last.should == "0" end + + it "can get the a node" do + new_node = @neo.create_node + id = new_node["self"].split('/').last + existing_node = @neo.execute_query("start n=node(#{id}) return n") + existing_node.should_not be_nil + existing_node.should have_key("data") + existing_node.should have_key("columns") + existing_node["data"][0][0].should have_key("self") + existing_node["data"][0][0]["self"].split('/').last.should == id + end + + it "can get the a node with a variable" do + new_node = @neo.create_node + id = new_node["self"].split('/').last + existing_node = @neo.execute_query("start n=node({id}) return n", {:id => id.to_i}) + existing_node.should_not be_nil + existing_node.should have_key("data") + existing_node.should have_key("columns") + existing_node["data"][0][0].should have_key("self") + existing_node["data"][0][0]["self"].split('/').last.should == id + end + end end \ No newline at end of file