Sha256: 5db15c8c38263602a40744e8ad91c0fc319919f52a467289bd9c130b668f62c8

Contents?: true

Size: 1.22 KB

Versions: 17

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Cased
  module Sensitive
    class Handler
      def self.handlers
        @handlers ||= []
      end

      class << self
        attr_writer :handlers
      end

      def self.register(label, handler)
        handlers << Handler.new(label, handler)
      end

      attr_reader :label

      def initialize(label, handler)
        @label = label.to_sym
        @handler = prepare_handler(handler)
      end

      def call(audit_event, key, value)
        @handler.call(audit_event, key.to_sym, value)
      end

      private

      def prepare_handler(handler)
        case handler
        when Regexp
          proc do |_audit_event, key, value|
            string = Cased::Sensitive::String.new(value)
            string.matches(handler).collect do |match|
              begin_offset = match.begin(0)
              end_offset = match.end(0)

              Cased::Sensitive::Range.new(
                label: label,
                key: key,
                begin_offset: begin_offset,
                end_offset: end_offset,
              )
            end
          end
        else
          raise ArgumentError, "expected #{handler} to be a Regexp or Proc"
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
cased-ruby-0.8.0 lib/cased/sensitive/handler.rb
cased-ruby-0.7.1 lib/cased/sensitive/handler.rb
cased-ruby-0.7.0 lib/cased/sensitive/handler.rb
cased-ruby-0.6.1 lib/cased/sensitive/handler.rb
cased-ruby-0.5.2 lib/cased/sensitive/handler.rb
cased-ruby-0.5.1 lib/cased/sensitive/handler.rb
cased-ruby-0.5.0 lib/cased/sensitive/handler.rb
cased-ruby-0.4.8 lib/cased/sensitive/handler.rb
cased-ruby-0.4.7 lib/cased/sensitive/handler.rb
cased-ruby-0.4.6 lib/cased/sensitive/handler.rb
cased-ruby-0.4.5 lib/cased/sensitive/handler.rb
cased-ruby-0.4.4 lib/cased/sensitive/handler.rb
cased-ruby-0.4.3 lib/cased/sensitive/handler.rb
cased-ruby-0.4.2 lib/cased/sensitive/handler.rb
cased-ruby-0.4.1 lib/cased/sensitive/handler.rb
cased-ruby-0.4.0 lib/cased/sensitive/handler.rb
cased-ruby-0.3.3 lib/cased/sensitive/handler.rb