credit_card_sanitizer 0.6.7 → 0.7.0

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 +5 -5
  2. data/lib/credit_card_sanitizer.rb +17 -8
  3. metadata +17 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ed76d9a9522df05c6dd170d26ea92106d17a57f3
4
- data.tar.gz: 2d674bd758d3de26a702d7934fecc385c3cf10eb
2
+ SHA256:
3
+ metadata.gz: d8cf23aad6e2adf391430f9aa253458b1015cc1d39f2d937cd44904298e38cb2
4
+ data.tar.gz: 0bf28a2cc585dd331500bf1dc78ac4ff4ae2cbdb5fd8d4f4bd04e6741a2543f4
5
5
  SHA512:
6
- metadata.gz: 6c4c88de4bc7357bb5897ca7f0eb18a472951fd62d63623ca0a37d28b859f3d4891c727cbf44e73920f8bae87f2abca5760532a1ee53f4b4cf89beb9717aa14c
7
- data.tar.gz: b55b8fcdf570998f1c4663d435e260b31962e3522fbf97e1b4230c2cef6809e42767de7657aa44390138586b8c122f9080cb360a20600c5971af93711d412cb5
6
+ metadata.gz: e0a11ad129443d4c1c727d8bd2a09dca6588dbc77a2260c3ffd159b98e65a2e3db3fc9b46fefc388aacfff52fb0e12cf23c18e59a13f7b6ec73cdd9b7ecd8737
7
+ data.tar.gz: 85c14c0b9bcfa716f3d46d4c0a3d78f11cd93781f02d9b166d2f8145285b0fd3073818126f3dd1b7c56eb7e427716071dbd55a1967e8bef01d014e37c1941da8
@@ -44,7 +44,7 @@ class CreditCardSanitizer
44
44
  LINE_NOISE_CHAR = /[^\w\n,()&.\/:;<>]/
45
45
  LINE_NOISE = /#{LINE_NOISE_CHAR}{,5}/
46
46
  NONEMPTY_LINE_NOISE = /#{LINE_NOISE_CHAR}{1,5}/
47
- SCHEME_OR_PLUS = /((?:&#43;|\+)|(?:[a-zA-Z][\-+.a-zA-Z\d]{,9}):[^\s>]+)/
47
+ SCHEME_OR_PLUS = /((?:&#43;|\+|\/)|(?:[a-zA-Z][\-+.a-zA-Z\d]{,9}):[^\s>]+)/
48
48
  NUMBERS_WITH_LINE_NOISE = /#{SCHEME_OR_PLUS}?\d(?:#{LINE_NOISE}\d){10,30}/
49
49
 
50
50
  DEFAULT_OPTIONS = {
@@ -88,14 +88,17 @@ class CreditCardSanitizer
88
88
  # sanitize!("I want all your credit card numbers!")
89
89
  # #=> nil
90
90
  #
91
- # Returns a String of the redacted text if a credit card number was detected.
92
- # Returns nil if no credit card numbers were detected.
91
+ # If options[:return_changes] is false, returns nil if no redaction happened,
92
+ # else the full text after redaction.
93
+ #
94
+ # If options[:return_changes] is true, returns nil if no redaction happened,
95
+ # else an array of [old_text, new_text] indicating what substrings were redacted.
93
96
  def sanitize!(text, options = {})
94
97
  options = @settings.merge(options)
95
98
 
96
99
  text.force_encoding(Encoding::UTF_8)
97
100
  text.scrub!('�')
98
- redacted = nil
101
+ changes = nil
99
102
 
100
103
  without_expiration(text) do
101
104
  text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
@@ -104,15 +107,21 @@ class CreditCardSanitizer
104
107
  candidate = Candidate.new(match, match.tr('^0-9', ''), $`, $')
105
108
 
106
109
  if valid_context?(candidate, options) && valid_numbers?(candidate, options)
107
- redacted = true
108
- redact_numbers(candidate, options)
110
+ redact_numbers(candidate, options).tap do |redacted_text|
111
+ changes ||= []
112
+ changes << [candidate.text, redacted_text]
113
+ end
109
114
  else
110
115
  match
111
116
  end
112
117
  end
113
118
  end
114
119
 
115
- redacted && text
120
+ if options[:return_changes]
121
+ changes
122
+ else
123
+ changes && text
124
+ end
116
125
  end
117
126
 
118
127
  # A proc that can be used
@@ -204,7 +213,7 @@ class CreditCardSanitizer
204
213
  def without_expiration(text)
205
214
  expiration_date_boundary = SecureRandom.hex.tr('0123456789', 'ABCDEFGHIJ')
206
215
  text.gsub!(EXPIRATION_DATE) do |expiration_date|
207
- match = expiration_date.match(/(?<whitespace>\s*)(?<rest>.*)/)
216
+ match = expiration_date.match(/(?<whitespace>\s*)(?<rest>.*)/m)
208
217
  "#{match[:whitespace]}#{expiration_date_boundary}#{match[:rest]}#{expiration_date_boundary}"
209
218
  end
210
219
  yield
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.6.7
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Chapweske
@@ -10,8 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-02-05 00:00:00.000000000 Z
13
+ date: 2022-08-15 00:00:00.000000000 Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: irb
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
15
29
  - !ruby/object:Gem::Dependency
16
30
  name: luhn_checksum
17
31
  requirement: !ruby/object:Gem::Requirement
@@ -67,8 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.6.11
84
+ rubygems_version: 3.1.6
72
85
  signing_key:
73
86
  specification_version: 4
74
87
  summary: Credit card sanitizer