Created
November 17, 2012 11:52
-
-
Save yswallow/4095237 to your computer and use it in GitHub Desktop.
短縮URLの展開
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
| 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/ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考(というかほぼこれ): http://blog.sorah.jp/2011/01/03/ruby-url-expand-is-strange