Sha256: 69e9bedb8c16e85034754a3a91af905906553f246714eabdd56ce2ecd1bf8a4f

Contents?: true

Size: 1.72 KB

Versions: 13

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require 'cased/sensitive/string'

module Cased
  module Sensitive
    class Processor
      def self.process(audit_event, handlers = nil)
        handlers ||= Cased::Sensitive::Handler.handlers
        processor = new(audit_event, handlers)
        processor.process
        processor
      end

      def self.process!(audit_event, handlers = nil)
        processor = process(audit_event, handlers)
        return unless processor.sensitive?

        audit_event[:'.cased'] = {
          pii: processor.to_h,
        }
      end

      attr_reader :audit_event
      attr_reader :handlers

      def initialize(audit_event, handlers)
        @audit_event = audit_event.dup.freeze
        @ranges = []
        @handlers = handlers
      end

      def process
        return true if defined?(@processed)

        walk(audit_event)
        @processed = true
      end

      def ranges
        @ranges.flatten
      end

      def sensitive?
        process && ranges.any?
      end

      def to_h
        results = {}
        ranges.each do |range|
          results[range.key] ||= []
          results[range.key] << range.to_h
        end
        results
      end

      private

      def walk(hash)
        hash.each_with_json_path do |path, value|
          case value
          when Cased::Sensitive::String
            @ranges << value.range(key: path)
          when ::String
            process_handlers(audit_event, path, value)
          end
        end
      end

      def process_handlers(audit_event, path, value)
        handlers.each do |handler|
          ranges = handler.call(audit_event, path, value)
          @ranges << ranges unless ranges.nil? || ranges.empty?
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
cased-ruby-0.5.2 lib/cased/sensitive/processor.rb
cased-ruby-0.5.1 lib/cased/sensitive/processor.rb
cased-ruby-0.5.0 lib/cased/sensitive/processor.rb
cased-ruby-0.4.8 lib/cased/sensitive/processor.rb
cased-ruby-0.4.7 lib/cased/sensitive/processor.rb
cased-ruby-0.4.6 lib/cased/sensitive/processor.rb
cased-ruby-0.4.5 lib/cased/sensitive/processor.rb
cased-ruby-0.4.4 lib/cased/sensitive/processor.rb
cased-ruby-0.4.3 lib/cased/sensitive/processor.rb
cased-ruby-0.4.2 lib/cased/sensitive/processor.rb
cased-ruby-0.4.1 lib/cased/sensitive/processor.rb
cased-ruby-0.4.0 lib/cased/sensitive/processor.rb
cased-ruby-0.3.3 lib/cased/sensitive/processor.rb