Class: ApiBase::ApiLog

Inherits:
ApplicationRecord show all
Defined in:
app/models/api_base/api_log.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start_outgoing_request(origin, method, endpoint, payload) ⇒ Object



37
38
39
40
41
# File 'app/models/api_base/api_log.rb', line 37

def self.start_outgoing_request(origin, method, endpoint, payload)
  ApiLog.new api: origin.identifier, origin: origin.class.to_s, source: 'outgoing_request',
             endpoint: "#{origin.connection.url_prefix}#{endpoint}", method:,
             request_headers: origin.connection.headers, request_body: payload
end

.start_webhook_request(origin, request) ⇒ Object



52
53
54
55
56
57
58
# File 'app/models/api_base/api_log.rb', line 52

def self.start_webhook_request(origin, request)
  ApiLog.new api: origin, origin: origin.class.to_s, source: 'incoming_webhook',
             endpoint: request.fullpath, method: request.method,
             request_headers: request.headers.env.reject { |key|
               key.to_s.include?('.')
             }, request_body: request.params
end

Instance Method Details

#complete_outgoing_request(response, duration) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/models/api_base/api_log.rb', line 43

def complete_outgoing_request(response, duration)
  # Ensure we are recording the actual headers that were sent on the request.
  # The ones set from the connection might not be the final headers.
  self.request_headers = response.env.request_headers
  # Set the rest of the response attributes.
  assign_attributes status_code: response.status, duration:,
                    response_body: response.body, response_headers: response.headers
end

#complete_webhook_request(response, duration) ⇒ Object



60
61
62
63
64
# File 'app/models/api_base/api_log.rb', line 60

def complete_webhook_request(response, duration)
  # Set the rest of the response attributes.
  assign_attributes status_code: response.status, duration:,
                    response_body: response.body, response_headers: response.headers
end

#filter_sensitive_dataObject



66
67
68
69
70
71
72
73
74
# File 'app/models/api_base/api_log.rb', line 66

def filter_sensitive_data
  parse_json_fields

  %i[request_headers request_body response_headers response_body exception].each do |prop|
    self[prop] = yield(self[prop]) if self[prop].is_a?(Hash)
  end

  self.sanitized = true
end