Question

I'm using httparty to unshorten short URIs and I happened upon:

HTTParty.get('http://bit.ly/19NoFfn', limit: 50 )

which when expanded yields:

https://sublime.wbond.net/packages/PhpSpec Snippets

which obviously throws a: URI::InvalidURIError.

Would it be possible to pass some parameter to httparty so that it would automatically try to encode URIs before trying to follow them?

Was it helpful?

Solution

I sort of solved my issue:

def unshorten(uri)
  begin
    response = HTTParty.get(uri, limit: 50)
  rescue URI::InvalidURIError => error
    bad_uri = error.message.match(/^bad\sURI\(is\snot\sURI\?\)\:\s(.*)$/)[1]
    good_uri = URI.encode bad_uri
    response = self.unshorten good_uri
  end
  response
end

I don't feel particularly comfortable fetching the URI from the error message string but it seems there's no other way. Or is there? :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top