Sha256: c9e11e19c85092359c87cafd719b9397bc69c9d22178d05744fd8e9c23b8bcd6

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# encoding: UTF-8
require 'mail'

class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if value.blank?
    begin
      # http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
      m = Mail::Address.new(value)
      # We must check that value contains a domain and that value is an email address
      r = m.domain && m.address == value
      t = m.__send__(:tree)
      # We need to dig into treetop
      # A valid domain must have dot_atom_text elements size > 1
      # user@localhost is excluded
      # treetop must respond to domain
      # We exclude valid email values like <user@localhost.com>
      # Hence we use m.__send__(tree).domain
      r &&= (t.domain.dot_atom_text.elements.size > 1)
    rescue Exception => e   
      r = false
    end
    record.errors[attribute] << (options[:message] || 'inválido') unless r
  end
end

class IncorrectEmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    domains = %w(
      hotmail.com.br gmail.com.br
      hotamil.com hotimail.com hotmail.com.br hotmail.com.com hotmail.con hotmal.com hoymail.com hotmil.com
      gamil.com gmail.com.br gmal.com gmeil.com.br gmial.com
      teste.com teste.com.br
      yaoo.com
      .com.be
    )
    domains.each do |d|
      if value && value.include?("@#{d}")
        record.errors[attribute] << 'está incorreto'
        break
      end
    end
  end
end

class DeniedEmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    domains = %w(mailinator.com dodgit.com uggsrock.com spambox.us spamhole.com spam.la trashymail.com guerrillamailblock.com spamspot.com spamfree tempomail.fr jetable.net maileater.com meltmail.com)
    domains.each do |d|
      if value && value.include?("@#{d}")
        record.errors[attribute] << 'inválido'
        break
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
campagne-0.1.2 app/validators/email_validator.rb
campagne-0.1.1 app/validators/email_validator.rb
campagne-0.1.0 app/validators/email_validator.rb
campagne-0.0.2 app/validators/email_validator.rb