shivam
shivam

Reputation: 16506

Get base url from a link

I tried but was unable to do so. I want to get the base url from a link. To make my question clear here's the example:

Link1: http://thechangelog.com/rawler-crawl-your-website-and-find-broken-links-with-rub/

Base url1: http://thechangelog.com

Link2: https://www.facebook.com/BreakingBad

Base url2: https://www.facebook.com

Upvotes: 1

Views: 1656

Answers (3)

Benny Hallett
Benny Hallett

Reputation: 7797

You could do this using the URI module: http://www.ruby-doc.org/stdlib-2.1.0/libdoc/uri/rdoc/URI.html

require 'uri'
uri = URI("http://thechangelog.com/rawler-crawl-your-website-and-find-broken-links-with-rub/")
puts "#{uri.scheme}://#{uri.host}"

The URI API is the same in v1.9.3 as well, so you should be able to do the same in older versions of Ruby, or JRuby if that's what you're using.

Upvotes: 7

Arie Xiao
Arie Xiao

Reputation: 14082

require 'uri'

uri = URI.parse('http://thechangelog.com/rawler-crawl-your-website-and-find-broken-links-with-rub/')
base = "#{uri.scheme}://#{uri.host}"

Upvotes: 0

sshashank124
sshashank124

Reputation: 32189

You can use the following regex:

'(http)(s?)(://)([^/]*)'

Upvotes: 0

Related Questions