Assuming that you want to only validate the URLs
pointing to image type of files.
For eg: http://www.xyz.com/logo.png
or http://www.xyz.com/logo.gif
or http://www.xyz.com/logo.jpg
Here's the solution:
require 'uri'
class Images
IMAGE_NOT_FOUND = '/images/no_image.gif'
attr_accessor :url
def initialize(parameters = {})
@url = parameters.fetch(:url, IMAGE_NOT_FOUND)
@url = IMAGE_NOT_FOUND unless ((File.extname(@url) =~/^(.png|.gif|.jpg)$/ )||(@url =~ /^#{URI::regexp}$/))
end
end
The above example will only allow url's pointing to images with extension .png or .gif or .jpg
and rest would be set to IMAGE_NOT_FOUND
. Add more extensions if you want to.