Skip to content

Commit

Permalink
Fix som duplicate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Dijk committed Sep 21, 2012
1 parent 9725bb8 commit 798d174
Showing 1 changed file with 100 additions and 87 deletions.
187 changes: 100 additions & 87 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ def get_batch(args)
end
end

# Nodes

def get_node(id)
{
:method => "GET",
:to => Nodes.base_path(:id => get_id(id))
}
get(Nodes, id)
end

def delete_node(id)
delete(Nodes, id)
end

def create_node(body)
Expand All @@ -56,12 +59,7 @@ def create_node(body)
}
end

def delete_node(id)
{
:method => "DELETE",
:to => Nodes.base_path(:id => get_id(id))
}
end
# NodeIndexes

def create_unique_node(index, key, value, properties)
{
Expand All @@ -75,48 +73,23 @@ def create_unique_node(index, key, value, properties)
}
end

def add_node_to_index(index, key, value, to)
{
:method => "POST",
:to => NodeIndexes.base_path(:index => index),
:body => {
:uri => build_node_uri(to),
:key => key,
:value => value
}
}
def add_node_to_index(index, key, value, id)
uri = build_node_uri(id)
add_to_index(NodeIndexes, index, key, value, uri)
end

def get_node_index(index, key, value)
{
:method => "GET",
:to => NodeIndexes.key_value_path(:index => index, :key => key, :value => value)
}
get_from_index(NodeIndexes, index, key, value)
end

def remove_node_from_index(index, key_or_id, value_or_id = nil, id = nil)
{
:method => "DELETE",
:to => remove_node_from_index_path(index, key_or_id, value_or_id, id)
}
remove_from_index(NodeIndexes, index, key_or_id, value_or_id, id)
end

def remove_node_from_index_path(index, key_or_id, value_or_id = nil, id = nil)
if id
NodeIndexes.value_path(:index => index, :key => key_or_id, :value => value_or_id, :id => get_id(id))
elsif value_or_id
NodeIndexes.key_path(:index => index, :key => key_or_id, :id => get_id(value_or_id))
else
NodeIndexes.id_path(:index => index, :id => get_id(key_or_id))
end
end
# NodeProperties

def set_node_property(id, property)
{
:method => "PUT",
:to => NodeProperties.single_path(:id => get_id(id), :property => property.keys.first),
:body => property.values.first
}
set_property(NodeProperties, id, property)
end

def reset_node_properties(id, body)
Expand All @@ -127,18 +100,23 @@ def reset_node_properties(id, body)
}
end

# NodeRelationships

def get_node_relationships(id, direction = nil)
{
:method => "GET",
:to => NodeRelationships.direction_path(:id => get_id(id), :direction => direction || 'all'),
}
end

# Relationships

def get_relationship(id)
{
:method => "GET",
:to => Relationships.base_path(:id => get_id(id))
}
get(Relationships, id)
end

def delete_relationship(id)
delete(Relationships, id)
end

def create_relationship(type, from, to, data)
Expand All @@ -153,12 +131,7 @@ def create_relationship(type, from, to, data)
}
end

def delete_relationship(id)
{
:method => "DELETE",
:to => Relationships.base_path(:id =>get_id(id))
}
end
# RelationshipIndexes

def create_unique_relationship(index, key, value, type, from, to)
{
Expand All @@ -175,30 +148,22 @@ def create_unique_relationship(index, key, value, type, from, to)
end

def add_relationship_to_index(index, key, value, id)
{
:method => "POST",
:to => RelationshipIndexes.base_path(:index => index),
:body => {
:uri => build_relationship_uri(id),
:key => key,
:value => value
}
}
uri = build_relationship_uri(id)
add_to_index(RelationshipIndexes, index, key, value, uri)
end

def get_relationship_index(index, key, value)
{
:method => "GET",
:to => RelationshipIndexes.key_value_path(:index => index, :key => key, :value => value)
}
get_from_index(RelationshipIndexes, index, key, value)
end

def remove_relationship_from_index(index, key_or_id, value_or_id = nil, id = nil)
remove_from_index(RelationshipIndexes, index, key_or_id, value_or_id, id)
end

# RelationshipProperties

def set_relationship_property(id, property)
{
:method => "PUT",
:to => RelationshipProperties.single_path(:id => get_id(id), :property => property.keys.first),
:body => property.values.first
}
set_property(RelationshipProperties, id, property)
end

def reset_relationship_properties(id, body)
Expand All @@ -209,23 +174,7 @@ def reset_relationship_properties(id, body)
}
end

def remove_relationship_from_index(index, key_or_id, value_or_id = nil, id = nil)

{
:method => "DELETE",
:to => remove_relationship_from_index_path(index, key_or_id, value_or_id, id)
}
end

def remove_relationship_from_index_path(index, key_or_id, value_or_id = nil, id = nil)
if id
RelationshipIndexes.value_path(:index => index, :key => key_or_id, :value => value_or_id, :id => get_id(id))
elsif value_or_id
RelationshipIndexes.key_path(:index => index, :key => key_or_id, :id => get_id(value_or_id))
else
RelationshipIndexes.id_path(:index => index, :id => get_id(key_or_id))
end
end
# Cypher

def execute_query(query, params = nil)
request = {
Expand All @@ -241,6 +190,8 @@ def execute_query(query, params = nil)
request
end

# Gremlin

def execute_script(script, params = nil)
{
:method => "POST",
Expand All @@ -252,6 +203,68 @@ def execute_script(script, params = nil)
}
end

# Similar between nodes and relationships

def get(klass, id)
{
:method => "GET",
:to => klass.base_path(:id => get_id(id))
}
end

def delete(klass, id)
{
:method => "DELETE",
:to => klass.base_path(:id => get_id(id))
}
end

def add_to_index(klass, index, key, value, uri)
{
:method => "POST",
:to => klass.base_path(:index => index),
:body => {
:uri => uri,
:key => key,
:value => value
}
}
end

def get_from_index(klass, index, key, value)
{
:method => "GET",
:to => klass.key_value_path(:index => index, :key => key, :value => value)
}
end

def remove_from_index(klass, index, key_or_id, value_or_id = nil, id = nil)
{
:method => "DELETE",
:to => remove_from_index_path(klass, index, key_or_id, value_or_id, id)
}
end

def remove_from_index_path(klass, index, key_or_id, value_or_id = nil, id = nil)
if id
klass.value_path(:index => index, :key => key_or_id, :value => value_or_id, :id => get_id(id))
elsif value_or_id
klass.key_path(:index => index, :key => key_or_id, :id => get_id(value_or_id))
else
klass.id_path(:index => index, :id => get_id(key_or_id))
end
end

def set_property(klass, id, property)
{
:method => "PUT",
:to => klass.single_path(:id => get_id(id), :property => property.keys.first),
:body => property.values.first
}
end

# Helper methods

def build_node_uri(value)
build_uri(value, "node")
end
Expand Down

0 comments on commit 798d174

Please sign in to comment.