Sha256: b9870578de194c48d497f31db050e18ce31f21274b5790305b53632ab57a2300

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module EmailInquire

  # Relevant literature:
  # http://emailregex.com/email-validation-summary/
  # http://www.regular-expressions.info/email.html
  class EmailValidator

    def initialize(email)
      @email = email.downcase
    end

    attr_reader :email

    def valid?
      return false unless email.count("@") == 1
      return false if email.length > 255
      name, domain = email.split("@", 2)

      name_valid?(name) && domain_valid?(domain)
    end

    private

    DOMAIN_REGEXP = /
      \A
      (?:
        (?=
          [a-z0-9-]{1,63}
          \.
        )
        [a-z0-9]+
        (?:
          -
          [a-z0-9]+
        )*
        \.
      ){1,8}
      [a-z]{2,63}
      \z
    /x

    def domain_valid?(domain)
      return false if domain == ""
      return false unless domain =~ DOMAIN_REGEXP

      true
    end

    NAME_REGEXP = /
      \A
      [a-z0-9]
      [a-z0-9._%+-]{0,63}
      \z
    /x

    def name_valid?(name)
      return false if name == ""
      return false if name.length > 64
      return false unless name =~ NAME_REGEXP

      true
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
email_inquire-0.6.0 lib/email_inquire/email_validator.rb