fix: Handle blank API keys as disabled clients#158
Merged
Conversation
Prompt To Fix All With AIFix the following 3 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 3
lib/posthog/client.rb:109
The error message says "empty after trimming whitespace" but is now also triggered when `@api_key` is `nil` — in that case no trimming ever happened, so the message is inaccurate and may confuse someone debugging a missing-key configuration.
```suggestion
logger.error('api_key is missing or empty after trimming whitespace; check your project API key') if @disabled
```
### Issue 2 of 3
lib/posthog/client.rb:85
The `respond_to?(:empty?)` guard is superfluous here. By the time this line runs, `@api_key` has already been through `normalize_string_option`, which returns only `nil` or a `String`. Since `nil` is already handled by the preceding `nil?` check, `@api_key` can only be a `String` at the `empty?` call — duck-typing adds noise with no defensive value.
```suggestion
@disabled = @api_key.nil? || @api_key.empty?
```
### Issue 3 of 3
spec/posthog/client_spec.rb:33-53
**Prefer parametrised tests over inline loops**
Several new `it` blocks (lines 33–53 and 85–97) iterate over input arrays inside the body rather than using RSpec's parametrization. With an inline `each` loop a single failure reports against the whole example rather than the specific input, making failures harder to diagnose. Consider using `where`-style tables or individual `context`/`it` blocks for each value, as the team's convention requires.
Reviews (1): Last reviewed commit: "Handle blank API keys as disabled client..." | Re-trigger Greptile |
dustinbyrne
approved these changes
May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Blank, missing, or whitespace-only API keys should behave like disabled PostHog clients instead of raising during initialization or starting background network work. This lets apps safely initialize the SDK when API key configuration is absent.
Changes:
capturefor disabled clients to skip unnecessary event processing.💚 How did you test it?
bundle exec rspec spec/posthog/client_spec.rbbundle exec rspec📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file