Forum: Ruby on Rails URL check URL validity

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5de98ed0903a32c2b4dc5582a87fc291?d=identicon&s=25 jimache (Guest)
on 2007-04-05 04:06
(Received via mailing list)
Hi everyone,

I have a web app that at some point asks for a URL ... and i need to
check that url's validity ... is there a way to do that in RoR...
- in C# i would to something along the lines of

HttpWebRequest hReq =
(HttpWebRequest)WebRequest.Create(url................);

HttpWebResponse hRes = (HttpWebResponse)hReq.GetResponse();
rCode = hRes.get_StatusCode()...etc

Any input is greatly appreciated.


Thank you,
Df376a191d1366036d65167f39a9e1db?d=identicon&s=25 Erik (Guest)
on 2007-04-05 12:55
(Received via mailing list)
I would say a lot depends on what criteria you are using to qualify a
URL as valid.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-04-05 15:01
(Received via mailing list)
On Apr 5, 2007, at 6:53 AM, Erik wrote:
>> HttpWebRequest hReq =
>> (HttpWebRequest)WebRequest.Create(url................);
>>
>> HttpWebResponse hRes = (HttpWebResponse)hReq.GetResponse();
>> rCode = hRes.get_StatusCode()...etc
>>
>> Any input is greatly appreciated.
>>
>> Thank you,

require 'uri'

def valid_uri?
   begin
     myuri = URI.parse(some_string_that_might_be_a_URL)
   rescue URI::InvalidURIError
     return false
   end
   # other checks on myuri.host or myuri.scheme to enforce
application rules
   # return false if myuri.scheme =~ /javascript/i
   # etc.
   return true
end

Something like that?

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.