.

Coffee Powered

code and content

Quick tip: Strip URLs before parsing!

Rather than roll my own URL regexes, I prefer to let the existing libraries do the heavy lifting. Ruby has a uri library which is fantastic for parsing (and validating) URLs.

For example, something like this might be used in a model validation:

require 'uri'

def validate_url(url)
	parsed_uri = URI::parse(url)
rescue URI::InvalidURIError
	errors.add :url, "Sorry, that doesn't look like a valid URL"
end

I noticed a bit ago that I started getting invalid URL errors where there shouldn’t be any. After far too long spent in the library’s code, I realized my error: the URLs were being pasted with a trailing space. Stripping the string before attempting to parse it fixed it right up.

I’d argue that URI::parse should likely strip any incoming strings, but in the meantime, remember to strip your user input before trying to determine whether it’s valid or not, or you may end up with frustrated users.

2 Comments

  1. May 23, 2009 at 8:45 am | Permalink

    Hi, nice posts there :-) thank's for the interesting information

  2. Kalebarkab
    June 9, 2009 at 5:44 am | Permalink

    I want to find good pop music. Help me please.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*