PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/cool_games/email.rb

https://github.com/gcao/cool_games
Ruby | 56 lines | 40 code | 3 blank | 13 comment | 1 complexity | f9d6e9e6ace0f057b497ee51487dd3d6 MD5 | raw file
  1. module CoolGames
  2. class Email
  3. # http://tfletcher.com/lib/rfc822.rb
  4. #
  5. # RFC822 Email Address Regex
  6. # --------------------------
  7. #
  8. # Originally written by Cal Henderson
  9. # c.f. http://iamcal.com/publish/articles/php/parsing_email/
  10. #
  11. # Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
  12. #
  13. # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
  14. # http://creativecommons.org/licenses/by-sa/2.5/
  15. #
  16. if RUBY_VERSION.to_f < 1.9
  17. FORMAT = begin
  18. qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
  19. dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
  20. atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
  21. quoted_pair = '\\x5c[\\x00-\\x7f]'
  22. domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
  23. quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
  24. domain_ref = atom
  25. sub_domain = "(?:#{domain_ref}|#{domain_literal})"
  26. word = "(?:#{atom}|#{quoted_string})"
  27. domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
  28. local_part = "#{word}(?:\\x2e#{word})*"
  29. addr_spec = "#{local_part}\\x40#{domain}"
  30. /\A#{addr_spec}\z/
  31. end
  32. else
  33. FORMAT = begin
  34. qtext = '[^\\u000d\\u0022\\u005c\\u0080-\\u00ff]'
  35. dtext = '[^\\u000d\\u005b-\\u005d\\u0080-\\u00ff]'
  36. atom = '[^\\u0000-\\u0020\\u0022\\u0028\\u0029\\u002c\\u002e\\u003a-\\u003c\\u003e\\u0040\\u005b-\\u005d\\u007f-\\u00ff]+'
  37. quoted_pair = '\\u005c[\\u0000-\\u007f]'
  38. domain_literal = "\\u005b(?:#{dtext}|#{quoted_pair})*\\u005d"
  39. quoted_string = "\\u0022(?:#{qtext}|#{quoted_pair})*\\u0022"
  40. domain_ref = atom
  41. sub_domain = "(?:#{domain_ref}|#{domain_literal})"
  42. word = "(?:#{atom}|#{quoted_string})"
  43. domain = "#{sub_domain}(?:\\u002e#{sub_domain})*"
  44. local_part = "#{word}(?:\\u002e#{word})*"
  45. addr_spec = "#{local_part}\\u0040#{domain}"
  46. /\A#{addr_spec}\z/
  47. end
  48. end
  49. def self.valid? email
  50. email =~ FORMAT
  51. end
  52. end
  53. end