Class: Remi::Transform::ValidateEmail

Inherits:
Remi::Transform show all
Defined in:
lib/remi/transform.rb

Overview

Public: Checks to see if an email validates against a regex (imperfect) and will substitute it with some value if not.

substitute - The value used to substitute for an invalid email. Can use a proc

that accepts the value of the invalid email

Examples:

ValidateEmail.new('[email protected]').to_proc.call('uhave.email') #=> '[email protected]'
ValidateEmail.new(->(v) { "#{SecureRandom.uuid}@example.com" }).to_proc.call('uhave.email') #=> '[email protected]'

Instance Attribute Summary

Attributes inherited from Remi::Transform

#multi_args, #source_metadata, #target_metadata

Instance Method Summary collapse

Methods inherited from Remi::Transform

#call, #to_proc

Constructor Details

#initialize(substitute = '', *args, **kargs, &block) ⇒ ValidateEmail

Returns a new instance of ValidateEmail.



446
447
448
449
# File 'lib/remi/transform.rb', line 446

def initialize(substitute='', *args, **kargs, &block)
  super
  @substitute   = substitute
end

Instance Method Details

#transform(value) ⇒ Object



451
452
453
454
455
456
457
458
459
460
# File 'lib/remi/transform.rb', line 451

def transform(value)
  value = value || ''
  if value.match(/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,}$/i)
    value
  elsif @substitute.respond_to? :call
    @substitute.call value
  else
    @substitute
  end
end