Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. require 'pry'
  2.  
  3. # Determine whether a string contains a SIN (Social Insurance Number).
  4. # A SIN is 9 digits and we are assuming that they must have dashes in them
  5.  
  6.  
  7. def has_sin?(string)
  8. string.match(/\b\d{3}-\d{3}-\d{3}\b/) != nil
  9. end
  10.  
  11. puts "has_sin? returns true if it has what looks like a SIN"
  12. puts has_sin?("please don't share this: 234-604-142") == true
  13. puts "has_sin? returns false if it doesn't have a SIN"
  14. puts has_sin?("please confirm your identity: XXX-XXX-142") == false
  15. puts has_sin?("please don't share this: 234-6043-142") == false
  16.  
  17. puts has_sin?("please don't share this: 2342-604-142") == false
  18. puts has_sin?("please don't share this: 234-604-1421") == false
  19.  
  20. # Return the Social Insurance Number from a string.
  21.  
  22. def grab_sin(string)
  23. match = string.match(/\b\d{3}-\d{3}-\d{3}\b/)
  24. if match
  25. string.match(/\d{3}-\d{3}-\d{3}/)[0]
  26. else
  27. nil
  28. end
  29. end
  30.  
  31. puts "grab_sin returns an SIN if the string has an SIN"
  32. puts grab_sin("please don't share this: 234-604-142") == "234-604-142"
  33. puts "grab_sin returns nil if it doesn't have a SIN"
  34. puts grab_sin("please confirm your identity: XXX-XXX-142") == nil
  35.  
  36.  
  37. # Return all of the SINs from a string, not just one.
  38.  
  39. def grab_all_sins(string)
  40. string.scan(/\d{3}\D?\d{3}\D?\d{3}\d?/)
  41. end
  42.  
  43. puts "grab_all_sins returns all SINs if the string has any SINs"
  44. puts grab_all_sins("234-604-142, 350-802-074, 013-630-876") == ["234-604-142", "350-802-074", "013-630-876"]
  45.  
  46. puts "grab_all_sins returns an empty Array if it doesn't have any SINs"
  47. puts grab_all_sins("please confirm your identity: XXX-XXX-142") == []
  48.  
  49.  
  50. # Obfuscate all of the Social Insurance numbers in a string. Example: XXX-XX-4430.
  51.  
  52. def hide_all_sins(string)
  53. string.gsub((/\d{3}-\d{3}/), "XXX-XXX")
  54. end
  55.  
  56. puts "hide_all_sins obfuscates any SINs in the string"
  57. puts hide_all_sins("234-601-142, 350-801-074, 013-601-876") == "XXX-XXX-142, XXX-XXX-074, XXX-XXX-876"
  58.  
  59. puts "hide_all_sins does not alter a string without SINs in it"
  60. string = "please confirm your identity: XXX-XXX-142"
  61. puts hide_all_sins(string) == string
  62.  
  63. # Ensure all of the Social Insurance numbers use dashes for delimiters.
  64. # Example: 480.01.4430 and 480014430 would both be 480-01-4430.
  65.  
  66. def format_sins(string)
  67. if has_sin?(string)
  68. sins_list = grab_all_sins(string).map do |a|
  69. a.gsub!(".", "")
  70. a.gsub!("-", "")
  71. a = "#{a[0..2]}-#{a[3..5]}-#{a[6..8]}"
  72. end
  73. sins_list.join(", ")
  74. else
  75. string
  76. end
  77. # string.gsub(("/\d{3}\D?\d{3}\D?\d{3}\d?/"), ("/\d{3}-\d{3}-\d{3}/"))
  78. end
  79.  
  80. puts "format_sins finds and reformat any SINs in the string"
  81. puts format_sins("234600142, 350.800.074, 013-600-876") == "234-600-142, 350-800-074, 013-600-876"
  82.  
  83. puts "format_sins does not alter a string without SINs in it"
  84. string = "please confirm your identity: 4421422"
  85. puts format_sins(string) == string
  86.  
  87. # string = "please confirm your identity: 123abc445"
  88. # puts format_sins(string) == string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement