Sha256: fcb58163ef7664573f8311e927bda58947db2e81a733fdfbcc8820dcf2ea3928

Contents?: true

Size: 1.9 KB

Versions: 16

Compression:

Stored size: 1.9 KB

Contents

require "timber/event"
require "timber/util"

module Timber
  module Events
    # The controller call event tracks controller invocations. For example, this line in Rails:
    #
    #   Processing by PagesController#home as HTML
    #
    # @note This event should be installed automatically through integrations,
    #   such as the {Integrations::ActionController::LogSubscriber} integration.
    class ControllerCall < Timber::Event
      PARAMS_JSON_MAX_BYTES = 8192.freeze
      PASSWORD_NAME = 'password'.freeze

      attr_reader :controller, :action, :params, :format

      def initialize(attributes)
        @controller = attributes[:controller] || raise(ArgumentError.new(":controller is required"))
        @action = attributes[:action] || raise(ArgumentError.new(":action is required"))
        @params = sanitize_params(attributes[:params])
        @format = attributes[:format]
      end

      def to_hash
        {controller: controller, action: action, params_json: params_json}
      end
      alias to_h to_hash

      # Builds a hash representation containing simple objects, suitable for serialization (JSON).
      def as_json(_options = {})
        {:controller_call => to_hash}
      end

      def message
        message = "Processing by #{controller}##{action}"
        if !message.nil?
          message << " as #{format}"
        end
        if !params.nil? && params.length > 0
          message << "\n  Parameters: #{params.inspect}"
        end
        message
      end

      private
        def params_json
          @params_json ||=
            if params.nil? || params == {}
              nil
            else
              params.to_json.byteslice(0, PARAMS_JSON_MAX_BYTES)
            end
        end

        def sanitize_params(params)
          if params.is_a?(::Hash)
            Util::Hash.sanitize(params, [PASSWORD_NAME])
          else
            params
          end
        end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
timber-2.3.2 lib/timber/events/controller_call.rb
timber-2.3.1 lib/timber/events/controller_call.rb
timber-2.3.0 lib/timber/events/controller_call.rb
timber-2.2.3 lib/timber/events/controller_call.rb
timber-2.2.2 lib/timber/events/controller_call.rb
timber-2.2.1 lib/timber/events/controller_call.rb
timber-2.2.0 lib/timber/events/controller_call.rb
timber-2.1.10 lib/timber/events/controller_call.rb
timber-2.1.9 lib/timber/events/controller_call.rb
timber-2.1.8 lib/timber/events/controller_call.rb
timber-2.1.7 lib/timber/events/controller_call.rb
timber-2.1.6 lib/timber/events/controller_call.rb
timber-2.1.5 lib/timber/events/controller_call.rb
timber-2.1.4 lib/timber/events/controller_call.rb
timber-2.1.3 lib/timber/events/controller_call.rb
timber-2.1.2 lib/timber/events/controller_call.rb