shorten a link

Suppose a user submits a url: http://www.nyt.com/education/2345545. How
can this be shortened to a cleaner url, like nyt.com?

require 'uri'

  uri = URI.parse("http://www.ruby-lang.org/")
  p uri
  # => #<URI::HTTP:0x202281be URL:http://www.ruby-lang.org/>
  p uri.scheme
  # => "http"
  p uri.host
  # => "www.ruby-lang.org"

http://www.ruby-doc.org/core/classes/URI.html

Sean,

Are you saving this url to the model? I'm not sure what you mean by
shorten it up?

--Tom

tomrossi7 wrote:

Sean,

Are you saving this url to the model? I'm not sure what you mean by
shorten it up?

--Tom

I want a user to submit a link to a webpage. After they submit the link
I want to parse it to the homepage url. Like if the article is
www.nyt.com/sports/lakers/123. I want to have this shortened to the
homepage link nyt.com. You can try it in the interpreter as someone
posted above.

Yeah, URI is the library to work with. You could tighten it up and
just do:

homepage = URI.parse("http://www.nyt.com/sports/lakers/123/").host

On the model, you could put "require 'uri' " but I think its loaded
with Rails by default.

--Tom

I don't think

I have @article.url and I want to parse that url. When I tried:
@homepage = URI.parse(@article.url).host in the controller I simply got
a blank output.

Can you post a sample @article.url that results in blank .host output from URI.parse?