Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.1 - 2025-05-20

1. fix: was warning on absent UUID when capturing ([#67](https://github.com/PostHog/posthog-ruby/pull/67))

## 3.0.0 - 2025-05-20

1. Drops support for Ruby 2.x ([#63](https://github.com/PostHog/posthog-ruby/pull/63))
Expand Down
15 changes: 12 additions & 3 deletions lib/posthog/field_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def parse_for_capture(fields)

isoify_dates! properties

common['uuid'] = uuid if uuid? uuid
common['uuid'] = uuid if valid_uuid_for_event_props? uuid

common.merge(
{
Expand Down Expand Up @@ -174,8 +174,17 @@ def check_is_hash!(obj, name)
raise ArgumentError, "#{name} must be a Hash" unless obj.is_a? Hash
end

def uuid?(uuid)
is_valid_uuid = uuid.match?(/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i) if uuid
# @param [Object] uuid - the UUID to validate, user provided, so we don't know the type
# @return [TrueClass, FalseClass] - true if the UUID is valid or absent, false otherwise
def valid_uuid_for_event_props?(uuid)
return true if uuid.nil?

unless uuid.is_a?(String)
logger.warn 'UUID is not a string. Ignoring it.'
return false
end

is_valid_uuid = uuid.match?(/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i)
logger.warn "UUID is not valid: #{uuid}. Ignoring it." unless is_valid_uuid

is_valid_uuid
Expand Down
2 changes: 1 addition & 1 deletion lib/posthog/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module PostHog
VERSION = '3.0.0'
VERSION = '3.0.1'
end
21 changes: 21 additions & 0 deletions spec/posthog/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ module PostHog
end

it 'captures uuid when provided' do
allow(logger).to receive(:warn)

client.capture(
{
distinct_id: 'distinct_id',
Expand All @@ -440,9 +442,12 @@ module PostHog
)
last_message = client.dequeue_last_message
expect(last_message['uuid']).to eq('123e4567-e89b-12d3-a456-426614174000')
expect(logger).not_to have_received(:warn)
end

it 'does not require a uuid be provided - ingestion will generate when absent' do
allow(logger).to receive(:warn)

client.capture(
{
distinct_id: 'distinct_id',
Expand All @@ -452,6 +457,7 @@ module PostHog
properties = client.dequeue_last_message[:properties]
# ingestion will add a UUID if one is not provided
expect(properties['uuid']).to be_nil
expect(logger).not_to have_received(:warn)
end

it 'does not use invalid uuid' do
Expand All @@ -468,6 +474,21 @@ module PostHog
'UUID is not valid: i am obviously not a uuid. Ignoring it.'
)
end

it 'does not use really invalid uuid' do
client.capture(
{
distinct_id: 'distinct_id',
event: 'test_event',
uuid: { 'not a uuid' => 'not a uuid' }
}
)
properties = client.dequeue_last_message[:properties]
expect(properties['uuid']).to be_nil
expect(logger).to have_received(:warn).with(
'UUID is not a string. Ignoring it.'
)
end
end

describe '#before_send' do
Expand Down