credit_card_sanitizer 0.3.7 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/credit_card_sanitizer.rb +51 -7
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a78eb4b9bd51d648be7cdba8808e4f75f394bfd
4
- data.tar.gz: 96ae7f4310a4c128e9949c4fac1855bca685cd61
3
+ metadata.gz: 98bf84c263cd302f4adb4cad41dbde01d048d979
4
+ data.tar.gz: 38ce9eb0baa8c4882bb100bbd5cd6ddfe402c99b
5
5
  SHA512:
6
- metadata.gz: 1df0d6ac8ddd3eab00def435e4b44db0816d3b20ac7d7bab51c3e931d7534c3a7c69704f15a6dbf546b41ee2243e0411ceedb875e12cceb64293c5c466289885
7
- data.tar.gz: 4811dcc2cc3b426a659afd1f4bcf088724416a107fab9aea145e40f5531163f13da8e95ad5d2b5bde319fd148fdaf1166d69f264c6abba7697b71e7fec1b7294
6
+ metadata.gz: 0eca7811cd6c6c822e1d6c3bf3ba7a00af4fd61f099da469b1b130344067885b4a27ec0c67116852113a289333a7827308882aead6a4dba4399d3e5e7b1acffa
7
+ data.tar.gz: e214f6777c5f0fa56e3fcbd6af818a63964570d2f5c7c9dff6bdc729b9ecce77d24def686bbc6c6a2a6241741f733d4301d842cf79d63d741b9d583caf10e57c
@@ -21,13 +21,31 @@ class CreditCardSanitizer
21
21
  'forbrugsforeningen' => /^600722\d{10}$/,
22
22
  'laser' => /^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/
23
23
  }
24
+
25
+ CARD_NUMBER_GROUPINGS = {
26
+ 'visa' => [[4, 4, 4, 4]],
27
+ 'master' => [[4, 4, 4, 4]],
28
+ 'discover' => [[4, 4, 4, 4]],
29
+ 'american_express' => [[4, 6, 5]],
30
+ 'diners_club' => [[4, 6, 4]],
31
+ 'jcb' => [[4, 4, 4, 4]],
32
+ 'switch' => [[4, 4, 4, 4]],
33
+ 'solo' => [[4, 4, 4, 4]],
34
+ 'dankort' => [[4, 4, 4, 4]],
35
+ 'maestro' => [[4], [5]],
36
+ 'forbrugsforeningen' => [[4, 4, 4, 4]],
37
+ 'laser' => [[4, 4, 4, 4]]
38
+ }
39
+
24
40
  VALID_COMPANY_PREFIXES = Regexp.union(*CARD_COMPANIES.values)
25
41
  EXPIRATION_DATE = /\s(?:0?[1-9]|1[0-2])(?:\/|-)(?:\d{4}|\d{2})(?:\s|$)/
26
- LINE_NOISE = /[^\w_\n,()\/:;<>]{,5}/
42
+ LINE_NOISE_CHAR = /[^\w_\n,()\/:;<>]/
43
+ LINE_NOISE = /#{LINE_NOISE_CHAR}{,5}/
44
+ NONEMPTY_LINE_NOISE = /#{LINE_NOISE_CHAR}{1,5}/
27
45
  SCHEME_OR_PLUS = /((?:&#43;|\+)|(?:[a-zA-Z][\-+.a-zA-Z\d]{,9}):\S+)/
28
46
  NUMBERS_WITH_LINE_NOISE = /#{SCHEME_OR_PLUS}?\d(?:#{LINE_NOISE}\d){10,18}/
29
47
 
30
- attr_reader :replacement_token, :expose_first, :expose_last
48
+ attr_reader :replacement_token, :expose_first, :expose_last, :use_groupings
31
49
 
32
50
  # Create a new CreditCardSanitizer
33
51
  #
@@ -41,6 +59,7 @@ class CreditCardSanitizer
41
59
  @replacement_token = options.fetch(:replacement_token, '▇')
42
60
  @expose_first = options.fetch(:expose_first, 6)
43
61
  @expose_last = options.fetch(:expose_last, 4)
62
+ @use_groupings = options.fetch(:use_groupings, false)
44
63
  end
45
64
 
46
65
  # Finds credit card numbers and redacts digits from them
@@ -68,14 +87,16 @@ class CreditCardSanitizer
68
87
  without_expiration(text) do
69
88
  text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
70
89
  next match if $1
90
+
91
+ @match = match
71
92
  @numbers = match.tr('^0-9', '')
72
93
 
73
94
  if valid_numbers?
74
95
  redacted = true
75
- redact_numbers!(match)
96
+ redact_numbers!
76
97
  end
77
98
 
78
- match
99
+ @match
79
100
  end
80
101
  end
81
102
 
@@ -109,12 +130,35 @@ class CreditCardSanitizer
109
130
  !!(numbers =~ VALID_COMPANY_PREFIXES)
110
131
  end
111
132
 
133
+ def find_company
134
+ CARD_COMPANIES.each do |company, pattern|
135
+ return company if @numbers =~ pattern
136
+ end
137
+ end
138
+
139
+ def valid_grouping?
140
+ if use_groupings
141
+ if company = find_company
142
+ groupings = @match.split(NONEMPTY_LINE_NOISE).map(&:length)
143
+ return true if groupings.length == 1
144
+ if company_groupings = CARD_NUMBER_GROUPINGS[company]
145
+ company_groupings.each do |company_grouping|
146
+ return true if groupings.take(company_grouping.length) == company_grouping
147
+ end
148
+ end
149
+ end
150
+ false
151
+ else
152
+ true
153
+ end
154
+ end
155
+
112
156
  def valid_numbers?
113
- LuhnChecksum.valid?(@numbers) && valid_prefix?(@numbers)
157
+ LuhnChecksum.valid?(@numbers) && valid_prefix?(@numbers) && valid_grouping?
114
158
  end
115
159
 
116
- def redact_numbers!(text)
117
- text.gsub!(/\d/).with_index do |number, digit_index|
160
+ def redact_numbers!
161
+ @match.gsub!(/\d/).with_index do |number, digit_index|
118
162
  if within_redaction_range?(digit_index)
119
163
  replacement_token
120
164
  else
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.3.7
4
+ version: 0.4.0
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: 2015-11-10 00:00:00.000000000 Z
13
+ date: 2016-01-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appraisal
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  requirements: []
112
112
  rubyforge_project:
113
- rubygems_version: 2.4.8
113
+ rubygems_version: 2.4.5.1
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Credit card sanitizer