class UrlValidator < ActiveModel::EachValidator
  SCHEMES = %w(https http)

  def validate_each(record, attribute, value)
    record.errors.add(attribute, :invalid_url, value: value) unless url_valid?(value)
  end

  private

  def url_valid?(url)
    uri = URI.parse(url)
    uri && uri.host && SCHEMES.include?(uri.scheme)
  rescue URI::InvalidURIError
    false
  end
end