Forum: Ruby Needed only the Domain name from an url

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.
44d59c6c5a7e3435409ebe94c9aa9db5?d=identicon&s=25 Srikanth Jeeva (srikanth_j)
on 2009-04-22 14:28
hi,
Suppose if i give,
url="www.google.com/picasa/images/1"

I need only the string "google" which is the domain name.

right now i have tried this,

require 'rubygems'
require 'uri'
url = "http://www.google.com/picasa/images/1"
puts url = URI.parse(url).host
-----------
output: "google.com"
-----------
but i want only the word "google". please help.

Pts:
the domain name can also be, "google.co.in"
ie) for example, "http://www.google.co.in/picasa/images/1"

Thanks in advance,
srikanth
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-22 15:26
(Received via mailing list)
On Wed, Apr 22, 2009 at 2:28 PM, Srikanth Jeeva <sri.jjhero@gmail.com>
wrote:
> url = "http://www.google.com/picasa/images/1"
> Thanks in advance,
> srikanth


Technically google.com is the domain
If I run your code, I get "www.google.com" as the output, not
"google.com"
This poses an interesting problem because a domain can be:
one.two.three.four.google.co.uk
What is the domain for you in that?

Otherwise you could do something like
uri.host.sub(/.*?\.?([^.]+)(?:\.\w{3}|\.\w{2,3}\.\w{2,3})$/, '\1')

It may not be the prettiest regex but it gets the job done for the
following:
  * one.two.google.com
  * one.two.google.co.in
  * www.somesite.com.au
  * www.one.co.uk
but it won't work for
  * www.one.com (but it will work for one.com)

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2009-04-24 14:31
Srikanth Jeeva wrote:
> hi,
> Suppose if i give,
> url="www.google.com/picasa/images/1"
>
> I need only the string "google" which is the domain name.
>
> right now i have tried this,
>
> require 'rubygems'
> require 'uri'
> url = "http://www.google.com/picasa/images/1"
> puts url = URI.parse(url).host
> -----------
> output: "google.com"
> -----------
> but i want only the word "google". please help.
>
> Pts:
> the domain name can also be, "google.co.in"
> ie) for example, "http://www.google.co.in/picasa/images/1"
>
> Thanks in advance,
> srikanth



Hi Sri,

Are you got the solution ?

If Yes, then update here about it..

Regards
P.Raveendran
http://raveendran.wordpress.com
Cbb46683c0ba244e9832aa348ac3ce9e?d=identicon&s=25 kranthi reddy (Guest)
on 2009-04-25 09:23
(Received via mailing list)
If you are using a rails app then something similar to this can be done:

   In the application controller
           private

               def locate_subdomain

               end


On Fri, Apr 24, 2009 at 6:01 PM, Raveendran Perumalsamy <
Cbb46683c0ba244e9832aa348ac3ce9e?d=identicon&s=25 kranthi reddy (Guest)
on 2009-04-25 09:26
(Received via mailing list)
On Sat, Apr 25, 2009 at 12:44 PM, kranthi reddy <kranthicu@gmail.com>
wrote:

> If you are using a rails app then something similar to this can be done:
>
>    In the application controller
>            private
>
>                def locate_subdomain
>                           domain = request.domain  # for just the domain
> name
>
                              subdomain = request.subdomain  # for the
sudomain
This topic is locked and can not be replied to.