Skip to content

Instantly share code, notes, and snippets.

@chsh
Created November 6, 2010 03:42
Show Gist options
  • Save chsh/665178 to your computer and use it in GitHub Desktop.
Save chsh/665178 to your computer and use it in GitHub Desktop.
Restore original url from the url made from some url shortening services.
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