summaryrefslogtreecommitdiff
path: root/app/models/hooks/web_hook_log.rb
blob: 8c0565e4a38497127ab33073ecc52eb23fa48f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

class WebHookLog < ApplicationRecord
  include SafeUrl
  include Presentable
  include DeleteWithLimit
  include CreatedAtFilterable
  include PartitionedTable

  self.primary_key = :id

  partitioned_by :created_at, strategy: :monthly, retain_for: 3.months

  belongs_to :web_hook

  serialize :request_headers, Hash # rubocop:disable Cop/ActiveRecordSerialize
  serialize :request_data, Hash # rubocop:disable Cop/ActiveRecordSerialize
  serialize :response_headers, Hash # rubocop:disable Cop/ActiveRecordSerialize

  validates :web_hook, presence: true

  before_save :obfuscate_basic_auth

  def self.recent
    where('created_at >= ?', 2.days.ago.beginning_of_day)
      .order(created_at: :desc)
  end

  def success?
    response_status =~ /^2/
  end

  def internal_error?
    response_status == WebHookService::InternalErrorResponse::ERROR_MESSAGE
  end

  private

  def obfuscate_basic_auth
    self.url = safe_url
  end
end