How to Use URI Library in Ruby

09/21/2021

Contents

In this article, you will learn how to use URI library in Ruby.

Using URI library

URI (Uniform Resource Identifier) is a standard library in Ruby that provides a way to handle URIs, URLs, and URNs in Ruby. Here are some steps to use the URI library in Ruby:

Require the URI library
require 'uri'
Parse a URI
uri = URI.parse("http://www.example.com/path/to/resource?key1=value1&key2=value2")

This will create a URI object that contains information about the URI, such as the scheme, host, path, and query parameters.

Access the different parts of the URI
puts uri.scheme # "http"
puts uri.host # "www.example.com"
puts uri.path # "/path/to/resource"
puts uri.query # "key1=value1&key2=value2"

You can also modify the URI by setting new values for the different parts of the URI. For example:

uri.scheme = "https"
uri.host = "secure.example.com"
uri.query = "key3=value3"
Create a new URI
new_uri = URI::HTTP.build({:host => "www.example.com", :path => "/new/path"})

This will create a new URI object with the specified host and path.

Use the URI object to make HTTP requests
require 'net/http'

response = Net::HTTP.get_response(uri)
puts response.body

This will make an HTTP GET request to the URI and print the response body.

 

Here are some additional things you can do with the URI library in Ruby:

Handling relative URIs

You can handle relative URIs by using the URI.join method. For example:

base_uri = URI.parse("http://www.example.com/")
relative_uri = URI.parse("/path/to/resource")
full_uri = URI.join(base_uri, relative_uri)

This will create a new URI object with the full URI:
“http://www.example.com/path/to/resource”.

Handling internationalized URIs

The URI library supports internationalized domain names (IDN) and Unicode in URIs. For example:

uri = URI.parse("http://こんにちは.com/")
puts uri.host # "xn--28j4b0c.com"

This will parse the URI with the IDN “こんにちは.com” and output the ASCII-encoded form of the host name.

Handling different URI schemes

The URI library supports different URI schemes, such as http, https, ftp, file, and many others. You can use the URI.scheme_list method to get a list of supported schemes. For example:

puts URI.scheme_list # ["file", "ftp", "gopher", "http", "https", "mailto", "news", "nntp", "prospero", "telnet", "rlogin", "tn3270", "wais"]
Encoding and decoding URI components

You can use the URI.encode and URI.decode methods to encode and decode URI components, such as query parameters or path segments. For example:

encoded_query = URI.encode("key=value&foo=bar")
decoded_query = URI.decode(encoded_query)

This will encode the query parameters “key=value&foo=bar” and then decode them back to the original string.

Comparing URIs

You can use the URI.eql? method to compare two URIs for equality. For example:

uri1 = URI.parse("http://www.example.com/")
uri2 = URI.parse("http://www.example.com/")
puts uri1.eql?(uri2) # true

This will compare two URI objects for equality and output true.