Skip to content

Commit

Permalink
first pass at using httpclient instead of httparty
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 21, 2013
1 parent 4f53d97 commit 8e36406
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/neography.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'cgi'
require 'httparty'
require 'httpclient'
require 'json'
require 'multi_json'
require 'logger'
Expand Down
26 changes: 18 additions & 8 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module Neography
class Connection

USER_AGENT = "Neography/#{Neography::VERSION}"

attr_accessor :protocol, :server, :port, :directory,
:cypher_path, :gremlin_path,
:log_file, :log_enabled, :logger,
:max_threads,
:authentication, :username, :password,
:parser
:parser, :client

def initialize(options = ENV['NEO4J_URL'] || {})
config = merge_configuration(options)
save_local_configuration(config)
@client = HTTPClient.new
end

def configure(protocol, server, port, directory)
Expand All @@ -33,21 +33,31 @@ def merge_options(options)
end

def get(path, options={})
evaluate_response(HTTParty.get(configuration + path, merge_options(options)))
authenticate(configuration + path)
evaluate_response(@client.get(configuration + path, merge_options(options)[:body], merge_options(options)[:headers]))
end

def post(path, options={})
evaluate_response(HTTParty.post(configuration + path, merge_options(options)))
authenticate(configuration + path)
evaluate_response(@client.post(configuration + path, merge_options(options)[:body], merge_options(options)[:headers]))
end

def put(path, options={})
evaluate_response(HTTParty.put(configuration + path, merge_options(options)))
authenticate(configuration + path)
evaluate_response(@client.put(configuration + path, merge_options(options)[:body], merge_options(options)[:headers]))
end

def delete(path, options={})
evaluate_response(HTTParty.delete(configuration + path, merge_options(options)))
authenticate(configuration + path)
evaluate_response(@client.delete(configuration + path, merge_options(options)[:body], merge_options(options)[:headers]))
end

def authenticate(path)
@client.set_auth(path,
@authentication[@authentication.keys.first][:username],
@authentication[@authentication.keys.first][:password]) unless @authentication.empty?
end

private

def merge_configuration(options)
Expand Down Expand Up @@ -91,10 +101,10 @@ def evaluate_response(response)
case code
when 200
@logger.debug "OK" if @log_enabled
response.parsed_response
MultiJsonParser.json(body) #response.parsed_response
when 201
@logger.debug "OK, created #{body}" if @log_enabled
response.parsed_response
MultiJsonParser.json(body) #response.parsed_response
when 204
@logger.debug "OK, no content returned" if @log_enabled
nil
Expand Down
5 changes: 2 additions & 3 deletions lib/neography/multi_json_parser.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class MultiJsonParser < HTTParty::Parser
class MultiJsonParser

protected

# I know this looks pretty ugly, but we have issues with Neo4j returning true, false,
# plain numbers and plain strings, which is considered by some JSON libraries to be
# invalid JSON, but some consider it perfectly fine.
# This ugly hack deals with the problem. Send me a Pull Request if you
# come up with a nicer solution... please!
#
def json
def self.json(body)
begin
MultiJson.load(body)
rescue MultiJson::DecodeError, ArgumentError
Expand Down
1 change: 0 additions & 1 deletion lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
module Neography

class Rest
include HTTParty
include Helpers
extend Forwardable

Expand Down
4 changes: 2 additions & 2 deletions lib/neography/tasks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# borrowed from architect4r
require 'os'
require 'httparty'
require 'httpclient'
require 'zip/zip'

namespace :neo4j do
Expand All @@ -14,7 +14,7 @@
unless File.exist?('neo4j.zip')
df = File.open('neo4j.zip', 'wb')
begin
df << HTTParty.get("http://dist.neo4j.org/neo4j-#{args[:edition]}-#{args[:version]}-windows.zip")
df << HTTPClient.new.get("http://dist.neo4j.org/neo4j-#{args[:edition]}-#{args[:version]}-windows.zip")
ensure
df.close()
end
Expand Down
2 changes: 1 addition & 1 deletion neography.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "net-http-spy", "0.2.1"
s.add_development_dependency "rake", ">= 0.8.7"
s.add_development_dependency "coveralls"
s.add_dependency "httparty", ">= 0.8.1"
s.add_dependency "httpclient", ">= 2.3.3"
s.add_dependency "rake", ">= 0.8.7"
s.add_dependency "json", ">= 1.6.0"
s.add_dependency "os", ">= 0.9.6"
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
Coveralls.wear!

# If you want to see more, uncomment the next few lines
# require 'net-http-spy'
require 'net-http-spy'
# Net::HTTP.http_logger_options = {:body => true} # just the body
# Net::HTTP.http_logger_options = {:verbose => true} # see everything
Net::HTTP.http_logger_options = {:verbose => true} # see everything

def generate_text(length=8)
chars = 'abcdefghjkmnpqrstuvwxyz'
Expand Down
47 changes: 23 additions & 24 deletions spec/unit/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ module Neography
context "requests" do

it "does a GET request" do
HTTParty.should_receive(:get).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object }
connection.client.should_receive(:get).with("http://localhost:7474/db/data/foo/bar", nil, nil) { stub.as_null_object }
connection.get("/foo/bar")
end

it "does a POST request" do
HTTParty.should_receive(:post).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object }
connection.client.should_receive(:post).with("http://localhost:7474/db/data/foo/bar", nil, nil) { stub.as_null_object }
connection.post("/foo/bar")
end

it "does a PUT request" do
HTTParty.should_receive(:put).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object }
connection.client.should_receive(:put).with("http://localhost:7474/db/data/foo/bar", nil, nil) { stub.as_null_object }
connection.put("/foo/bar")
end

it "does a DELETE request" do
HTTParty.should_receive(:delete).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object }
connection.client.should_receive(:delete).with("http://localhost:7474/db/data/foo/bar", nil, nil) { stub.as_null_object }
connection.delete("/foo/bar")
end

Expand All @@ -107,25 +107,24 @@ module Neography
end

it "does requests with authentication" do
HTTParty.should_receive(:get).with(
connection.client.should_receive(:set_auth).with(
"http://localhost:7474/db/data/foo/bar",
{ :parser => MultiJsonParser,
:basic_auth => {
:username => "foo",
:password => "bar"
}
}) { stub.as_null_object }
"foo",
"bar") { stub.as_null_object }

connection.client.should_receive(:get).with(
"http://localhost:7474/db/data/foo/bar", nil, nil
) { stub.as_null_object }

connection.get("/foo/bar")
end
end

it "adds the User-Agent to the headers" do
HTTParty.should_receive(:get).with(
connection.client.should_receive(:get).with(
"http://localhost:7474/db/data/foo/bar",
{ :parser => MultiJsonParser,
:headers => { "User-Agent" => "Neography/#{Neography::VERSION}" }
}) { stub.as_null_object }
nil, { "User-Agent" => "Neography/#{Neography::VERSION}" }
) { stub.as_null_object }

connection.get("/foo/bar", :headers => {})
end
Expand All @@ -134,71 +133,71 @@ module Neography

it "raises NodeNotFoundException" do
response = error_response(code: 404, message: "a message", exception: "NodeNotFoundException")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error NodeNotFoundException
end

it "raises OperationFailureException" do
response = error_response(code: 409, message: "a message", exception: "OperationFailureException")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error OperationFailureException
end

it "raises PropertyValueException" do
response = error_response(code: 400, message: "a message", exception: "PropertyValueException")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error PropertyValueException
end

it "raises NoSuchPropertyException" do
response = error_response(code: 404, message: "a message", exception: "NoSuchPropertyException")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error NoSuchPropertyException
end

it "raises RelationshipNotFoundException" do
response = error_response(code: 404, message: "a message", exception: "RelationshipNotFoundException")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error RelationshipNotFoundException
end

it "raises BadInputException" do
response = error_response(code: 400, message: "a message", exception: "BadInputException")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error BadInputException
end

it "raises UnauthorizedError" do
response = error_response(code: 401)
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error UnauthorizedError
end

it "raises NeographyError in all other cases" do
response = error_response(code: 418, message: "I'm a teapot.")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error NeographyError
end

it "raises BadInputException" do
response = error_response(code: 500, message: "a message", exception: "JsonParseException")
HTTParty.stub(:get).and_return(response)
connection.client.stub(:get).and_return(response)
expect {
connection.get("/foo/bar")
}.to raise_error NeographyError
Expand Down

0 comments on commit 8e36406

Please sign in to comment.