2021-06-09 18:53:11 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Webhook < ApplicationRecord
|
2021-06-17 20:54:30 +08:00
|
|
|
enum http_method: { get: 0, post: 1, patch: 2 }
|
2021-06-09 18:53:11 +08:00
|
|
|
|
|
|
|
belongs_to :activity_filter
|
2021-06-17 20:54:30 +08:00
|
|
|
validates :http_method, presence: true
|
2021-06-09 18:53:11 +08:00
|
|
|
validates :url, presence: true
|
2021-08-26 20:08:15 +08:00
|
|
|
validate :enabled?
|
2021-06-09 18:53:11 +08:00
|
|
|
validate :valid_url
|
|
|
|
|
2021-06-17 15:19:04 +08:00
|
|
|
scope :active, -> { where(active: true) }
|
|
|
|
|
2021-06-09 18:53:11 +08:00
|
|
|
private
|
|
|
|
|
2021-08-26 20:08:15 +08:00
|
|
|
def enabled?
|
|
|
|
unless Rails.application.config.x.webhooks_enabled
|
|
|
|
errors.add(:configuration, I18n.t('activerecord.errors.models.webhook.attributes.configuration.disabled'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-09 18:53:11 +08:00
|
|
|
def valid_url
|
|
|
|
unless /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(url)
|
|
|
|
errors.add(:url, I18n.t('activerecord.errors.models.webhook.attributes.url.not_valid'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|