Url Parsing in Crystal

Our URL parsing program demonstrates how to parse and extract information from URLs in Crystal.

require "uri"
require "socket"

# We'll parse this example URL, which includes a
# scheme, authentication info, host, port, path,
# query params, and query fragment.
s = "postgres://user:pass@host.com:5432/path?k=v#f"

# Parse the URL and ensure there are no errors.
u = URI.parse(s)

# Accessing the scheme is straightforward.
puts u.scheme

# `userinfo` contains all authentication info; we need to
# parse it manually to get individual username and password.
if userinfo = u.userinfo
  puts userinfo
  username, password = userinfo.split(":")
  puts username
  puts password
end

# The `host` and `port` are separate in Crystal's URI.
puts "#{u.host}:#{u.port}"
puts u.host
puts u.port

# Here we extract the `path` and the fragment after
# the `#`.
puts u.path
puts u.fragment

# To get query params in a string of `k=v` format,
# use `query`. You can also parse query params
# into a hash. The parsed query param hash is from
# strings to arrays of strings, so index into `[0]`
# if you only want the first value.
puts u.query
if query = u.query
  m = URI::Params.parse(query)
  puts m
  puts m["k"][0] if m["k"]?
end

Running our URL parsing program shows all the different pieces that we extracted.

$ crystal run url_parsing.cr
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
f
k=v
{"k" => ["v"]}
v

In this Crystal version:

  1. We use the URI module to parse URLs, which is part of Crystal’s standard library.
  2. The URI.parse method is used instead of url.Parse.
  3. Crystal’s URI struct provides properties like scheme, host, port, path, fragment, and query directly.
  4. For parsing the userinfo, we need to do it manually as Crystal doesn’t provide separate methods for username and password.
  5. The host and port are separate properties in Crystal’s URI.
  6. Query parameters are parsed using URI::Params.parse instead of url.ParseQuery.
  7. Error handling is not shown explicitly as Crystal uses exceptions for error handling.

This example demonstrates how to work with URLs in Crystal, which provides a robust set of tools for URL parsing and manipulation.