class GuardValidator

Constants

METHOD_NAMES

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/guard_validator.rb, line 12
def validate_each(record, attribute, value)
  image_path = fetch_image_path(record, attribute)
  return if valid?(image_path)
  record.errors.add(attribute, @message, options.merge(value: value))
end

Private Instance Methods

face_recognised?(result) click to toggle source
# File lib/guard_validator.rb, line 63
def face_recognised?(result)
  return true if options[:face_detection] && result[:face_recognised]
  @message = 'Face could not be recognised on given picture.'
  false
end
fetch_image_path(record, attribute) click to toggle source
# File lib/guard_validator.rb, line 20
def fetch_image_path(record, attribute)
  arr = [attribute].push(*Array(fetch_method_names))
  arr.inject(record, :public_send)
end
fetch_method_names() click to toggle source
# File lib/guard_validator.rb, line 25
def fetch_method_names
  options[:tool].present? ? METHOD_NAMES.fetch(options[:tool]) : options[:method_name]
end
path_exists?(image_path) click to toggle source
# File lib/guard_validator.rb, line 57
def path_exists?(image_path)
  return true if image_path && File.exist?(image_path)
  @message = 'Picture doesn\'t exist.'
  false
end
safety_violated?(result) click to toggle source
# File lib/guard_validator.rb, line 51
def safety_violated?(result)
  return false if (!result[:safe_search][:adult] && !result[:safe_search][:violence])
  @message = 'Picture shows inappropriate content.'
  true
end
valid?(image_path) click to toggle source
# File lib/guard_validator.rb, line 29
def valid?(image_path)
  return false unless path_exists?(image_path)
  result = Picguard.analyze(
    image_path:
      image_path,
    safe_search:
      options[:safe_search] || false,
    face_detection:
      options[:face_detection] || false,
    threshold_adult:
      options[:threshold_adult] || Picguard.configuration.threshold_adult,
    threshold_violence:
      options[:threshold_violence] || Picguard.configuration.threshold_violence,
    threshold_face:
      options[:threshold_face] || Picguard.configuration.threshold_face,
  )

  return false if options[:safe_search] && safety_violated?(result)
  return false if options[:face_detection] && !face_recognised?(result)
  true
end