Created
May 7, 2018 13:00
-
-
Save cored/bb3f682da0a2b324872e31ea7a8b4c36 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
require "minitest/autorun" | |
module PI | |
extend self | |
def call(str) | |
return mask_email(str) if str =~ /@/ | |
mask_phone(str) | |
end | |
def mask_email(str) | |
email_parts = str.downcase.scan(/(^\w{1})(\w*)(.+)(@)(\w+)(.)(\w+)/).flatten | |
email_parts[1] = "*" * 5 | |
email_parts.join | |
end | |
def mask_phone(str) | |
normalize_phone= str.gsub(/[\(|\)|\-]/, '') | |
if normalize_phone.size == 10 | |
phone_parts = normalize_phone.scan(/(\d{3})(\d{3})(\d{4})/).flatten | |
phone_parts[0] = replace_number_with_asterisk_for(phone_parts[0]) | |
phone_parts[1] = replace_number_with_asterisk_for(phone_parts[1]) | |
phone_parts.join('-') | |
else | |
phone_parts = normalize_phone.scan(/(\d{2})(\d{3})(\d{3})(\d{4})/).flatten | |
0.upto(2).each do |idx| | |
phone_parts[idx] = replace_number_with_asterisk_for(phone_parts[idx]) | |
end | |
"+#{phone_parts.join('-')}" | |
end | |
end | |
def replace_number_with_asterisk_for(str) | |
str.gsub(/./, '*') | |
end | |
end | |
describe PI do | |
describe "when passing emails" do | |
it "replace all letters in the first name to asterisks and lowercase everything" do | |
PI.("[email protected]").must_equal "n*****[email protected]" | |
PI.("[email protected]").must_equal "l*****[email protected]" | |
PI.("[email protected]").must_equal "a*****[email protected]" | |
end | |
it "masks phone numbers" do | |
PI.("1(234)567-890").must_equal "***-***-7890" | |
PI.("86-(10)12345678").must_equal "+**-***-***-5678" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment