Reputation: 16506
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
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
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