Skip to content

Commit

Permalink
Merge branch 'ruckus:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefernando1 authored Dec 19, 2023
2 parents 3c0500e + 5c19c7e commit 34c42b2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,17 @@ JSON]( https://github.com/ruckus/quickbooks-ruby/issues/257#issuecomment-1268344

## Logging

Set the default log enablement:
```ruby
Quickbooks.log = true
```

Configure a service instance to log (or not) independently:
```ruby
customer_service = Quickbooks::Service::Customer.new
customer_service.log = true
```

By default, logging is directed at STDOUT, but another target may be defined, e.g. in Rails
```ruby
Quickbooks.logger = Rails.logger
Expand Down
6 changes: 2 additions & 4 deletions lib/quickbooks-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,8 @@ def log_xml_pretty_print?
end

def log(msg)
if log?
logger.info(msg)
logger.flush if logger.respond_to?(:flush)
end
logger.info(msg)
logger.flush if logger.respond_to?(:flush)
end
end # << self

Expand Down
8 changes: 6 additions & 2 deletions lib/quickbooks/service/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def url_for_query(query = nil, start_position = 1, max_results = 20, options = {
query ||= default_model_query
query = "#{query} STARTPOSITION #{start_position} MAXRESULTS #{max_results}"

"#{url_for_base}/query?query=#{CGI.escape(query)}"
URI("#{url_for_base}/query").tap do |uri|
params = Faraday::Utils::ParamsHash.new
params.update(options.merge(query: query))
uri.query = params.to_query
end.to_s
end

private
Expand Down Expand Up @@ -116,7 +120,7 @@ def fetch_collection(query, model, options = {})
start_position = ((page - 1) * per_page) + 1 # page=2, per_page=10 then we want to start at 11
max_results = per_page

response = do_http_get(url_for_query(query, start_position, max_results))
response = do_http_get(url_for_query(query, start_position, max_results, options.except(:page, :per_page)))

parse_collection(response, model)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/quickbooks/util/logging.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Quickbooks
module Util
module Logging
attr_writer :log

def log(msg)
::Quickbooks.log(msg)
::Quickbooks.log(msg) if log?
end

def log_multiple(messages)
Expand All @@ -14,7 +16,7 @@ def log_multiple(messages)
end

def log?
::Quickbooks.log?
defined?(@log) ? @log : ::Quickbooks.log?
end

def condense_logs?
Expand Down
58 changes: 58 additions & 0 deletions spec/lib/quickbooks/util/logging_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe Quickbooks::Util::Logging do
before { Quickbooks.log = general_log_setting }
let(:general_log_setting) { true }
let(:dummy_class) { Class.new { include Quickbooks::Util::Logging } }

describe 'log' do
context 'when one service needs to log but general logging is disabled' do
let(:general_log_setting) { false }
let(:control_instance) { instance = dummy_class.new }
let(:loggable_instance) do
instance = dummy_class.new
instance.log = true
instance
end

it 'allows one service instance to log without affecting other service instances' do
expect(Quickbooks.log?).to be(false)
expect(control_instance.log?).to be(false)
expect(loggable_instance.log?).to be(true)
end

it 'does not log if disabled' do
expect(Quickbooks).not_to receive(:log)
control_instance.log('test message')
end

it 'does log if enabled' do
expect(Quickbooks).to receive(:log)
loggable_instance.log('test message')
end
end

context 'when one service needs to not log but general logging is enabled' do
let(:control_instance) { instance = dummy_class.new }
let(:unloggable_instance) do
instance = dummy_class.new
instance.log = false
instance
end

it 'allows one service instance to log without affecting other service instances' do
expect(Quickbooks.log?).to be(true)
expect(control_instance.log?).to be(true)
expect(unloggable_instance.log?).to be(false)
end

it 'does not log if disabled' do
expect(Quickbooks).not_to receive(:log)
unloggable_instance.log('test message')
end

it 'does log if enabled' do
expect(Quickbooks).to receive(:log)
control_instance.log('test message')
end
end
end
end

0 comments on commit 34c42b2

Please sign in to comment.