Class: Maskerade::CreditCardMasker

Inherits:
Object
  • Object
show all
Defined in:
lib/maskerade/credit_card_masker.rb

Constant Summary collapse

COMPANY_CARD_REGEX =
{
  :visa => /\b4\d{3}[ -]?\d{4}[ -]?\d{4}[ -]?(?:\d|\d{4}|\d{7})\b/,
  :amex => /\b3[47]\d{2}[ -]?\d{6}[ -]?\d{5}\b/,
  :diners_club => /\b3(?:0[0-5]|[68]\d)\d[ -]?\d{6}[ -]?(?:\d{4}|\d{9})\b/,
  :mastercard => /\b(?:5[1-5]\d{2}[ -]?\d{2}|6771[ -]?89|222[1-9][ -]?\d{2}|22[3-9]\d[ -]?\d{2}|2[3-6]\d{2}[ -]?\d{2}|27[01]\d[ -]?\d{2}|2720[ -]?\d{2})\d{2}[ -]?\d{4}[ -]?\d{4}\b/,
  :discover => /\b(?:6011|65\d{2}|64[4-9]\d)[ -]?\d{4}[ -]?\d{4}[ -]?(?:\d{4}|\d{7})\b/,
  :union_pay => /\b62\d{2}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}\b/,
  :jcb => /\b(?:308[89]|309[0-4]|309[6-9]|310[0-2]|311[2-9]|3120|315[8-9]|333[7-9]|334[0-9]|352[89]|35[3-8]\d)[ -]?\d{4}[ -]?\d{4}[ -]?(?:\d{4}|\d{7})\b/,
  :switch_short_iin => /\b(?:4903|4905|4911|4936|6333|6759)[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}(?:\d{2,3})?\b/,
  :switch_long_iin => /\b(?:5641[ -]?82|6331[ -]?10)\d{2}[ -]?\d{4}[ -]?\d{4}(?:\d{2,3})?\b/,
  :solo => /\b(?:6334|6767)[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}(?:\d{2,3})?\b/,
  :dankort => /\b5019[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}\b/,
  :maestro => /\b(?:5[06-8]|6\d)\d{2}[ -]?\d{4}[ -]?\d{4}[ -]?\d{0,7}\b$/,
  :forbrugsforeningen => /\b6007[ -]?22\d{2}[ -]?\d{4}[ -]?\d{4}\b/,
  :laser => /\b(?:6304|6706|6709|6771(?!89))[ -]?\d{4}[ -]?\d{4}[ -]?(?:\d{4}|\d{6,7})?\b/
}
ALL_CARDS_REGEX =
::Regexp.union(COMPANY_CARD_REGEX.values)
DIGIT_REGEX =
/[[:digit:]]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(replacement_text: nil, replacement_token: "X", expose_last: 0) ⇒ CreditCardMasker

Options:

replacement_text: The entire card number gets replaced with this text; if set, the other two options are ignored.
replacement_token: The character used to replace digits of the credit number.  Defaults to "X".
expose_last: The number of trailing digits of the credit card number to leave intact.  Defaults to 0.


33
34
35
36
37
# File 'lib/maskerade/credit_card_masker.rb', line 33

def initialize(replacement_text: nil, replacement_token: "X", expose_last: 0)
  @replacement_text = replacement_text
  @replacement_token = replacement_token
  @expose_last = expose_last
end

Instance Attribute Details

#expose_lastObject (readonly)

Returns the value of attribute expose_last.



7
8
9
# File 'lib/maskerade/credit_card_masker.rb', line 7

def expose_last
  @expose_last
end

#replacement_tokenObject (readonly)

Returns the value of attribute replacement_token.



7
8
9
# File 'lib/maskerade/credit_card_masker.rb', line 7

def replacement_token
  @replacement_token
end

Instance Method Details

#mask(value) ⇒ Object



39
40
41
42
43
# File 'lib/maskerade/credit_card_masker.rb', line 39

def mask(value)
  value&.gsub(ALL_CARDS_REGEX) do |match|
    mask_one(match)
  end
end