Skip to content

Instantly share code, notes, and snippets.

@yswallow
Created November 17, 2012 11:52
Show Gist options
  • Save yswallow/4095237 to your computer and use it in GitHub Desktop.
Save yswallow/4095237 to your computer and use it in GitHub Desktop.
短縮URLの展開
require 'net/http'
require 'uri'
def expand_url(url)
uri = url.kind_of?(URI) ? url : URI.parse(url)
begin
Net::HTTP.start(uri.host, uri.port) do |io|
r = io.head(uri.path)
r['Location'] ? expand_url(r['Location']) : uri.to_s
end
rescue
uri.to_s
end
end
p expand_url("http://j.mp/fNiW4v") #=>http://www.gehirn.co.jp/
@yswallow
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment