Ruby Link Checker

This code checks if the given link exists and returns an HTTP Code 200.


def self.link_ok? link
  url = URI.parse( link )
  req = Net::HTTP.new(url.host, url.port)
  req.use_ssl = true
  req.verify_mode = OpenSSL::SSL::VERIFY_NONE
  res = req.request_head(url.path)
  res.code.to_i == 200
rescue => e
  p "ERROR #{e.message}"
  return false
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s