Class: DeliverableValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/truelist/validators/deliverable_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/truelist/validators/deliverable_validator.rb', line 4

def validate_each(record, attribute, value)
  return if value.blank?

  result = Truelist::Client.new.validate(value)

  allow_risky = if options.key?(:allow_risky)
                  options[:allow_risky]
                else
                  Truelist.configuration.allow_risky
                end

  # Fail open for transient errors (timeouts, 500s) so forms still work when API is down
  return if result.error?

  deliverable = result.state == 'ok' || (result.state == 'accept_all' && allow_risky) || result.unknown?

  return if deliverable

  message = options[:message] || :invalid_email
  record.errors.add(attribute, message)
end