Last active
December 16, 2015 14:29
-
-
Save JFickel/5449297 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Determine whether a string contains a Social Security number. | |
def has_ssn?(string) | |
if /\d{3}-\d{2}-\d{4}/.match(string) | |
true | |
else | |
false | |
end | |
end | |
# Return the Social Security number from a string. | |
def grab_ssn(string) | |
if /\d{3}-\d{2}-\d{4}/.match(string) | |
m = /\d{3}-\d{2}-\d{4}/.match(string) | |
return m[0] | |
end | |
end | |
# Return all of the Social Security numbers from a string. | |
def grab_all_ssns(string) | |
ssn_array = [] | |
while /\d{3}-\d{2}-\d{4}/.match(string) | |
m = /\d{3}-\d{2}-\d{4}/.match(string) | |
ssn_array << m[0] | |
string = m.post_match | |
end | |
return ssn_array | |
end | |
# Obfuscate all of the Social Security numbers in a string. Example: XXX-XX-4430. | |
def hide_all_ssns(string) | |
if /\d{3}-\d{2}-\d{4}/.match(string) | |
@pusher = "" | |
while /\d{3}-\d{2}-\d{4}/.match(string) | |
m = /\d{3}-\d{2}-\d{4}/.match(string) | |
@pusher << m.pre_match | |
@pusher << m[0].replace("XXX-XX-#{m[0][7,4]}") | |
string = m.post_match | |
if !/\d{3}-\d{2}-\d{4}/.match(string) | |
@pusher << m.post_match | |
end | |
end | |
@pusher | |
else | |
string | |
end | |
end | |
# Ensure all of the Social Security numbers use dashes for delimiters. | |
# Example: 480.01.4430 and 480014430 would both be 480-01-4430. | |
def format_ssns(string) | |
if /\d{9}/.match(string) || /\d{3}.\d{2}.\d{4}/.match(string) | |
@pusher = "" | |
while /\d{9}/.match(string) || /\d{3}.\d{2}.\d{4}/.match(string) | |
m1 = /\d{9}/.match(string) | |
m2 = /\d{3}.\d{2}.\d{4}/.match(string) | |
if m1 && m2 | |
if m1.post_match.length < m2.post_match.length | |
@pusher << m2.pre_match | |
@pusher << m2[0].replace("#{m2[0][0,3]}-#{m2[0][4,2]}-#{m2[0][7,4]}") | |
string = m2.post_match | |
@last_push = m2 | |
elsif m1.post_match.length > m2.post_match.length | |
@pusher << m1.pre_match | |
r1 = m1[0].slice(0,3) | |
r2 = m1[0].slice(3,2) | |
r3 = m1[0].slice(5,4) | |
@pusher << "#{r1}-#{r2}-#{r3}" | |
string = m1.post_match | |
@last_push = m1 | |
end | |
elsif m1 | |
@pusher << m1.pre_match | |
r1 = m1[0].slice(0,3) | |
r2 = m1[0].slice(3,2) | |
r3 = m1[0].slice(5,4) | |
@pusher << "#{r1}-#{r2}-#{r3}" | |
string = m1.post_match | |
@last_push = m1 | |
elsif m2 | |
@pusher << m2.pre_match | |
@pusher << m2[0].replace("#{m2[0][0,3]}-#{m2[0][4,2]}-#{m2[0][7,4]}") | |
string = m2.post_match | |
@last_push = m2 | |
end | |
if !/\d{9}/.match(string) && !/\d{3}.\d{2}.\d{4}/.match(string) | |
@pusher << @last_push.post_match | |
end | |
end | |
@pusher | |
else | |
string | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment