Ruby: Extend URL

Is there a way to open URLS in ruby ​​and output the redirected URL: i.e. convert http://bit.ly/l223ue to http://paper.li/CoyDavidsonCRE/1309121465

I find that there are more url shortening services than gems that can keep up, so I ask for a tough but reliable one, instead of using a gem that connects to some API.

+4
source share
1 answer

Here is a long method

This has very little error handling, but it can help you get started. You can wrap the extension with a disaster recovery unit that returns zero or will try again later. Not sure what you are trying to build, but hope this helps.

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" 
+6
source

Source: https://habr.com/ru/post/1396771/


All Articles