38 lines
886 B
Ruby
38 lines
886 B
Ruby
class Webhooks::Trigger
|
|
def initialize(url, payload, webhook_type)
|
|
@url = url
|
|
@payload = payload
|
|
@webhook_type = webhook_type
|
|
end
|
|
|
|
def self.execute(url, payload, webhook_type)
|
|
new(url, payload, webhook_type).execute
|
|
end
|
|
|
|
def execute
|
|
perform_request
|
|
rescue StandardError => e
|
|
Rails.logger.warn "Webhook request failed for #{@url}: #{e.message}"
|
|
raise CustomExceptions::Webhook::RetriableError, "Webhook request failed: #{e.message}"
|
|
end
|
|
|
|
private
|
|
|
|
def perform_request
|
|
RestClient::Request.execute(
|
|
method: :post,
|
|
url: @url,
|
|
payload: @payload.to_json,
|
|
headers: { content_type: :json, accept: :json },
|
|
timeout: webhook_timeout
|
|
)
|
|
end
|
|
|
|
def webhook_timeout
|
|
raw_timeout = GlobalConfig.get_value('WEBHOOK_TIMEOUT')
|
|
timeout = raw_timeout.presence&.to_i
|
|
|
|
timeout&.positive? ? timeout : 5
|
|
end
|
|
end
|