Skip to content

Commit

Permalink
Allow all HTTParty's options for Neo4j Sessions, #51
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasronge committed Apr 18, 2014
1 parent 80871d0 commit 7507da0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
13 changes: 11 additions & 2 deletions README.md
Expand Up @@ -69,12 +69,19 @@ Open a IRB/Pry session:
session = Neo4j::Session.open(:server_db)
```

Basic Authentication:
### Session Configuration

Example, Basic Authentication:

```ruby
Neo4j::Session.open(:server_db, 'http://my.server', basic_auth: { username: 'username', password: 'password')
Neo4j::Session.open(:server_db, 'http://my.server', basic_auth: { username: 'username', password: 'password'})
```

The last option hash is passed on to HTTParty. See here for more available options:
http://rdoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods

### Embedded Session

Using the Neo4j Embedded Database, `:embedded_db`

```ruby
Expand All @@ -90,6 +97,8 @@ session.running? #=> false
session.close # make the session not current/default
```

### Session.current

When a session has been created it will be stored in the `Neo4j::Session` object.
Example, get the default session

Expand Down
14 changes: 10 additions & 4 deletions lib/neo4j-server/cypher_session.rb
Expand Up @@ -11,11 +11,17 @@ class CypherSession < Neo4j::Session

alias_method :super_query, :query

def self.open(url = nil, params = nil)
endpoint = Neo4jServerEndpoint.new(url, params)

endpoint_url = url || 'http://localhost:7474'
response = endpoint.get(endpoint_url)
# Opens a session to the database
# @see Neo4j::Session#open
#
# @param url - defaults to 'http://localhost:7474' if not given
# @params - see https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb for supported
# HTTParty options
def self.open(endpoint_url=nil, params = {})
endpoint = Neo4jServerEndpoint.new(params)
url = endpoint_url || 'http://localhost:7474'
response = endpoint.get(url)
raise "Server not available on #{url} (response code #{response.code})" unless response.code == 200

root_data = JSON.parse(response.body)
Expand Down
28 changes: 16 additions & 12 deletions lib/neo4j-server/neo4j_server_endpoint.rb
@@ -1,20 +1,24 @@
module Neo4j::Server
class Neo4jServerEndpoint
def initialize(url, params = nil)
unless params.nil?
@auth = params if params[:basic_auth]
end
def initialize(params = {})
@params = params
end

["get", "post", "delete"].each do |verb|
define_method verb do |url, *params|
unless @auth.nil?
params.push({}) if params.empty?
params.last.merge!(@auth)
end
def merged_options(options)
options.merge!(@params)
end

def get(url, options={})
HTTParty.get(url, merged_options(options))
end

HTTParty.send(verb, url, *params)
end
def post(url, options={})
HTTParty.post(url, merged_options(options))
end

def delete(url, options={})
HTTParty.delete(url, merged_options(options))
end

end
end
3 changes: 2 additions & 1 deletion lib/neo4j/session.rb
Expand Up @@ -89,7 +89,8 @@ def _query(*params)
end

class << self
# Creates a new session
# Creates a new session to Neo4j
# @see also Neo4j::Server::CypherSession#open for :server_db params
# @param db_type the type of database, e.g. :embedded_db, or :server_db
def open(db_type, *params)
register(create_session(db_type, params))
Expand Down
12 changes: 6 additions & 6 deletions spec/neo4j-server/unit/cypher_session_unit_spec.rb
Expand Up @@ -4,7 +4,7 @@ module Neo4j::Server
describe CypherSession do

before(:each) do
@endpoint = Neo4jServerEndpoint.new(nil, nil)
@endpoint = Neo4jServerEndpoint.new()
end

let(:cypher_response) do
Expand Down Expand Up @@ -51,7 +51,7 @@ def request
end

it 'allow root resource with urls ending with slash' do
Neo4jServerEndpoint.should_receive(:new).with(nil, nil).and_return(@endpoint)
Neo4jServerEndpoint.should_receive(:new).with({}).and_return(@endpoint)

@endpoint.should_receive(:get).with('http://localhost:7474').and_return(TestResponse.new(root_resource_with_slash))
@endpoint.should_receive(:get).with("http://localhost:7474/db/data/").and_return(TestResponse.new(data_resource))
Expand All @@ -61,7 +61,7 @@ def request
end

it 'allow root resource with urls NOT ending with slash' do
Neo4jServerEndpoint.should_receive(:new).with(nil, nil).and_return(@endpoint)
Neo4jServerEndpoint.should_receive(:new).with({}).and_return(@endpoint)

@endpoint.should_receive(:get).with('http://localhost:7474').and_return(TestResponse.new(root_resource_with_no_slash))
@endpoint.should_receive(:get).with("http://localhost:7474/db/data/").and_return(TestResponse.new(data_resource))
Expand All @@ -75,7 +75,7 @@ def request
auth = {basic_auth: { username: 'username', password: 'password'}}
params = [base_url, auth]

Neo4jServerEndpoint.should_receive(:new).with(*params).and_return(@endpoint)
Neo4jServerEndpoint.should_receive(:new).with(auth).and_return(@endpoint)

@endpoint.should_receive(:get).with(base_url)
.and_return(TestResponse.new(root_resource_with_slash))
Expand All @@ -90,15 +90,15 @@ def request
auth = {basic_auth: { username: 'username', password: 'password'}}
params = [base_url, auth]

Neo4jServerEndpoint.should_receive(:new).with(*params).and_return(@endpoint)
Neo4jServerEndpoint.should_receive(:new).with(auth).and_return(@endpoint)
@endpoint.should_receive(:get).with(base_url)
.and_return(TestResponse.new(root_resource_with_slash))
@endpoint.should_receive(:get).with("http://localhost:7474/db/data/")
.and_return(TestResponse.new(data_resource))

Neo4j::Session.create_session(:server_db, params)

Neo4jServerEndpoint.should_receive(:new).with(nil, nil).and_return(@endpoint)
Neo4jServerEndpoint.should_receive(:new).with({}).and_return(@endpoint)
@endpoint.should_receive(:get).with('http://localhost:7474')
.and_return(TestResponse.new(root_resource_with_no_slash))
@endpoint.should_receive(:get).with("http://localhost:7474/db/data/")
Expand Down
14 changes: 7 additions & 7 deletions spec/neo4j-server/unit/neo4j_server_endpoint_unit_spec.rb
Expand Up @@ -12,18 +12,18 @@ module Neo4j::Server

describe "for requests WITHOUT basic auth" do
before(:each) do
@endpoint = Neo4jServerEndpoint.new(nil, nil)
@endpoint = Neo4jServerEndpoint.new()
end

it "should allow HTTP requests WITHOUT params" do
HTTParty.should_receive(:get).with(url).and_return(response)
HTTParty.should_receive(:get).with(url, {}).and_return(response)
expect(@endpoint.get(url)).to eq(response)

HTTParty.should_receive(:post).with(url).and_return(response)
expect(@endpoint.post(url)).to eq(response)
HTTParty.should_receive(:post).with(url, {}).and_return(response)
expect(@endpoint.post(url, {})).to eq(response)

HTTParty.should_receive(:delete).with(url).and_return(response)
expect(@endpoint.delete(url)).to eq(response)
HTTParty.should_receive(:delete).with(url, {}).and_return(response)
expect(@endpoint.delete(url, {})).to eq(response)
end

it "should allow HTTP requests WITH params" do
Expand All @@ -43,7 +43,7 @@ module Neo4j::Server
describe "for requests with basic auth" do
before(:each) do
@basic_auth = { basic_auth: { username: "U", password: "P"} }
@endpoint = Neo4jServerEndpoint.new(nil, @basic_auth)
@endpoint = Neo4jServerEndpoint.new(@basic_auth)
end

it "should allow HTTP requests WITHOUT other params" do
Expand Down

0 comments on commit 7507da0

Please sign in to comment.