Sha256: e2d89faa91a40d873f82b11136312e788a67cdae8e31c2fa9b3120d6fac652c8

Contents?: true

Size: 900 Bytes

Versions: 1

Compression:

Stored size: 900 Bytes

Contents

# frozen_string_literal: true
require 'credit_card_validations'

module SensitiveDataFilter
  module Types
    module CreditCard
      SEPARATORS = /[\s-]/
      SEPRS      = SEPARATORS.source + '*'
      LENGTHS    = (11..19)
      CARD       = Regexp.new(LENGTHS.map { |l| /(?=((?:\d#{SEPRS}){#{l - 1}}\d)?)/.source }.join)
      FILTERED   = '[FILTERED]'

      module_function def valid?(number)
        return false unless number.is_a? String
        CreditCardValidations::Detector.new(number.gsub(SEPARATORS, '')).brand.present?
      end

      module_function def scan(value)
        return [] unless value.is_a? String
        value.scan(CARD).flatten.compact.select { |card| valid?(card) }
      end

      module_function def mask(value)
        return value unless value.is_a? String
        scan(value).inject(value) { |acc, elem| acc.gsub(elem, FILTERED) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sensitive_data_filter-0.2.2 lib/sensitive_data_filter/types/credit_card.rb