auto url formatting?

Hello!

I'm having a bear finding this, so I thought I'd just ask...

If I want to capture "website" as a user field (i.e. in a contacts
database)... is there an easy, automatic way to properly format it for
a link_to tag regardless of whether they include the "http" or not?
I.e. some helper or something that someone has that I could do
something like:

<%= link_to @user.website, format_url(@user.website) %>

And it would turn:

"www.google.com" # => "http://www.google.com/"

but also:

"http://www.google.com" # => "http://www.google.com/"

and:

"ftp://ftp.google.com" # => "ftp://ftp.google.com/"

I imagine there are some code snippits for doing this... I just
haven't found one yet.

Thanks!

-Danimal

Have you looked at using URI.parse? I think you should be able to use
the returned object to determine what parts were provided. You could
either store it as provided and parse/rebuild on the way out or
"clean" it as it's stored.

Andy,

Thanks for the pointer! URI.parse has gotten me halfway there. The
problem is that if I pass in "www.foobar.com" I get a URI::Generic
object instead of a URI::HTTP (or other protocol) object.

So, I can make this work, I think, but I have to check if the return
of URI.parse is URI::Generic and if so, then explicitly prepend the
"http://" for link_to's.

I.e.

[code]
url = "www.foobar.com"
uri = URI.parse(url)
full_url = uri.instance_of?(URI::Generic) ? "http://#{uri.to_s}" :
uri.to_s
[/code]

Not ideal but it works. I suppose I'd rather have something like:

uri = URI.parse(url, :default => "http")

and that would return a URI::HTTP is no protocol is defined, instead
of a URI::Generic. Ah well. One extra if/then chunk isn't too bad.

-Danimal

URI parse won’t recognise that www.domain.com is actually a http url as www is just some subdomain name that a whole ton of people love getting to point to their main site. It’s archaic, and I hate it. Think of it if it was radar.domain.com, how is URI.parse supposed to know you mean HTTP and not FTP or RSync?

Ryan,

I hear what you are saying and agree. But for my purposes, in a
Contacts database, if someone enters their company website as
"www.google.com" or even "google.com" I want to ASSUME "http". If
their company website is an FTP drop, then they'd have to put in
"ftp://companyco.com" or whatever.

The beauty of URI.parse is that it'll return a URI::Generic class if
it can't match it to one of the specific protocols. So, in my case,
I'll assume "http" and move forward. If it turns out to be wrong, they
(the users) can always explicitly add the protocol.

I suppose I could have done the same thing with a regexp, but given
that I'm dealing with URIs, I like the URI module/class better.

-Danimal

You could always get it to do some code when it detects a Generic and automatically add a http://, on a before_save perhaps.

before_save do
case URI.parse(site).class
when URI::Generic
site = “http://” + site
end
end

That way it will automatically stick http:// before URLs that are “vague” and not urls with any other prefix.

%w{www.google.com http://www.google.com}.each do |website|
  uri = URI.parse(website)
  uri.scheme ||= 'http'
  p uri.to_s
end

#=> ‘http:www.google.com’, ‘http://www.google.com