Created
May 2, 2013 23:38
-
-
Save chuckg/5506275 to your computer and use it in GitHub Desktop.
Parse URL to see if it's an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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