Sha256: bd7d679b20294ee9f86a08bf4d53310c1ec9893817a7e1bf9c7c28225e882e0d

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require 'time'
require 'securerandom'

class RailsLogParser::Action
  class << self
    attr_accessor :last
  end

  SEVERITIES = %i[debug info warn error fatal].freeze
  KNOWN_EXCEPTIONS = {
    "Can't verify CSRF token authenticity." => :warn,
    'ActionController::InvalidAuthenticityToken' => :fatal,
    'ActionController::RoutingError' => :fatal,
    'ActionController::UnfilteredParameters' => :fatal,
    'ActionController::UnknownFormat' => :fatal,
    'ActiveRecord::RecordNotFound' => :fatal,
  }.freeze

  extend Enumerize
  enumerize :severity, in: SEVERITIES, predicates: true
  enumerize :type, in: %i[request without_request delayed_job active_job], predicates: true
  attr_reader :datetime

  def initialize(type, id)
    self.type = type
    @id = id
    @messages = []
    @stacktrace = []
    self.class.last = self
  end

  def severity=(value)
    value = value.downcase.to_sym
    return unless severity.nil? || SEVERITIES.index(severity.to_sym) < SEVERITIES.index(value)

    super(value)
    @headline = nil
  end

  def known_exception?(key = nil)
    @messages.any? do |message|
      KNOWN_EXCEPTIONS.any? { |e, s| message.include?(e) && severity == s && (key.nil? || key == e) }
    end
  end

  def headline
    @headline.presence || @messages.first
  end

  def datetime=(value)
    @datetime ||= Time.parse(value)
  end

  def after?(datetime)
    @datetime > datetime
  end

  def add_message(value)
    @messages.push(value)
    @headline = value if @headline.nil?
  end

  def add_stacktrace(value)
    @stacktrace.push(value)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_log_parser-0.0.14 lib/rails_log_parser/action.rb
rails_log_parser-0.0.13 lib/rails_log_parser/action.rb
rails_log_parser-0.0.12 lib/rails_log_parser/action.rb