Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions lib/qbo_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/qbo_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down