Sha256: 58ed4d02bc9a7071e98dc784890ce9cbc1a151c8afdb77b0f7a39a8d872aca25

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 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

2 entries across 2 versions & 1 rubygems

Version Path
email_inquire-0.8.0 lib/email_inquire/email_validator.rb
email_inquire-0.7.0 lib/email_inquire/email_validator.rb