Help: trick to get the domain name of an URL

1 view
Skip to first unread message

Chris Nowlan

unread,
Jul 25, 2008, 10:17:34 PM7/25/08
to rubyonra...@googlegroups.com
Hello,

I've been toying with my regex for a while and still has some problem
with this.

I would like to get the web hostname of URLs : "get_domain(url) ":

such as:

http://yahoo.com --> yahoo.com
http://www.yahoo.com/ --> www.yahoo.com
http://www.yahoo.com/cool/dir --> www.yahoo.com
http://sub1.sub2.google.com --> sub1.sub2.google.com
http://sub1.sub2.google.com/abc/def/aa?? --> sub1.sub2.google.com

I would appreciate any help and pointer. Thanks in advance!!


Thanks,
-Chris


Craig White

unread,
Jul 25, 2008, 10:31:49 PM7/25/08
to rubyonra...@googlegroups.com

Chris

unread,
Jul 25, 2008, 11:04:02 PM7/25/08
to Ruby on Rails: Talk
Wow, that was easy.

Thanks for sharing your ruby-fu :)

Rob Biedenharn

unread,
Jul 26, 2008, 1:48:14 PM7/26/08
to rubyonra...@googlegroups.com


Or,
require 'uri'
url = "http://sub1.sub2.google.com/abc/def/aa?param1=foo&param2=bar"
URI.parse(url).host
=> "sub1.sub2.google.com"

-Rob

Rob Biedenharn http://agileconsultingllc.com
R...@AgileConsultingLLC.com


Chris Nowland

unread,
Aug 24, 2008, 7:24:27 PM8/24/08
to rubyonra...@googlegroups.com
Hello,

Hope everyone's doing great.

I have a simple problem and maybe someone could help -- my
requirement is pretty simple:


o I do a lot of scripting in Ruby, for plumbing our Rails web and
Java legacy applications.
o I need to persist [string1,string2,string3] data, such as:
"apple", "november", "5"
o I do this quite often with different data sets
o Performance is not a major concern
o Simplicity is best
o I need to be able to retrieve them and update them easily

I have been using ActiveRecord (object.new, object.save, etc) to do this
with a table with 3 columns, however I found it a bit too heavy-duty for
this simple purpose because I have to use different databases since the
data are in different locations..

I wonder if anyone could share some Ruby-Fu or a Ruby library tips on this.

Thanks in advance!

Chris

------

Buy text links SEO : http://www.ask2link.com/market_place?cnow
Sell text links : http://www.ask2link.com/refer/rails

------

Xavier Noria

unread,
Aug 24, 2008, 8:25:55 PM8/24/08
to rubyonra...@googlegroups.com
On Mon, Aug 25, 2008 at 1:24 AM, Chris Nowland <chri...@gmail.com> wrote:

> o I need to persist [string1,string2,string3] data, such as:
> "apple", "november", "5"
> o I do this quite often with different data sets
> o Performance is not a major concern
> o Simplicity is best
> o I need to be able to retrieve them and update them easily

For such simple persistence Marshal may do:

data = %w(foo bar baz)

File.open('data.db', 'wb') do |fh|
fh.write(Marshal.dump(data))
end

File.open('data.db', 'rb') do |fh|
data = Marshal.load(fh.read)
end

Frederick Cheung

unread,
Aug 25, 2008, 5:19:03 AM8/25/08
to Ruby on Rails: Talk


On Aug 25, 1:25 am, "Xavier Noria" <f...@hashref.com> wrote:
> On Mon, Aug 25, 2008 at 1:24 AM, Chris Nowland <chrisn...@gmail.com> wrote:
> >  o  I need to persist [string1,string2,string3] data, such as:
> >     "apple", "november", "5"
> >  o  I do this quite often with different data sets
> >  o  Performance is not a major concern
> >  o  Simplicity is best
> >  o  I need to be able to retrieve them and update them easily
>
> For such simple persistence Marshal may do:
>
Or yaml if human readability is important (and easier to read on other
platforms)

Fred
Reply all
Reply to author
Forward
0 new messages