Overview
URI module of ruby can be used to get all parts from a URL. This module can be used to parse a URL and extract all parts https://ruby-doc.org/stdlib-2.5.1/libdoc/uri/rdoc/URI.html
Once the given URL is parsed correctly, then it will return the URI object. We can then access the below information from the URI
- Scheme
- User Info
- Hostname
- Port
- Pathname
- Query Params
- Fragment
Once we have all the parts we can concatenate them to get the full hostname along with the port.
Program
Below is the program for the same.
require 'uri'
uri = "https://test:abcd123@techbyexample.com:8000/tutorials/intro?type=advance&compact=false#history"
pasrse_uri = URI(uri)
hostname = pasrse_uri.scheme + "://" + pasrse_uri.hostname + ":" + pasrse_uri.port.to_s
puts hostname
Output
https://techbyexample.com:8000