.

Coffee Powered

code and content

Tag Archives: parse

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 [...]