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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/credit_card_sanitizer.rb +54 -52
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7edbbbe547cddd637e85333d80f056f69c589f6
4
- data.tar.gz: a65f280f27271a2526538d4b4e7569ef77f8d8aa
3
+ metadata.gz: ad592d6323845db01136562c8c8b272db59aec9b
4
+ data.tar.gz: 58a6d304fb0a6d080fff579e11598800ba9905f1
5
5
  SHA512:
6
- metadata.gz: bceacdd9c61e8a603e077ba269125e24a5435a4825f4a1f7b7f549c32d16ec58f2c712395c62abf1a5665bffc02fb1ef0f82f5bbd815ef946bb20ca660eb8ebe
7
- data.tar.gz: cbc84dcbb32738dfa32cac469c46020b4ad8a605d4d6cbda405e0851d06364af5d284a8c50f4b8fe915bbe09d8a69187da7607ce8e737564dae7dad6a288a7f8
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
- NUMBERS_WITH_LINE_NOISE = /(
6
- \d # starts with a number
7
- [\d|\W]+ # number or non-word character
8
- \d # ends with a number
9
- )/x
10
-
11
- def self.parameter_filter
12
- Proc.new { |_, value| new.sanitize!(value) if value.is_a?(String) }
13
- end
14
-
15
- def initialize(replacement_token='X', replace_first=6, replace_last=4)
16
- @replacement_token = replacement_token
17
- @replace_first = replace_first
18
- @replace_last = replace_last
19
- end
20
-
21
- def sanitize!(text)
22
- replaced = false
23
-
24
- text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
25
- numbers = match.gsub(/\D/, '')
26
-
27
- if valid_length?(numbers) && LuhnChecksum.valid?(numbers)
28
- replaced = true
29
- replace_numbers!(match, numbers)
30
- end
31
-
32
- match
33
- end
34
-
35
- replaced ? text : nil
36
- end
37
-
38
- # From https://en.wikipedia.org/wiki/Primary_Account_Number#Issuer_identification_number_.28IIN.29
39
- def valid_length?(numbers)
40
- numbers.size.between?(13,19)
41
- end
42
-
43
- def replace_numbers!(text, numbers)
44
- # Leave the first @replace_first and last @replace_last numbers visible
45
- replacement_limit = numbers.size - @replace_last
46
- digit_index = 0
47
-
48
- text.gsub!(/\d/) do |number|
49
- digit_index += 1
50
- if digit_index > @replace_first && digit_index <= replacement_limit
51
- @replacement_token
52
- else
53
- number
54
- end
55
- end
56
- end
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.3
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-15 00:00:00.000000000 Z
13
+ date: 2014-07-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appraisal