class Spieker::LinkValidator LOCAL_LINK_REGEX = /^(?!(http(s)?\:|\/\/)).*/ def initialize(link, origin) @link = link @origin = origin end def valid? assertions.all? { |assert| send(assert) } end private def assertions private_methods.select { |m| m.to_s.start_with?('assert_') } end def assert_is_local link =~ LOCAL_LINK_REGEX || begin URI.parse(link).hostname == origin.hostname rescue false end end def assert_is_not_email link !~ /mailto/ end def assert_is_not_javascript link !~ /javascript/ end def assert_is_not_pdf link !~ /\bpdf\b/ end def assert_is_not_data link !~ /^data/ end def link @link end def origin URI.parse @origin.start_with?('http') ? @origin : 'http://' + @origin end end