Forum: Ruby on Rails Check whether externam URL is valid?

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.
826fe6a621b575bed1af99c7556e0e57?d=identicon&s=25 Gustav (Guest)
on 2006-03-14 23:32
Hey,

I have users entering URLs into my app, is there anyway in which I can
check to see if the URLs actually point to something before saving the
user input? Maybe a kind of 'ping'-action that returns true on success?

(btw: does anyone of a tentative release date for Rails1.1? I've been
slobbering all over my RailsRecipes book and I can't wait! Also, Scott
Raymond's compiled a list of awesome stuff that'll be incorporated into
the next release. . . link on LoudThinking.Com)


Lots of Thanks,
Gustav
gustav@hamburgerrobot.com
826fe6a621b575bed1af99c7556e0e57?d=identicon&s=25 Gustav (Guest)
on 2006-03-14 23:33
mmm...Externam should read External :) 'suppose you noticed.

Thanks,
Gustav
858392acd1398aa6071fb1d44cc33842?d=identicon&s=25 John Smilanick (Guest)
on 2006-03-14 23:48
(Received via mailing list)
Net::HTTP looks like your best shot.

require 'net/http'
Net::HTTP.start('www.something.com') do |http|
   response = http.get('/index.html')
   p response.code
end

Check acceptable response codes here: http://www.w3.org/Protocols/
rfc2616/rfc2616-sec10.html

-John

--
John Smilanick
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
jsmilani@kitp.ucsb.edu
(805) 893-6307
Aeb5810616aafc577c999d769d3f1bd2?d=identicon&s=25 Clint Pidlubny (Guest)
on 2006-03-15 05:21
(Received via mailing list)
Hey John,

That only works for valid URL's. It doesn't alert you if the URL isn't
there. I'm not sure how to check that?

I've tried your code with a check on the response.

unless response == '200'
  p 'Site is down'
else
  p 'Found it!'
end

I get a "SocketError: getaddrinfo: No address associated with
nodename", which isn't checkable to my knowledge.

Any ideas?
Thanks,
Clint
This topic is locked and can not be replied to.