Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic HTTP query logging #125

Merged
merged 1 commit into from
Dec 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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