From 51d803458f0db006e408589b8ba94cf527aac621 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Fri, 30 Jan 2026 10:23:20 -0500 Subject: [PATCH] Support passing production: ... to QboApi.new --- README.md | 2 +- lib/qbo_api.rb | 16 +++++++++++++--- spec/qbo_api_spec.rb | 10 ++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1114f69..d659606 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ If you're not using ActiveSupport, you'll need to use `#iso8601` method to conve ### Configuration options - By default this client runs against a QBO sandbox. To run against the production QBO API URL do: ```ruby -QboApi.production = true +QboApi.production = true # or pass `production:` to QboApi.new(...) ``` - Logging: ```ruby diff --git a/lib/qbo_api.rb b/lib/qbo_api.rb index b1df4c8..ed8ef42 100644 --- a/lib/qbo_api.rb +++ b/lib/qbo_api.rb @@ -32,6 +32,7 @@ class QboApi def initialize(attributes = {}) raise ArgumentError, "missing keyword: access_token" unless attributes.key?(:access_token) raise ArgumentError, "missing keyword: realm_id" unless attributes.key?(:realm_id) + @production = attributes.delete(:production) attributes = default_attributes.merge!(attributes) attributes.each do |attribute, value| public_send("#{attribute}=", value) @@ -53,13 +54,22 @@ def endpoint_url @endpoint_url.dup end + # Use the `production:` from initialization, + # or fall back to the current setting at `QboApi.production` + def production + if @production.nil? + self.class.production + else + @production + end + end + private def get_endpoint - prod = self.class.production { - accounting: prod ? V3_ENDPOINT_BASE_URL.sub("sandbox-", '') : V3_ENDPOINT_BASE_URL, - payments: prod ? PAYMENTS_API_BASE_URL.sub("sandbox.", '') : PAYMENTS_API_BASE_URL + accounting: production ? V3_ENDPOINT_BASE_URL.sub("sandbox-", '') : V3_ENDPOINT_BASE_URL, + payments: production ? PAYMENTS_API_BASE_URL.sub("sandbox.", '') : PAYMENTS_API_BASE_URL }.fetch(endpoint) do raise KeyError, "Invalid endpoint: #{endpoint.inspect}" end diff --git a/spec/qbo_api_spec.rb b/spec/qbo_api_spec.rb index 2d66a69..ea15d30 100644 --- a/spec/qbo_api_spec.rb +++ b/spec/qbo_api_spec.rb @@ -43,11 +43,21 @@ end it '.get_endpoint' do + # These two use their initialized value, not the global setting: + prod_api = QboApi.new(creds.merge(production: true)) + sandbox_api = QboApi.new(creds.merge(production: false)) + expect(api.send(:get_endpoint)).to eq QboApi::V3_ENDPOINT_BASE_URL + expect(prod_api.send(:get_endpoint)).to_not match /sandbox/ + expect(sandbox_api.send(:get_endpoint)).to match /sandbox/ + QboApi.production = true expect(api.send(:get_endpoint)).to_not match /sandbox/ new_api = QboApi.new(creds.to_h) expect(new_api.send(:get_endpoint)).to_not match /sandbox/ + + expect(prod_api.send(:get_endpoint)).to_not match /sandbox/ + expect(sandbox_api.send(:get_endpoint)).to match /sandbox/ QboApi.production = false api = QboApi.new(creds.to_h.merge(endpoint: :payments)) expect(api.send(:get_endpoint)).to eq QboApi::PAYMENTS_API_BASE_URL