Skip to content

Commit

Permalink
Merge pull request #53 from simpsonjulian/user-agent
Browse files Browse the repository at this point in the history
HTTP User-Agent to inform Neo4j about client versions
  • Loading branch information
maxdemarzi committed Aug 14, 2012
2 parents b0f6aec + 0da275e commit 4455051
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def find_and_require_user_defined_code

require 'neography/multi_json_parser'

require 'neography/version'

require 'neography/config'
require 'neography/rest'
require 'neography/neography'
Expand Down
18 changes: 14 additions & 4 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -488,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

Expand Down Expand Up @@ -572,19 +580,21 @@ def evaluate_response(response)
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)
Expand Down
19 changes: 19 additions & 0 deletions spec/integration/rest_header_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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({}).keys.should == [:parser]
end

it "should add a content type if there's existing headers" do
@neo.merge_options({:headers => {'Content-Type' => 'foo/bar'}})[:headers].should ==
{'Content-Type' => "foo/bar", "User-Agent" => "Neography/#{Neography::VERSION}"}

end


end

0 comments on commit 4455051

Please sign in to comment.