Sha256: d37dda73b41c492b1a79055b0e2e9be7c83e7577801858f30fd34e9663576dd7

Contents?: true

Size: 824 Bytes

Versions: 2

Compression:

Stored size: 824 Bytes

Contents

module Validator
  class Email
    attr_accessor :domain

    # Returns the local part (the left hand side of the @ sign in the email address) of the address
    #  a = Email.new('vitaliy@gmail.com')
    #  a.local #=> 'vitaliy'
    attr_accessor :local

    LOCAL_LENGTH = 64 
    
    def initialize(value)
      @value = value
      self.parse
    end

    def parse
      @local, @domain = @value.split(/@/)
    end

    def is_email?
      @value.split(/@/).size == 2
    end

    def valid_by_local_length?(local_length = nil)
      @local.length <= (local_length || LOCAL_LENGTH)
    end

    def valid_by_regexp?
      @value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
    end

    # valid if passes all conditions
    def valid?
      is_email? and valid_by_local_length? and valid_by_regexp?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
validator-0.1.1 lib/validator/email.rb
validator-0.1.0 lib/validator/email.rb