Getting Started

Hex pm License

Overview

ex_url is a library modelled on the Elixir URI module. It parses and formats URL’s with the additional function that is parses the scheme-specific payload of known URI schemes. At present it can parse:

The basic API is URL.parse/1. The function URL.format/1 is delegated to the URI module.

Of course these are really URI’s, not URL’s but its a reasonable choice of name given that WHATWG prefers URL over URI:

Standardize on the term URL. URI and IRI [Internationalized Resource Identifier] are just confusing. In practice a single algorithm is used for both so keeping them distinct is not helping anyone. URL also easily wins the search result popularity contest

Examples

Parse a geo URL:

iex> URL.parse("geo:48.198634,-16.371648,3.4;crs=wgs84;u=40.0")
%URL{
  authority: nil,
  fragment: nil,
  host: nil,
  parsed_path: %URL.Geo{
    alt: 3.4,
    lat: 48.198634,
    lng: -16.371648,
    params: %{"crs" => "wgs84", "u" => 40.0}
  },
  path: "48.198634,-16.371648,3.4;crs=wgs84;u=40.0",
  port: nil,
  query: nil,
  scheme: "geo",
  userinfo: nil
}

Parse a tel URL:

iex> URL.parse "tel:+61-0407-555-987"
%URL{
  authority: nil,
  fragment: nil,
  host: nil,
  parsed_path: %URL.Tel{params: %{}, tel: "+61 407 555 987"},
  path: "+61-0407-555-987",
  port: nil,
  query: nil,
  scheme: "tel",
  userinfo: nil
}

Parse a data URL:

iex> data = URL.parse("data:;base64,SGVsbG8gV29ybGQh")
%URL{
  authority: nil,
  fragment: nil,
  host: nil,
  parsed_path: %URL.Data{
    data: "Hello World!",
    mediatype: "text/plain",
    params: %{"encoding" => "base64"}
  },
  path: ";base64,SGVsbG8gV29ybGQh",
  port: nil,
  query: nil,
  scheme: "data",
  userinfo: nil
}

Configuration

Configure ex_url in mix.exs:

  defp deps do
    [
      {ex_url, "~> 0.2"},
      ...
    ]
  end

If configured in mix.exs, URL will use the following libraries:

  • ex_phone_number will be used to parse and format telephone numbers defined in the tel URI scheme

  • ex_cldr and gettext will be used to determine the current locale and therefore the current territory (country) for parsing and formatting telephone numbers that don’t have a country code supplied.

Option configuration in mix.exs:

  defp deps do
    [
      {ex_url, "~> 0.2"},
      {:ex_phone_number, "~> 0.1"},
      {:ex_cldr, "~> 1.7"},
      {:gettext, "~> 0.13"}
      ...
    ]
  end