Skip to content

Commit

Permalink
adding indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 18, 2010
1 parent 2b93d8e commit 2bedc62
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and
Neography::Rest.remove_relationship_properties(id, "since") # Remove one property of a relationship
Neography::Rest.remove_relationship_properties(id, ["since","met"]) # Remove multiple properties of a relationship

Neography::Rest.list_indexes # doesn't really seam to do what the api says it does
Neography::Rest.add_to_index(key, value, id) # adds a node to an index with the given key/value pair
Neography::Rest.remove_from_index(key, value, id) # removes a node to an index with the given key/value pair
Neography::Rest.get_index(key, value) # gets an index with the given key/value pair

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

Neography::Node.new # Create an empty node
Expand All @@ -72,8 +77,8 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and
Neography::Node.del(3) # Deletes a node without any relationships
Neography::Node.del!(3) # Deletes a node and all its relationships


=== License

* Neography - MIT, see the LICENSE file http://github.com/maxdemarzi/neography/tree/master/LICENSE.
* Lucene - Apache, see http://lucene.apache.org/java/docs/features.html
* Neo4j - Dual free software/commercial license, see http://neo4j.org/
Expand Down
19 changes: 19 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ def delete_node!(id)
rescue_ij { delete("/node/#{id}") }
end

def list_indexes
rescue_ij { get("/index") }
end

def add_to_index(key, value, id)
options = { :body => (Neography::Config.to_s + "/node/#{id}").to_json, :headers => {'Content-Type' => 'application/json'} }
rescue_ij { post("/index/node/#{key}/#{value}", options) }
end

def remove_from_index(key, value, id)
rescue_ij { delete("/index/node/#{key}/#{value}/#{id}") }
end

def get_index(key, value)
index = rescue_ij { get("/index/node/#{key}/#{value}") } || Array.new
return nil if index.empty?
index
end

private

# Rescue from Invalid JSON error thrown by Crack Gem
Expand Down
48 changes: 48 additions & 0 deletions spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -599,4 +599,52 @@
end
end

describe "list indexes" do
it "can get a listing of indexes" do
Neography::Rest.list_indexes.should_not be_nil
end
end

describe "add to index" do
it "can add a node to an index" do
new_node = Neography::Rest.create_node
new_node[:id] = new_node["self"].split('/').last
key = generate_text(6)
value = generate_text
Neography::Rest.add_to_index(key, value, new_node[:id])
new_index = Neography::Rest.get_index(key, value)
new_index.should_not be_nil
Neography::Rest.remove_from_index(key, value, new_node[:id])
end
end

describe "remove from index" do
it "can remove a node from an index" do
new_node = Neography::Rest.create_node
new_node[:id] = new_node["self"].split('/').last
key = generate_text(6)
value = generate_text
Neography::Rest.add_to_index(key, value, new_node[:id])
new_index = Neography::Rest.get_index(key, value)
new_index.should_not be_nil
Neography::Rest.remove_from_index(key, value, new_node[:id])
new_index = Neography::Rest.get_index(key, value)
new_index.should be_nil
end
end

describe "get index" do
it "can get an index" do
new_node = Neography::Rest.create_node
new_node[:id] = new_node["self"].split('/').last
key = generate_text(6)
value = generate_text
Neography::Rest.add_to_index(key, value, new_node[:id])
new_index = Neography::Rest.get_index(key, value)
new_index.should_not be_nil
Neography::Rest.remove_from_index(key, value, new_node[:id])
end
end


end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@
FakeWeb.clean_registry
FakeWeb.allow_net_connect = true

def generate_text(length=8)
chars = 'abcdefghjkmnpqrstuvwxyz'
key = ''
length.times { |i| key << chars[rand(chars.length)] }
key
end

0 comments on commit 2bedc62

Please sign in to comment.