Skip to content

Instantly share code, notes, and snippets.

@chuckg
Created May 2, 2013 23:38
Show Gist options
  • Save chuckg/5506275 to your computer and use it in GitHub Desktop.
Save chuckg/5506275 to your computer and use it in GitHub Desktop.
Parse URL to see if it's an image
class UriParser
def self.uri_is_image?(uri)
case File.extname(uri.path)
when '.jpg', '.png', '.gif'
return true
else
return false
end
end
end
describe UriParser
describe '.uri_is_image?' do
it 'returns true when the URL is a jpg' do
uri = URI('http://www.chuckg.org/images/logo.jpg')
Scraper.send(:uri_is_image?, uri).should be_true
end
it 'returns true when the URL is a png' do
uri = URI('http://www.chuckg.org/images/logo.png')
Scraper.send(:uri_is_image?, uri).should be_true
end
it 'returns true when the URL is a gif' do
uri = URI('http://www.chuckg.org/images/logo.gif')
Scraper.send(:uri_is_image?, uri).should be_true
end
it 'returns false when the URL is not an image' do
uri = URI('http://www.chuckg.org/blog/blah/blahjpg?test=true')
Scraper.send(:uri_is_image?, uri).should be_false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment