defvalidate_each(record, attribute, value) if private_ip?(value) record.errors.add(attribute, :is_private_ip) elsif forbidden_host?(value) record.errors.add(attribute, :is_forbidden_host) end end
private
defget_host(value) @host ||= URI(value).host rescuenil end
defprivate_ip?(url) ip_address = IPAddr.new(get_host(url)) rescuenil PRIVATE_IPS.any? {|private_ip| private_ip.include?(ip_address)} if ip_address end
defforbidden_host?(url) host = get_host(url) FORBIDDEN_HOSTS.any? {|forbidden_host| forbidden_host == host.downcase} if host end end
1 2 3 4 5
# web_hook.rb
classWebHook < ActiveRecord::Base validates url, url:true end
raise ArgumentError, "You need to supply at least one attribute"if attributes.empty? raise ArgumentError, "You need to supply at least one validation"if validations.empty?
if validator.respond_to?(:attributes) && !validator.attributes.empty? validator.attributes.each do|attribute| _validators[attribute.to_sym] << validator end else _validators[nil] << validator end