Suppose a user submits a url: http://www.nyt.com/education/2345545. How can this be shortened to a cleaner url, like nyt.com? -- Posted via http://www.ruby-forum.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 On May 28, 1:57 pm, Sean Six <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Suppose a user submits a url:http://www.nyt.com/education/2345545. How > can this be shortened to a cleaner url, like nyt.com? > -- > Posted viahttp://www.ruby-forum.com/.
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.htmlThis 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? -- Posted via http://www.ruby-forum.com/.
Sean, Are you saving this url to the model? I''m not sure what you mean by shorten it up? --Tom On May 28, 8:16 am, Sean Six <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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? > -- > Posted viahttp://www.ruby-forum.com/.
tomrossi7 wrote:> Sean, > > Are you saving this url to the model? I''m not sure what you mean by > shorten it up? > > --TomI 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. -- Posted via http://www.ruby-forum.com/.
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 iswww.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. > > -- > Posted viahttp://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
Can you post a sample @article.url that results in blank .host output from URI.parse? -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Sean Six Sent: Thursday, May 28, 2009 8:53 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: shorten a link 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. -- Posted via http://www.ruby-forum.com/. GHC Confidentiality Statement This message and any attached files might contain confidential information protected by federal and state law. The information is intended only for the use of the individual(s) or entities originally named as addressees. The improper disclosure of such information may be subject to civil or criminal penalties. If this message reached you in error, please contact the sender and destroy this message. Disclosing, copying, forwarding, or distributing the information by unauthorized individuals or entities is strictly prohibited by law.