Forum: Ruby on Rails shorten a link

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee4ead180a47f98999854b3e1eef7638?d=identicon&s=25 Sean Six (seansix)
on 2009-05-28 10:57
Suppose a user submits a url: http://www.nyt.com/education/2345545.  How
can this be shortened to a cleaner url, like nyt.com?
29ebf90af6107d2eb39b587c7972639c?d=identicon&s=25 Mukund (Guest)
on 2009-05-28 12:12
(Received via mailing list)
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
Ee4ead180a47f98999854b3e1eef7638?d=identicon&s=25 Sean Six (seansix)
on 2009-05-28 14:16
Mukund wrote:
> 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

This is a little harder than I expected.  So I include the require 'uri'
in the model.  Then it seems I need to parse the host part of the uri.
How do I parse this?  Do I need to use a RegExpression?
5f3d52658aafcf0aabe76a5147e8909f?d=identicon&s=25 tomrossi7 (Guest)
on 2009-05-28 15:01
(Received via mailing list)
Sean,

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

--Tom
Ee4ead180a47f98999854b3e1eef7638?d=identicon&s=25 Sean Six (seansix)
on 2009-05-28 18:19
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.
5f3d52658aafcf0aabe76a5147e8909f?d=identicon&s=25 tomrossi7 (Guest)
on 2009-05-28 18:48
(Received via mailing list)
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
On May 28, 12:19 pm, Sean Six <rails-mailing-l...@andreas-s.net>
Ee4ead180a47f98999854b3e1eef7638?d=identicon&s=25 Sean Six (seansix)
on 2009-05-29 05:52
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.
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2009-06-01 17:13
(Received via mailing list)
Can you post a sample @article.url that results in blank .host output
from URI.parse?
This topic is locked and can not be replied to.