Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major refactor of Rest class #58

Merged
merged 22 commits into from
Sep 8, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/neography.rb
Original file line number Diff line number Diff line change
@@ -30,8 +30,9 @@ def find_and_require_user_defined_code

require 'neography/config'

require 'neography/rest_helpers'
require 'neography/rest_paths'
require 'neography/rest/helpers'
require 'neography/rest/paths'

require 'neography/rest/nodes'
require 'neography/rest/node_properties'
require 'neography/rest/node_relationships'
8 changes: 4 additions & 4 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
@@ -76,19 +76,19 @@ def merge_options(options)
end

def get(path, options={})
evaluate_response(HTTParty.get(configuration + URI.encode(path), merge_options(options)))
evaluate_response(HTTParty.get(configuration + path, merge_options(options)))
end

def post(path, options={})
evaluate_response(HTTParty.post(configuration + URI.encode(path), merge_options(options)))
evaluate_response(HTTParty.post(configuration + path, merge_options(options)))
end

def put(path, options={})
evaluate_response(HTTParty.put(configuration + URI.encode(path), merge_options(options)))
evaluate_response(HTTParty.put(configuration + path, merge_options(options)))
end

def delete(path, options={})
evaluate_response(HTTParty.delete(configuration + URI.encode(path), merge_options(options)))
evaluate_response(HTTParty.delete(configuration + path, merge_options(options)))
end

def evaluate_response(response)
File renamed without changes.
6 changes: 1 addition & 5 deletions lib/neography/rest/node_indexes.rb
Original file line number Diff line number Diff line change
@@ -13,10 +13,6 @@ class NodeIndexes
add_path :key_value, "/index/node/:index/:key/:value"
add_path :query, "/index/node/:index?query=:query"

# this should be the same as the :key_value path above
# was changed to ?query="..." to fix bug with spaces and slashes
add_path :key_value2, "/index/node/:index/:key?query=\":value\""

def initialize(connection)
@connection = connection
end
@@ -78,7 +74,7 @@ def get(index, key, value)

# TODO FIX BUG %20
def find_by_value(index, key, value)
@connection.get(key_value2_path(:index => index, :key => key, :value => value)) || []
@connection.get(key_value_path(:index => index, :key => key, :value => value)) || []
end

def find_by_query(index, query)
6 changes: 5 additions & 1 deletion lib/neography/rest_paths.rb → lib/neography/rest/paths.rb
Original file line number Diff line number Diff line change
@@ -8,10 +8,14 @@ def self.included(mod)

def build_path(path, attributes)
path.gsub(/:([\w_]*)/) do
attributes[$1.to_sym].to_s
encode(attributes[$1.to_sym].to_s)
end
end

def encode(value)
URI.encode(value).gsub("/","%2F")
end

module ClassMethods
def add_path(key, path)
define_method :"#{key}_path" do |*attributes|
2 changes: 1 addition & 1 deletion spec/integration/rest_index_spec.rb
Original file line number Diff line number Diff line change
@@ -190,7 +190,7 @@
@neo.remove_node_from_index("test_node_index", key, value, new_node)
end

xit "can get a node index with a slash in the value" do
it "can get a node index with a slash in the value" do
new_node = @neo.create_node
key = generate_text(6)
value = generate_text + "/" + generate_text
2 changes: 1 addition & 1 deletion spec/unit/rest/node_indexes_spec.rb
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ class Rest
end

it "finds by key and value" do
connection.should_receive(:get).with("/index/node/some_index/some_key?query=\"some_value\"")
connection.should_receive(:get).with("/index/node/some_index/some_key/some_value")
subject.find_by_value("some_index", "some_key", "some_value")
end

39 changes: 39 additions & 0 deletions spec/unit/rest/paths_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

module Neography
class Rest

class Dummy
include 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) }

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
end