Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alxberardi committed Dec 19, 2016
1 parent 8d64a4b commit 058a780
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 11 additions & 10 deletions lib/sensitive_data_filter/types/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@
module SensitiveDataFilter
module Types
module CreditCard
SEPARATORS = /[ -]/
SEPARATORS = /[\s-]/
SEPRS = SEPARATORS.source + '*'
CARD_16_DIGITS = /\d{4} #{SEPRS} \d{4} #{SEPRS} \d{4} #{SEPRS} \d{4}/
CARD_13_DIGITS = /\d{3} #{SEPRS} \d{3} #{SEPRS} \d{3} #{SEPRS} \d #{SEPRS} \d{3}/
CARD_14_DIGITS = /\d{4} #{SEPRS} \d{6} #{SEPRS} \d{4}/
CARD_15_DIGITS = /\d{4} #{SEPRS} \d{6} #{SEPRS} \d{5}/
CARD = /
(?<!\d)(?:
#{CARD_16_DIGITS.source}
| #{CARD_13_DIGITS.source}
| #{CARD_14_DIGITS.source}
| #{CARD_15_DIGITS.source}
)(?!\d)
/x
(?:
#{CARD_16_DIGITS.source}
| #{CARD_13_DIGITS.source}
| #{CARD_14_DIGITS.source}
| #{CARD_15_DIGITS.source}
)
/x
CATCH_ALL = /((\d#{SEPRS}?){13,16})/
FILTERED = '[FILTERED]'

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

module_function def scan(value)
return [] unless value.is_a? String
value.scan(CARD).select { |card| valid?(card) }
puts value
[CARD, CATCH_ALL].flat_map { |pattern| value.scan(pattern) }.select { |card| valid?(card) }
end

module_function def mask(value)
Expand Down
12 changes: 9 additions & 3 deletions spec/sensitive_data/types/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
'Visa' => ['4111 1111 1111 1111', '4012 8888 8888 1881',
'422 222 222 2 222'],
'Dankort (PBS)' => ['5019 7170 1010 3742'],
'Switch/Solo (Paymentech)' => ['6331 1019 9999 0016']
'Switch/Solo (Paymentech)' => ['6331 1019 9999 0016'],
'Unconventionally Typed' => ['3782 8224 6310 005']
}
}
let(:valid_cards) { example_cards.values.flatten }
Expand Down Expand Up @@ -54,12 +55,17 @@ def validations(cards)

let(:masked_value) {
'This text contains [FILTERED] and [FILTERED], '\
'which are valid credit card numbers, '\
'and 4123 4567 8912 3456, which is not a valid credit card number.'
'which are valid credit card numbers, '\
'and 4123 4567 8912 3456, which is not a valid credit card number.'
}
specify { expect(mask).to eq masked_value }
end

context 'a value that contains valid credit card numbers in a longer numerical pattern' do
let(:value) { '1234111 1111 1111 1111234' }
specify { expect(scan).to eq '4111 1111 1111 1111' }
end

context 'a value that does not contain valid credit card numbers' do
let(:value) { 'This text does not contain credit card values' }
specify { expect(scan).to be_empty }
Expand Down

0 comments on commit 058a780

Please sign in to comment.