有没有办法在红宝石打开URL和输出重定向的URL:即转换http://bit.ly/l223ue到http://paper.li/CoyDavidsonCRE/1309121465
我发现有更多的网址缩短服务比宝石可以跟上,所以我要求坚硬但强大的方式,而不是使用连接到某些API的gem.
这是一种延长方法
这几乎没有错误处理,但它可能会帮助您开始.你可以用一个返回nil的开始救援块包裹加长,或者稍后尝试重试.不确定你想要建立什么,但希望它有所帮助.
require 'uri'
require 'net/http'
def lengthen(url)
uri = URI(url)
Net::HTTP.new(uri.host, uri.port).get(uri.path).header['location']
end
irb(main):008:0> lengthen('http://bit.ly/l223ue')
=> "http://paper.li/CoyDavidsonCRE/1309121465"
Run Code Online (Sandbox Code Playgroud)