View Source Moar.URI (Moar v1.47.0)

URI/URL-related functions.

Link to this section Summary

Functions

Applies some fixes to a URI string if necessary. Returns nil when given nil.

Returns a simplified string representation of a URI for display purposes. Scheme, port, params, and fragments are removed. nil is converted to an empty string.

Returns true if the URI has a host and scheme, and if it has a path, the path does not contain spaces.

Returns true when given a valid URI string with an http or https scheme.

Link to this section Functions

@spec fix(binary() | nil) :: binary() | nil

Applies some fixes to a URI string if necessary. Returns nil when given nil.

Adds a default path if one is not given:

iex> Moar.URI.fix("https://www.example.com")
"https://www.example.com/"

iex> Moar.URI.fix("https://www.example.com/")
"https://www.example.com/"

Adds an https scheme if no scheme is given:

iex> Moar.URI.fix("www.example.com/")
"https://www.example.com/"

iex> Moar.URI.fix("http://www.example.com/")
"http://www.example.com/"

Lowercases the scheme:

iex> Moar.URI.fix("HttpS://www.example.com/")
"https://www.example.com/"

Unwraps CDATA:

iex> Moar.URI.fix("<![CDATA[http://example.com/]]>")
"http://example.com/"
Link to this function

to_simple_string(string_or_uri)

View Source
@spec to_simple_string(binary() | URI.t() | nil) :: binary()

Returns a simplified string representation of a URI for display purposes. Scheme, port, params, and fragments are removed. nil is converted to an empty string.

iex> Moar.URI.to_simple_string("https://www.example.com:446/crackers/potato%20chips/fruit?a=1&b=2#something")
"www.example.com/crackers/potato chips/fruit"

iex> Moar.URI.to_simple_string(nil)
""
@spec valid?(URI.t()) :: boolean()

Returns true if the URI has a host and scheme, and if it has a path, the path does not contain spaces.

iex> Moar.URI.valid?(%URI{host: "example.com", path: "users/1", scheme: "https"})
true

iex> Moar.URI.valid?(%URI{host: "example.com", path: "users/1", scheme: nil})
false

iex> Moar.URI.valid?(%URI{host: "example.com", path: "spaces not allowed", scheme: "https"})
false
@spec web_url?(nil | binary() | URI.t()) :: boolean()

Returns true when given a valid URI string with an http or https scheme.

iex> Moar.URI.web_url?(nil)
false

iex> Moar.URI.web_url?("ftp://example.org")
false

iex> Moar.URI.web_url?("http://example.org")
true