V
V
Viktor Vsk2015-01-04 02:17:47
ruby
Viktor Vsk, 2015-01-04 02:17:47

How to encode only unencoded URLs?

URLs from different sources can be in different forms: somewhere they are already encoded, somewhere they still need to be encoded.
I use

def urlencode(url)
  url = begin
    url = URI(url)
    url
  rescue URI::InvalidURIError
     URI.encode(url)
  end
end

Are there better ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily, 2015-01-10
@viktorvsk

Always decode the URL first and then encode again. Decoding an unencoded URL is a non-modifying operation and will not make any changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question