Skip to content

Commit

Permalink
Add path helpers as class methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Dijk committed Sep 20, 2012
1 parent 23e4522 commit 2dd4b16
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class Batch
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :batch, "/batch"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/clean.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class Clean
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :clean, "/cleandb/secret-key"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_auto_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeAutoIndexes < AutoIndexes
include Neography::Rest::Paths
extend Neography::Rest::Paths

add_path :key_value, "/index/auto/node/:key/:value"
add_path :query_index, "/index/auto/node/?query=:query"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeIndexes < Indexes
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :all, "/index/node"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_paths.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodePaths
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :base, "/node/:id/path"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_properties.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeProperties < Properties
include Neography::Rest::Paths
extend Neography::Rest::Paths

add_path :all, "/node/:id/properties"
add_path :single, "/node/:id/properties/:property"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_relationships.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeRelationships
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :base, "/node/:id/relationships"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_traversal.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeTraversal
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :traversal, "/node/:id/traverse/:type"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/nodes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class Nodes
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :index, "/node"
Expand Down
31 changes: 17 additions & 14 deletions lib/neography/rest/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ module Neography
class Rest
module Paths

def self.included(mod)
mod.send :extend, ClassMethods
def add_path(key, path)
method_name = :"#{key}_path"

metaclass = (class << self; self; end)
metaclass.instance_eval do
define_method method_name do |*attributes|
if attributes.any?
build_path(path, *attributes)
else
path
end
end
end

define_method method_name do |*attributes|
self.class.send(method_name, *attributes)
end
end

def build_path(path, attributes)
Expand All @@ -16,18 +31,6 @@ def encode(value)
URI.encode(value).gsub("/","%2F")
end

module ClassMethods
def add_path(key, path)
define_method :"#{key}_path" do |*attributes|
if attributes.any?
build_path(path, *attributes)
else
path
end
end
end
end

end
end
end
2 changes: 1 addition & 1 deletion lib/neography/rest/relationship_auto_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class RelationshipAutoIndexes < AutoIndexes
include Neography::Rest::Paths
extend Neography::Rest::Paths

add_path :key_value, "/index/auto/relationship/:key/:value"
add_path :query_index, "/index/auto/relationship/?query=:query"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/relationship_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class RelationshipIndexes < Indexes
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :all, "/index/relationship"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/relationship_properties.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class RelationshipProperties < Properties
include Neography::Rest::Paths
extend Neography::Rest::Paths

add_path :all, "/relationship/:id/properties"
add_path :single, "/relationship/:id/properties/:property"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/relationships.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class Relationships
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :base, "/relationship/:id"
Expand Down
58 changes: 44 additions & 14 deletions spec/unit/rest/paths_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,62 @@ module Neography
class Rest

class Dummy
include Paths
extend Paths

add_path :one, "/node/:id"
add_path :two, "/node/:id/properties/:property"
end

describe Dummy do

it { should respond_to(:one_path) }
it { should respond_to(:two_path) }
context "instance methods" do

it "replaces a key" do
subject.one_path(:id => 42).should == "/node/42"
end
it { should respond_to(:one_path) }
it { should respond_to(:two_path) }

it "replaces multiple keys" do
subject.two_path(:id => 42, :property => "foo").should == "/node/42/properties/foo"
end
it "replaces a key" do
subject.one_path(:id => 42).should == "/node/42"
end

it "replaces multiple keys" do
subject.two_path(:id => 42, :property => "foo").should == "/node/42/properties/foo"
end

it "url encodes spaces" do
subject.one_path(:id => "with space").should == "/node/with%20space"
end

# URI.encode does not escape slashes (and rightly so), but should escape these keys
it "url encodes slashes" do
subject.one_path(:id => "with/slash").should == "/node/with%2Fslash"
end

it "url encodes spaces" do
subject.one_path(:id => "with space").should == "/node/with%20space"
end

# URI.encode does not escape slashes (and rightly so), but should escape these keys
it "url encodes slashes" do
subject.one_path(:id => "with/slash").should == "/node/with%2Fslash"
context "class methods" do

subject { Dummy }

it { should respond_to(:one_path) }
it { should respond_to(:two_path) }

it "replaces a key" do
subject.one_path(:id => 42).should == "/node/42"
end

it "replaces multiple keys" do
subject.two_path(:id => 42, :property => "foo").should == "/node/42/properties/foo"
end

it "url encodes spaces" do
subject.one_path(:id => "with space").should == "/node/with%20space"
end

# URI.encode does not escape slashes (and rightly so), but should escape these keys
it "url encodes slashes" do
subject.one_path(:id => "with/slash").should == "/node/with%2Fslash"
end

end

end
Expand Down

0 comments on commit 2dd4b16

Please sign in to comment.