Module: BetterAuth::Telemetry::ProjectId

Defined in:
lib/better_auth/telemetry/project_id.rb

Overview

Project-name resolver used by project_id.

The chain (Requirement 14.7) is:

  1. CurrentOptions.app_name — when set and not the default ‘“Better Auth”`.

  2. ‘File.basename(Bundler.root)` — the directory name of the Gemfile root.

Every fallback is wrapped in ‘rescue StandardError; nil` so that a missing Bundler load, an unreadable lockfile, or any unrelated error in one rule degrades to the next rule rather than escaping to the caller (Requirement 14.8).

Constant Summary collapse

DEFAULT_APP_NAME =

Upstream sentinel: the ‘Better Auth` literal is treated as “not configured” so the chain falls through to the Bundler signals.

"Better Auth"

Class Method Summary collapse

Class Method Details

.from_app_nameString?

Read the host’s ‘app_name` from CurrentOptions. Treats the literal `“Better Auth”` (the upstream default) as “not configured” so it never wins over the Bundler-derived rules.

Returns:

  • (String, nil)


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/better_auth/telemetry/project_id.rb', line 89

def from_app_name
  name = CurrentOptions.app_name
  return nil if name.nil?
  return nil unless name.is_a?(String)
  return nil if name.empty?
  return nil if name == DEFAULT_APP_NAME

  name
rescue
  nil
end

.from_bundler_rootString?

Directory name of ‘Bundler.root`. The closest Ruby analog to upstream’s “directory containing package.json” fallback.

Returns:

  • (String, nil)


127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/better_auth/telemetry/project_id.rb', line 127

def from_bundler_root
  return nil unless defined?(::Bundler)

  root = ::Bundler.root
  return nil if root.nil?

  name = File.basename(root.to_s)
  return nil if name.nil? || name.empty?

  name
rescue
  nil
end

.from_locked_gemsString?

Legacy helper retained as a test seam for older specs. The resolver no longer uses the first locked dependency as project identity because that can collide across unrelated apps.

Returns:

  • (String, nil)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/better_auth/telemetry/project_id.rb', line 106

def from_locked_gems
  return nil unless defined?(::Bundler)

  locked = ::Bundler.locked_gems
  return nil if locked.nil?

  spec = locked.specs&.first
  return nil if spec.nil?

  name = spec.name
  return nil if name.nil? || name.empty?

  name
rescue
  nil
end

.resolve_project_nameString?

Returns the resolved project name, or ‘nil` when no rule produced a non-empty string.

Returns:

  • (String, nil)

    the resolved project name, or ‘nil` when no rule produced a non-empty string.



78
79
80
81
82
# File 'lib/better_auth/telemetry/project_id.rb', line 78

def resolve_project_name
  from_app_name || from_bundler_root
rescue
  nil
end