From a871d00fb9fbe1b64f6c09d024a1e31133319453 Mon Sep 17 00:00:00 2001 From: Julian Simpson Date: Tue, 14 Aug 2012 18:01:13 +0100 Subject: [PATCH 1/3] Add User-Agent header. --- lib/neography.rb | 2 ++ lib/neography/rest.rb | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/neography.rb b/lib/neography.rb index 8b67800..363334d 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -26,6 +26,8 @@ def find_and_require_user_defined_code require 'neography/oj_parser' +require 'neography/version' + require 'neography/config' require 'neography/rest' require 'neography/neography' diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index c527b39..c5c555a 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -2,6 +2,7 @@ module Neography class Rest include HTTParty + 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 @@ -48,6 +49,7 @@ def initialize(options=ENV['NEO4J_URL'] || {}) @authentication = Hash.new @authentication = {"#{init[:authentication]}_auth".to_sym => {:username => init[:username], :password => init[:password]}} unless init[:authentication].empty? @parser = init[:parser] + @user_agent = {"User-Agent" => USER_AGENT} end def configure(protocol, server, port, directory) @@ -569,20 +571,28 @@ def evaluate_response(response) end end + def merge_options(options) + merged_options = options.merge!(@authentication).merge!(@parser) + merged_options[:headers].merge!(@user_agent) if merged_options[:headers] + merged_options + end + def get(path,options={}) - evaluate_response(HTTParty.get(configuration + URI.encode(path), options.merge!(@authentication).merge!(@parser))) + evaluate_response(HTTParty.get(configuration + URI.encode(path), merge_options(options))) end + + def post(path,options={}) - evaluate_response(HTTParty.post(configuration + URI.encode(path), options.merge!(@authentication).merge!(@parser))) + evaluate_response(HTTParty.post(configuration + URI.encode(path), merge_options(options))) end def put(path,options={}) - evaluate_response(HTTParty.put(configuration + URI.encode(path), options.merge!(@authentication).merge!(@parser))) + evaluate_response(HTTParty.put(configuration + URI.encode(path), merge_options(options))) end def delete(path,options={}) - evaluate_response(HTTParty.delete(configuration + URI.encode(path), options.merge!(@authentication).merge!(@parser))) + evaluate_response(HTTParty.delete(configuration + URI.encode(path), merge_options(options))) end def get_id(id) From cfd281f12095d1ffc4b0c3bb0a298a87147d1db7 Mon Sep 17 00:00:00 2001 From: Julian Simpson Date: Tue, 14 Aug 2012 18:49:36 +0100 Subject: [PATCH 2/3] Provide tests as well. --- lib/neography/rest.rb | 12 ++++++------ spec/integration/rest_header_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 spec/integration/rest_header_spec.rb diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index c5c555a..71e91e4 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -490,6 +490,12 @@ def clean_database(sanity_check = "not_really") false end end + + def merge_options(options) + merged_options = options.merge!(@authentication).merge!(@parser) + merged_options[:headers].merge!(@user_agent) if merged_options[:headers] + merged_options + end private @@ -571,12 +577,6 @@ def evaluate_response(response) end end - def merge_options(options) - merged_options = options.merge!(@authentication).merge!(@parser) - merged_options[:headers].merge!(@user_agent) if merged_options[:headers] - merged_options - end - def get(path,options={}) evaluate_response(HTTParty.get(configuration + URI.encode(path), merge_options(options))) end diff --git a/spec/integration/rest_header_spec.rb b/spec/integration/rest_header_spec.rb new file mode 100644 index 0000000..473d294 --- /dev/null +++ b/spec/integration/rest_header_spec.rb @@ -0,0 +1,20 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end + + it "should not add a content-type header if there's no existing headers" do + @neo.merge_options({}).should == {:parser => OjParser} + end + + it "should add a content type if there's existing headers" do + @neo.merge_options({:headers => {'Content-Type' => 'foo/bar'}}).should == + {:headers => {'Content-Type' => "foo/bar", + "User-Agent" => "Neography/#{Neography::VERSION}"}, + :parser => OjParser} + end + + +end From 0da275ea66717e23ad654554018c7f1d288350da Mon Sep 17 00:00:00 2001 From: Julian Simpson Date: Tue, 14 Aug 2012 19:20:40 +0100 Subject: [PATCH 3/3] Making tests work on Travis. --- spec/integration/rest_header_spec.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/spec/integration/rest_header_spec.rb b/spec/integration/rest_header_spec.rb index 473d294..15b0eef 100644 --- a/spec/integration/rest_header_spec.rb +++ b/spec/integration/rest_header_spec.rb @@ -6,14 +6,13 @@ end it "should not add a content-type header if there's no existing headers" do - @neo.merge_options({}).should == {:parser => OjParser} + @neo.merge_options({}).keys.should == [:parser] end it "should add a content type if there's existing headers" do - @neo.merge_options({:headers => {'Content-Type' => 'foo/bar'}}).should == - {:headers => {'Content-Type' => "foo/bar", - "User-Agent" => "Neography/#{Neography::VERSION}"}, - :parser => OjParser} + @neo.merge_options({:headers => {'Content-Type' => 'foo/bar'}})[:headers].should == + {'Content-Type' => "foo/bar", "User-Agent" => "Neography/#{Neography::VERSION}"} + end