Guest User

Untitled

a guest
Sep 1st, 2018
216
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. >> [["[email protected]", false],
  58. >> ["[email protected]", true],
  59. >> ["[email protected]", true],
  60. >> ["[email protected]", true],
  61. >> ["[email protected]", true],
  62. >> ["[email protected]", true],
  63. >> ["[email protected]", true],
  64. >> ["[email protected]", true],
  65. >> ["[email protected]", true]]
  66.  
  67. pp sample_email_addresses.select{ |e| e[regex] }
  68.  
  69.  
  70. pp sample_email_addresses.reject{ |e| e[regex] }
  71.  
  72.  
  73. function isJunk(email) {
  74. return hasPlus(email) || supportLike(email) || craigsList(email);
  75. }
  76.  
  77. function craigsList(email) {
  78. return email.match(/@craigslist.org/);
  79. }
  80.  
  81. function supportLike(email) {
  82. return email.match(/do-not-reply|support|test|service|tips|twitter|alerts|survey/);
  83. }
  84.  
  85. function hasPlus(email) {
  86. return email.match(/+.*@/);
  87. }
Add Comment
Please, Sign In to add comment