Skip to content

Commit

Permalink
adding authentication options, fixing index tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Feb 4, 2011
1 parent 56f0508 commit 49fe9ac
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 47 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
neography (0.0.6)
httparty (~> 0.6.1)
neography (0.0.7)
httparty (~> 0.7.3)
json

GEM
Expand All @@ -11,7 +11,7 @@ GEM
crack (0.1.8)
diff-lcs (1.1.2)
fakeweb (1.3.0)
httparty (0.6.1)
httparty (0.7.3)
crack (= 0.1.8)
json (1.4.6)
json (1.4.6-java)
Expand All @@ -33,7 +33,7 @@ PLATFORMS

DEPENDENCIES
fakeweb (~> 1.3.0)
httparty (~> 0.6.1)
httparty (~> 0.7.3)
json
neography!
net-http-spy (~> 0.2.1)
Expand Down
27 changes: 19 additions & 8 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ Just add gem 'neography' to your Gemfile and run bundle install

A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and returns JSON or Nil:

# protocol, server, port, log_file, log_enabled, max_threads
@neo = Neography::Rest.new ('http://', '192.168.10.1', 7479, 'log/neography.log', true, 50)

Default Parameters are:

@neo = Neography::Rest.new ('http://', 'localhost', 7474, '/neography.log', false, 20)
@neo = Neography::Rest.new({:protocol => 'http://',
:server => 'localhost',
:port => 7474,
:directory => '', # use '/<my directory>' or leave out for default
:authentication => 'basic', # 'basic', 'digest' or leave out for default
:username => 'your username', #leave out for default
:password => 'your password', #leave out for default
:log_file => 'neography.log',
:log_enabled => false,
:max_threads => 20})

To Use:

Expand Down Expand Up @@ -136,7 +140,7 @@ Trying to mimic the Neo4j.rb API.
Now we are returning full objects. The properties of the node or relationship can be accessed directly (node.name).
The Neo4j ID is available by using node.neo_id .

@neo2 = Neography::Rest.new ('http://', '192.168.10.1')
@neo2 = Neography::Rest.new ({:server => '192.168.10.1'})

Neography::Node.create # Create an empty node
Neography::Node.create("age" => 31, "name" => "Max") # Create a node with some properties
Expand Down Expand Up @@ -228,13 +232,20 @@ See Neo4j API for:

=== Examples

A couple of examples borrowed from Matthew Deiters's Neo4jr-social
A couple of examples borrowed from Matthew Deiters's Neo4jr-social:

* {Facebook}[https://github.com/maxdemarzi/neography/blob/master/examples/facebook.rb]
* {Linked In}[https://github.com/maxdemarzi/neography/blob/master/examples/linkedin.rb]

Phase 2 way of doing these:

* {Facebook}[https://github.com/maxdemarzi/neography/blob/master/examples/facebook_v2.rb]
* {Linked In}[https://github.com/maxdemarzi/neography/blob/master/examples/linkedin_v2.rb]


=== To Do

* Phase 2 Index functionality
* More Tests
* More Examples
* Mixins ?
Expand Down
5 changes: 3 additions & 2 deletions lib/neography/config.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Neography
class Config
class << self; attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger, :max_threads end
class << self; attr_accessor :protocol, :server, :port, :directory, :log_file, :log_enabled, :logger, :max_threads, :authentication end

@protocol = 'http://'
@server = 'localhost'
@port = 7474
@directory = ''
@log_file = 'neography.log'
@log_enabled = false
@logger = Logger.new(@log_file) if @log_enabled
@max_threads = 20

@authentication = {}
end
end
54 changes: 32 additions & 22 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
module Neography
class Rest
include HTTParty
attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger, :max_threads

def initialize(protocol=Neography::Config.protocol,
server=Neography::Config.server,
port=Neography::Config.port,
log_file=Neography::Config.log_file,
log_enabled=Neography::Config.log_enabled,
max_threads=Neography::Config.max_threads)
@protocol = protocol
@server = server
@port = port
@log_file = log_file
@log_enabled = log_enabled
@logger = Logger.new(@log_file) if @log_enabled
@max_threads = max_threads
end

def configure(protocol, server, port)
attr_accessor :protocol, :server, :port, :directory, :log_file, :log_enabled, :logger, :max_threads, :authentication, :username, :password

def initialize(options={})
init = {:protocol => Neography::Config.protocol,
:server => Neography::Config.server,
:port => Neography::Config.port,
:directory => Neography::Config.directory,
:log_file => Neography::Config.log_file,
:log_enabled => Neography::Config.log_enabled,
:max_threads => Neography::Config.max_threads,
:authentication => Neography::Config.authentication}
init.merge!(options)

@protocol = init[:protocol]
@server = init[:server]
@port = init[:port]
@directory = init[:directory]
@log_file = init[:log_file]
@log_enabled = init[:log_enabled]
@logger = Logger.new(@log_file) if @log_enabled
@max_threads = init[:max_threads]
@authentication = Hash.new
@authentication = {"#{init[:authentication]}_auth".to_sym => {:username => init[:username], :password => init[:password]}} unless init[:authentication].empty?
end

def configure(protocol, server, port, directory)
@protocol = protocol
@server = server
@port = port
@directory = directory
end

def configuration
@protocol + @server + ':' + @port.to_s + "/db/data"
@protocol + @server + ':' + @port.to_s + @directory + "/db/data"
end

def get_root
Expand Down Expand Up @@ -277,19 +287,19 @@ def evaluate_response(response)
end

def get(path,options={})
evaluate_response(HTTParty.get(configuration + path, options))
evaluate_response(HTTParty.get(configuration + path, options.merge!(@authentication)))
end

def post(path,options={})
evaluate_response(HTTParty.post(configuration + path, options))
evaluate_response(HTTParty.post(configuration + path, options.merge!(@authentication)))
end

def put(path,options={})
evaluate_response(HTTParty.put(configuration + path, options))
evaluate_response(HTTParty.put(configuration + path, options.merge!(@authentication)))
end

def delete(path,options={})
evaluate_response(HTTParty.delete(configuration + path, options))
evaluate_response(HTTParty.delete(configuration + path, options.merge!(@authentication)))
end

def get_id(id)
Expand Down
2 changes: 1 addition & 1 deletion neography.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec", "~> 2.0.0.beta.22"
s.add_development_dependency "net-http-spy", "~> 0.2.1"
s.add_development_dependency "fakeweb", "~> 1.3.0"
s.add_dependency "httparty", "~> 0.6.1"
s.add_dependency "httparty", "~> 0.7.3"
s.add_dependency "json"
end
21 changes: 21 additions & 0 deletions spec/integration/heroku_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Neography::Rest do
before(:each) do
@neo = Neography::Rest.new({:server => '4c36b641.neo4j.atns.de', :port => 7474, :directory => '/9dc1fda5be8b5cde29621e21cae5adece3de0f37', :authentication => 'basic', :username => "abbe3c012", :password => "34d7b22eb"})
end

describe "get_root" do
it "can get the root node" do
root_node = @neo.get_root
root_node.should have_key("reference_node")
end
end

describe "create_node" do
it "can create an empty node" do
new_node = @neo.create_node
new_node.should_not be_nil
end
end
end
24 changes: 14 additions & 10 deletions spec/integration/rest_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

describe "list indexes" do
it "can get a listing of indexes" do
new_node = @neo.create_node
key = generate_text(6)
value = generate_text
@neo.add_to_index("test_index", key, value, new_node)
@neo.list_indexes.should_not be_nil
end
end
Expand All @@ -16,10 +20,10 @@
new_node = @neo.create_node
key = generate_text(6)
value = generate_text
@neo.add_to_index(key, value, new_node)
new_index = @neo.get_index(key, value)
@neo.add_to_index("test_index", key, value, new_node)
new_index = @neo.get_index("test_index", key, value)
new_index.should_not be_nil
@neo.remove_from_index(key, value, new_node)
@neo.remove_from_index("test_index", key, value, new_node)
end
end

Expand All @@ -28,11 +32,11 @@
new_node = @neo.create_node
key = generate_text(6)
value = generate_text
@neo.add_to_index(key, value, new_node)
new_index = @neo.get_index(key, value)
@neo.add_to_index("test_index", key, value, new_node)
new_index = @neo.get_index("test_index", key, value)
new_index.should_not be_nil
@neo.remove_from_index(key, value, new_node)
new_index = @neo.get_index(key, value)
@neo.remove_from_index("test_index", key, value, new_node)
new_index = @neo.get_index("test_index", key, value)
new_index.should be_nil
end
end
Expand All @@ -42,10 +46,10 @@
new_node = @neo.create_node
key = generate_text(6)
value = generate_text
@neo.add_to_index(key, value, new_node)
new_index = @neo.get_index(key, value)
@neo.add_to_index("test_index", key, value, new_node)
new_index = @neo.get_index("test_index", key, value)
new_index.should_not be_nil
@neo.remove_from_index(key, value, new_node)
@neo.remove_from_index("test_index", key, value, new_node)
end
end

Expand Down

0 comments on commit 49fe9ac

Please sign in to comment.