RubyEmail/lib/ruby_email/rfc5322.rb

38 lines
1.1 KiB
Ruby
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

require_relative 'core'
require_relative 'rfc1123'
module RubyEmail
# an Email Address follows the Rfc5322: http://www.ietf.org/rfc/rfc5322.txt
# a Domain Name follows the Rfc1123: https://tools.ietf.org/html/rfc1123
# The Rfc5322 designate an email with the format local@domain, where
# - local is a string of US-ASCII characters, without some symboles.
# - domain is a valid Domain Name
class Rfc5322 < Core
# one valid character for local part (do not take escaped because the Rfc prefere to avoid them)
ATEXT = '([A-Za-z0-9!#\$%&\'*\+\-/=\?\^_`\{\}\|~])'
# a valid string for local part
ATOM = "#{ATEXT}+"
# a valid string with subdomains, separated by dots for local part
DOT_ATOM_TEXT = "(#{ATOM})(\\.#{ATOM})*"
# email grammar
VALIDE = "(?<local>#{DOT_ATOM_TEXT})@(#{Rfc1123::VALIDE})"
# regexp to validate complete email
REGEXP = Regexp.new "\\A#{VALIDE}\\Z"
module String
# Check if the current [::String] instance is a valid email
# @return [TrueClass or FalseClass]
def is_email?
RubyEmail::Rfc5322.validates? self
end
end
end
end