Created
November 6, 2010 03:42
-
-
Save chsh/665178 to your computer and use it in GitHub Desktop.
Restore original url from the url made from some url shortening services.
This file contains 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
require 'net/http' | |
require 'uri' | |
# Restore original url from the url made from some url shortening services. | |
# | |
# Currently tested: | |
# bit.ly(j.mp), ustre.am, tinyurl | |
class URLExtractor | |
def self.expand_url(short_url) | |
uri = URI.parse(short_url) | |
return nil unless uri.scheme =~ /^https?$/ | |
resp = nil | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
resp = http.head(uri.path) | |
end | |
return nil unless ['301', '302'].include? resp.code | |
resp['location'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment