Guest User

Untitled

a guest
Sep 1st, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. How to build a method to validate emails
  2. def email_is_junk(email_address)
  3. end
  4.  
  5. USER_RULES = ['+', 'do-not-reply', 'support', 'test', 'service', 'tips', 'twitter', 'alerts', 'survey']
  6. DOMAIN_RULES = ['craigslist.org']
  7.  
  8. def email_is_junk(email)
  9. return true if !email.match('@') # return early if no @
  10. user, domain = email.split('@')
  11. USER_RULES.each { |rule| return true if user.match(rule) }
  12. DOMAIN_RULES.each { |rule| return true if domain.match(rule) }
  13. false # reached the end without matching anything
  14. end
  15.  
  16. Regexp.union #=> /(?!)/
  17. Regexp.union("penzance") #=> /penzance/
  18. Regexp.union("a+b*c") #=> /a+b*c/
  19. Regexp.union("skiing", "sledding") #=> /skiing|sledding/
  20. Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
  21. Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
  22.  
  23. Regexp.escape('*?{}.') #=> \*?{}.
  24.  
  25. patterns = [
  26. /.+?+.+?@/
  27. ]
  28.  
  29. strings = [
  30. 'do-not-reply', 'support', 'test', 'service', 'tips', 'twitter', 'alerts', 'survey',
  31. 'craigslist.org'
  32. ]
  33.  
  34. regex = Regexp.union(
  35. *patterns,
  36. *strings.map{ |s|
  37. Regexp.new( Regexp.escape("#{ s }@"), Regexp::IGNORECASE ) }
  38. )
  39. pp regex
  40.  
  41. >> /(?-mix:.+?+.+?@)|(?i-mx:do-not-reply@)|(?i-mx:support@)|(?i-mx:test@)|(?i-mx:service@)|(?i-mx:tips@)|(?i-mx:twitter@)|(?i-mx:alerts@)|(?i-mx:survey@)|(?i-mx
  42.  
  43. sample_email_addresses = %w[
  44. user
  45. user+foo
  46. do-not-reply
  47. support
  48. service
  49. tips
  50. twitter
  51. alerts
  52. survey
  53. ].map{ |e| e << '@host.com' }
  54.  
  55. pp sample_email_addresses.map{ |e| [e, !!e[regex]] }
  56.  
  57. >> [["user@host.com", false],
  58. >> ["user+foo@host.com", true],
  59. >> ["do-not-reply@host.com", true],
  60. >> ["support@host.com", true],
  61. >> ["service@host.com", true],
  62. >> ["tips@host.com", true],
  63. >> ["twitter@host.com", true],
  64. >> ["alerts@host.com", true],
  65. >> ["survey@host.com", true]]
  66.  
  67. pp sample_email_addresses.select{ |e| e[regex] }
  68.  
  69. >> ["user+foo@host.com",
  70. >> "do-not-reply@host.com",
  71. >> "support@host.com",
  72. >> "service@host.com",
  73. >> "tips@host.com",
  74. >> "twitter@host.com",
  75. >> "alerts@host.com",
  76. >> "survey@host.com"]
  77.  
  78. pp sample_email_addresses.reject{ |e| e[regex] }
  79.  
  80. >> ["user@host.com"]
  81.  
  82. function isJunk(email) {
  83. return hasPlus(email) || supportLike(email) || craigsList(email);
  84. }
  85.  
  86. function craigsList(email) {
  87. return email.match(/@craigslist.org/);
  88. }
  89.  
  90. function supportLike(email) {
  91. return email.match(/do-not-reply|support|test|service|tips|twitter|alerts|survey/);
  92. }
  93.  
  94. function hasPlus(email) {
  95. return email.match(/+.*@/);
  96. }
Add Comment
Please, Sign In to add comment