1

Given a url like https://example.com/<network_id>/1

Ruby's URI module can validate URIs using a regular expression:

URI.regexp.match("https://example.com/<network_id>/1")
=> #<MatchData "https://example.com/" 1:"https" 2:nil 3:nil 4:"example.com" 5:nil 6:nil 7:"/" 8:nil 9:nil>

But if you try to hand this off to another package, say Java's URI class

It will fail:

Error message:
Illegal character in path at index ...

                               java.net.URI.create(URI.java:852)
org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:73)
...

Is there a better URI validator, something that we can use in a Rails class?

Ken Mayer
  • 1,884
  • 1
  • 13
  • 16

1 Answers1

1

I would use URI.parse to validate a URL. URL.parse raises an exception if the URL is not valid:

require 'uri'

URI.parse('https://example.com/<network_id>/1')
#=> bad URI(is not URI?): "https://example.com/<network_id>/1" (URI::InvalidURIError)                                       

Which allows writing a simple URL validation method like this:

def valid_url?(url)
  URI.parse(url) rescue false
end

valid_url?('https://example.com/<network_id>/1')
#=> false

valid_url?('https://stackoverflow.com/questions/75179882')
#=> #<URI::HTTPS https://stackoverflow.com/questions/75179882>
spickermann
  • 100,941
  • 9
  • 101
  • 131
  • Also `URI.regexp` and related methods are obsolete [since 2.2](https://github.com/ruby/ruby/commit/bb83f32dc3e0424d25fa4e55d8ff32b061320e41). The documentation of `URI` doesn't mention that but running with warnings enabled (`-w`) is always a good idea. – cremno Jan 20 '23 at 16:02