credit_card_sanitizer 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/credit_card_sanitizer.rb +54 -52
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad592d6323845db01136562c8c8b272db59aec9b
|
4
|
+
data.tar.gz: 58a6d304fb0a6d080fff579e11598800ba9905f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41c5f8ccfb9b9c2c14805a86cee4ed1fd3532958519bf19f9970b584f29be3fbb9d77ae29c0952b7b79a76c73cab645ab5c2e7f07b9d9188100b2adcb9cfae87
|
7
|
+
data.tar.gz: 1f3c2054694c70b1e24679530cdb35ca7c0f7ee1a31e056ab0f2ec00282699fba26f0fc5f0a39dc4c1061fd76aa10ef72f4e7fae3716251bf68bdce42acf35a5
|
@@ -2,56 +2,58 @@ require 'luhn_checksum'
|
|
2
2
|
|
3
3
|
class CreditCardSanitizer
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
5
|
+
# 12-19 digits explanation: https://en.wikipedia.org/wiki/Primary_Account_Number#Issuer_identification_number_.28IIN.29
|
6
|
+
NUMBERS_WITH_LINE_NOISE = /\d(?:\W*\d\W*){10,17}\d/x
|
7
|
+
|
8
|
+
def self.parameter_filter
|
9
|
+
Proc.new { |_, value| new.sanitize!(value) if value.is_a?(String) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(replacement_token = 'X', replace_first = 6, replace_last = 4)
|
13
|
+
@replacement_token, @replace_first, @replace_last = replacement_token, replace_first, replace_last
|
14
|
+
end
|
15
|
+
|
16
|
+
def sanitize!(text)
|
17
|
+
replaced = nil
|
18
|
+
|
19
|
+
text.force_encoding(Encoding::UTF_8)
|
20
|
+
replace_invalid_characters(text) if !text.valid_encoding?
|
21
|
+
|
22
|
+
text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
|
23
|
+
numbers = match.gsub(/\D/, '')
|
24
|
+
|
25
|
+
if LuhnChecksum.valid?(numbers)
|
26
|
+
replaced = true
|
27
|
+
replace_numbers!(match, numbers.size - @replace_last)
|
28
|
+
end
|
29
|
+
|
30
|
+
match
|
31
|
+
end
|
32
|
+
|
33
|
+
replaced && text
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def replace_numbers!(text, replacement_limit)
|
39
|
+
# Leave the first @replace_first and last @replace_last numbers visible
|
40
|
+
digit_index = 0
|
41
|
+
|
42
|
+
text.gsub!(/\d/) do |number|
|
43
|
+
digit_index += 1
|
44
|
+
if digit_index > @replace_first && digit_index <= replacement_limit
|
45
|
+
@replacement_token
|
46
|
+
else
|
47
|
+
number
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def replace_invalid_characters(str)
|
53
|
+
for i in (0...str.size)
|
54
|
+
if !str[i].valid_encoding?
|
55
|
+
str[i] = "?"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
57
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: credit_card_sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Chapweske
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-07-
|
13
|
+
date: 2014-07-23 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: appraisal
|