Files
baffle-hub/app/models/dsn.rb
2025-11-09 21:57:31 +11:00

39 lines
1.0 KiB
Ruby

class Dsn < ApplicationRecord
validates :key, presence: true, uniqueness: true
validates :name, presence: true
before_validation :generate_key, on: :create
scope :enabled, -> { where(enabled: true) }
def self.authenticate(key)
enabled.find_by(key: key)
end
def full_dsn_url
# Generate a complete DSN URL like Sentry does
# Format: https://{key}@{domain}/api/events
domain = Rails.application.config.action_mailer.default_url_options[:host] ||
ENV['RAILS_HOST'] ||
'localhost:3000'
protocol = Rails.env.development? ? 'http' : 'https'
"#{protocol}://#{key}@#{domain}/api/events"
end
def api_endpoint_url
# Just the API endpoint URL (without key)
domain = Rails.application.config.action_mailer.default_url_options[:host] ||
ENV['RAILS_HOST'] ||
'localhost:3000'
protocol = Rails.env.development? ? 'http' : 'https'
"#{protocol}://#{domain}/api/events"
end
private
def generate_key
self.key ||= SecureRandom.hex(32)
end
end