Skip to content

Commit

Permalink
Merge pull request #125 from erez-rabih/query-logging
Browse files Browse the repository at this point in the history
Basic HTTP query logging
  • Loading branch information
maxdemarzi committed Dec 5, 2013
2 parents 51d16c1 + 4f49812 commit 7568b36
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,27 @@ def merge_options(options)

ACTIONS.each do |action|
define_method(action) do |path, options = {}|
authenticate(configuration + path)
evaluate_response(@client.send(action.to_sym, configuration + path, merge_options(options)[:body], merge_options(options)[:headers]))
query_path = configuration + path
query_body = merge_options(options)[:body]
authenticate(query_path)
log path, query_body do
evaluate_response(@client.send(action.to_sym, query_path, query_body, merge_options(options)[:headers]))
end
end
end

def log(path, body)
if @log_enabled
start_time = Time.now
response = yield
time = ((Time.now - start_time) * 1000).round(2)
@logger.info "[Neography::Query] #{path} #{body} [#{time}ms]"
response
else
yield
end
end

def authenticate(path)
@client.set_auth(path,
@authentication[@authentication.keys.first][:username],
Expand Down
25 changes: 25 additions & 0 deletions spec/unit/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,31 @@ module Neography
end

end

context "query logging" do
before do
connection.logger = Logger.new(nil)
connection.log_enabled = true
end

let :expected_response do
"expected_response"
end

let :request_body do
{key1: :val1}
end

it "should log query" do
connection.should_receive(:log).with("/foo/bar", request_body).once
connection.get("/foo/bar", {body: request_body})
end

it "should return original response" do
connection.stub(:evaluate_response).and_return expected_response
connection.get("/foo/bar").should eq expected_response
end
end
end
end
end
Expand Down

0 comments on commit 7568b36

Please sign in to comment.