From 8e364069659e28a7518af27b2badd9b956a9c8f6 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Thu, 21 Mar 2013 00:04:59 -0500 Subject: [PATCH] first pass at using httpclient instead of httparty --- lib/neography.rb | 2 +- lib/neography/connection.rb | 26 ++++++++++++----- lib/neography/multi_json_parser.rb | 5 ++-- lib/neography/rest.rb | 1 - lib/neography/tasks.rb | 4 +-- neography.gemspec | 2 +- spec/spec_helper.rb | 4 +-- spec/unit/connection_spec.rb | 47 +++++++++++++++--------------- 8 files changed, 49 insertions(+), 42 deletions(-) diff --git a/lib/neography.rb b/lib/neography.rb index ab74afd..135228c 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -1,5 +1,5 @@ require 'cgi' -require 'httparty' +require 'httpclient' require 'json' require 'multi_json' require 'logger' diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index 088ea94..e663f1d 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -1,6 +1,5 @@ module Neography class Connection - USER_AGENT = "Neography/#{Neography::VERSION}" attr_accessor :protocol, :server, :port, :directory, @@ -8,11 +7,12 @@ class Connection :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) @@ -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) @@ -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 diff --git a/lib/neography/multi_json_parser.rb b/lib/neography/multi_json_parser.rb index 9d8646c..33533c5 100644 --- a/lib/neography/multi_json_parser.rb +++ b/lib/neography/multi_json_parser.rb @@ -1,6 +1,5 @@ -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 @@ -8,7 +7,7 @@ class MultiJsonParser < HTTParty::Parser # 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 diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 5820b48..95e6ce5 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -31,7 +31,6 @@ module Neography class Rest - include HTTParty include Helpers extend Forwardable diff --git a/lib/neography/tasks.rb b/lib/neography/tasks.rb index 5eb48bc..8c02ddd 100644 --- a/lib/neography/tasks.rb +++ b/lib/neography/tasks.rb @@ -1,6 +1,6 @@ # borrowed from architect4r require 'os' -require 'httparty' +require 'httpclient' require 'zip/zip' namespace :neo4j do @@ -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 diff --git a/neography.gemspec b/neography.gemspec index fd01acb..676b1ae 100644 --- a/neography.gemspec +++ b/neography.gemspec @@ -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" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4ca9bb1..c251282 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' diff --git a/spec/unit/connection_spec.rb b/spec/unit/connection_spec.rb index 6b2be11..b78bcab 100644 --- a/spec/unit/connection_spec.rb +++ b/spec/unit/connection_spec.rb @@ -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 @@ -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 @@ -134,7 +133,7 @@ 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 @@ -142,7 +141,7 @@ module Neography 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 @@ -150,7 +149,7 @@ module Neography 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 @@ -158,7 +157,7 @@ module Neography 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 @@ -166,7 +165,7 @@ module Neography 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 @@ -174,7 +173,7 @@ module Neography 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 @@ -182,7 +181,7 @@ module Neography 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 @@ -190,7 +189,7 @@ module Neography 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 @@ -198,7 +197,7 @@ module Neography 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