Created
August 20, 2014 08:30
-
-
Save clemens/3a34d74e8544926705e1 to your computer and use it in GitHub Desktop.
This file contains 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
url = 'http://example.com/ ?api_token=a-token&username=USERNAME' # => "http://example.com/ ?api_token=a-token&username=USERNAME" | |
url =~ URI.regexp(%w[http https]) # => 0 | |
URI.parse(url) # => URI::InvalidURIError: bad URI(is not URI?): http://example.com/ ?api_token=a-token&username=USERNAME |
Yardboy
commented
Aug 20, 2014
Ah, so basically URI.regexp
seems to match all URIs in a given string and not just one. That explains something. Thanks for helping me see that!
I guess the proper solution would be to normalize the URI like xxx posted over at the HTTParty repo (jnunemaker/httparty#154) and then use the normalized URI which has spaces substituted with +
or %20
.
However, in my app I've just done it like this for now:
def validate_webhook_url
return if webhook_url.blank?
url = URI.parse(webhook_url) rescue nil
errors.add(:webhook_url, :invalid) if url.blank? || url.scheme !~ /\Ahttps?\z/
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment