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
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,29 @@ Incognia.configure(
)
```

To reuse HTTP connections between requests, enable `keep_alive`. When enabled,
`max_connections` sets the maximum number of concurrent persistent
connections used by the client.
HTTP connection reuse is enabled by default. When connection reuse is enabled,
`max_connections` sets the maximum number of concurrent persistent connections
used by the client.

```ruby
Incognia.configure(
client_id: ENV['INCOGNIA_CLIENT_ID'],
client_secret: ENV['INCOGNIA_CLIENT_SECRET'],
keep_alive: true,
max_connections: 5
)
```

To disable persistent connections, set `keep_alive: false`. `max_connections`
can only be configured when `keep_alive` is enabled.

```ruby
Incognia.configure(
client_id: ENV['INCOGNIA_CLIENT_ID'],
client_secret: ENV['INCOGNIA_CLIENT_SECRET'],
keep_alive: false
)
```

For sandbox credentials, refer to the [API testing guide](https://developer.incognia.com/).

:bulb: For Rails applications it's recommended to create an initializer file, for example `config/initializers/incognia.rb`.
Expand Down
2 changes: 2 additions & 0 deletions lib/incognia_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class APIError < StandardError
attr_reader :message, :errors, :status

def initialize(message, response_info = {})
response_info ||= {}
Comment on lines 32 to +33
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it sent as null?
As you're already treating nil, we can change the default to be nil, wdyt?

Suggested change
def initialize(message, response_info = {})
response_info ||= {}
def initialize(message, response_info = nil)
response_info ||= {}


@status = response_info[:status]
@errors = response_info[:body]
@message = format_message(message)
Expand Down
2 changes: 1 addition & 1 deletion lib/incognia_api/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Configuration

attr_accessor :client_id, :client_secret, :host, :keep_alive, :max_connections

def configure(client_id:, client_secret:, host: nil, keep_alive: false, max_connections: nil)
def configure(client_id:, client_secret:, host: nil, keep_alive: true, max_connections: nil)
Copy link
Copy Markdown
Member

@ottony ottony May 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a breaking change, or is it okay to assume it as the default?

I'm asking so we can decide about the version.

validate_connection_settings!(keep_alive: keep_alive, max_connections: max_connections)

@client_id = client_id
Expand Down
34 changes: 33 additions & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Incognia
Incognia.configure(
client_id: 'client_id',
client_secret: 'client_secret',
host: 'https://api.incognia.com/api'
host: 'https://api.incognia.com/api',
keep_alive: false
)
end

Expand Down Expand Up @@ -258,6 +259,37 @@ module Incognia
before { Singleton.__init__(described_class) }
after { instance.reset! }

it "uses the persistent adapter by default" do
Incognia.configure(
client_id: 'client_id',
client_secret: 'client_secret',
host: 'https://api.incognia.com/api'
)

allow_any_instance_of(Faraday::RackBuilder).to receive(:adapter).and_call_original
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we need to mock it before the expect_any_instance_of? Does it have another call, with different params?

expect_any_instance_of(Faraday::RackBuilder).to receive(:adapter)
.with(:net_http_persistent)
.and_call_original

instance.connection
end

Comment thread
figueredo marked this conversation as resolved.
it "uses the default adapter when keep_alive is false" do
Incognia.configure(
client_id: 'client_id',
client_secret: 'client_secret',
host: 'https://api.incognia.com/api',
keep_alive: false
)

allow_any_instance_of(Faraday::RackBuilder).to receive(:adapter).and_call_original
expect_any_instance_of(Faraday::RackBuilder).to receive(:adapter)
.with(Faraday.default_adapter)
.and_call_original

instance.connection
end

it "uses the persistent adapter with the configured max_connections" do
Incognia.configure(
client_id: 'client_id',
Expand Down
7 changes: 5 additions & 2 deletions spec/incognia_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ module Incognia
expect(Configuration.instance.client_id).to eq(config[:client_id])
expect(Configuration.instance.client_secret).to eq(config[:client_secret])
expect(Configuration.instance.host).to eq(config[:host])
expect(Configuration.instance.keep_alive).to eq(true)
end

it 'raises when max_connections is set without keep_alive' do
it 'raises when max_connections is set with keep_alive disabled' do
expect {
Incognia.configure(
client_id: SecureRandom.uuid,
client_secret: SecureRandom.uuid,
keep_alive: false,
max_connections: 5
)
}.to raise_error(ArgumentError, 'max_connections requires keep_alive: true')
Expand Down Expand Up @@ -61,7 +63,8 @@ module Incognia
Incognia.configure(
client_id: 'client_id',
client_secret: 'client_secret',
host: 'https://api.incognia.com/api'
host: 'https://api.incognia.com/api',
keep_alive: false
)
end

Expand Down
Loading