ExLiveUrl.Url (ExLiveUrl v0.2.0)

ExLiveUrl.Url structs represent fully qualified urls. You can think of them as bespoke URI structs.

Link to this section Summary

Functions

This function turns a string into a ExLiveUrl.Url. The given string must be absolute url.

This function turns a URI into a ExLiveUrl.Url. The given URI must be absolute.

This function serializes a ExLiveUrl.Url to a relative link, aka just the path and query params.

This function sets the host of an ExLiveUrl.Url either directly or via an updater function.

This function sets the params of an ExLiveUrl.Url either directly or via an updater function.

This function sets the path of an ExLiveUrl.Url either directly or via an updater function.

This function sets the port of an ExLiveUrl.Url either directly or via an updater function.

This function sets the scheme of an ExLiveUrl.Url either directly or via an updater function.

Link to this section Types

Link to this type

t()

(since 0.2.0)
@type t() :: %ExLiveUrl.Url{
  host: String.t(),
  params: Phoenix.LiveView.unsigned_params(),
  path: String.t(),
  port: :inet.port_number(),
  scheme: :https | :http
}

Link to this section Functions

Link to this function

from_string(string)

(since 0.2.0)
@spec from_string(String.t()) :: t()

This function turns a string into a ExLiveUrl.Url. The given string must be absolute url.

iex> ExLiveUrl.Url.from_string("https://google.com/abc?a[]=b&a[]=c")
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 443,
  path: "/abc",
  params: %{"a" => ["b", "c"]}
}
Link to this function

from_uri(uri)

(since 0.2.0)
@spec from_uri(URI.t()) :: t()

This function turns a URI into a ExLiveUrl.Url. The given URI must be absolute.

iex> ExLiveUrl.Url.from_uri(
...>   %URI{
...>     scheme: "https",
...>     host: "google.com",
...>     port: 443,
...>     path: "/abc",
...>     query: "a[]=b&a[]=c",
...>   }
...> )
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 443,
  path: "/abc",
  params: %{"a" => ["b", "c"]}
}
Link to this function

to_relative(url)

(since 0.2.0)
@spec to_relative(t()) :: String.t()

This function serializes a ExLiveUrl.Url to a relative link, aka just the path and query params.

iex> ExLiveUrl.Url.to_relative(
...>   %ExLiveUrl.Url{
...>     scheme: :https,
...>     host: "google.com",
...>     port: 443,
...>     path: "/abc",
...>     params: %{"a" => "b"}
...>   }
...> )
"/abc?a=b"
Link to this function

with_host(url, host)

(since 0.2.0)
@spec with_host(t(), host_or_updater :: String.t() | (String.t() -> String.t())) ::
  t()

This function sets the host of an ExLiveUrl.Url either directly or via an updater function.

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_host("apple.com")
%ExLiveUrl.Url{
  scheme: :https,
  host: "apple.com",
  port: 443,
  path: "/abc",
  params: %{}
}

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_host(fn host -> "www.#{host}" end)
%ExLiveUrl.Url{
  scheme: :https,
  host: "www.google.com",
  port: 443,
  path: "/abc",
  params: %{}
}
Link to this function

with_params(url, params)

(since 0.2.0)
@spec with_params(
  t(),
  params_or_updater ::
    Phoenix.LiveView.unsigned_params()
    | (Phoenix.LiveView.unsigned_params() -> Phoenix.LiveView.unsigned_params())
) :: t()

This function sets the params of an ExLiveUrl.Url either directly or via an updater function.

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_params(%{"a" => ["b", "c"]})
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 443,
  path: "/abc",
  params: %{"a" => ["b", "c"]}
}

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_params(fn params -> Map.put(params, "a", ["b", "c"]) end)
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 443,
  path: "/abc",
  params: %{"a" => ["b", "c"]}
}
Link to this function

with_path(url, path)

(since 0.2.0)
@spec with_path(t(), path_or_updater :: String.t() | (String.t() -> String.t())) ::
  t()

This function sets the path of an ExLiveUrl.Url either directly or via an updater function.

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_path("/")
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 443,
  path: "/",
  params: %{}
}

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_path(fn path -> path <> "/efg" end)
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 443,
  path: "/abc/efg",
  params: %{}
}
Link to this function

with_port(url, port)

(since 0.2.0)
@spec with_port(
  t(),
  port_or_updater ::
    :inet.port_number() | (:inet.port_number() -> :inet.port_number())
) :: t()

This function sets the port of an ExLiveUrl.Url either directly or via an updater function.

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_port(1234)
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 1234,
  path: "/abc",
  params: %{}
}

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_port(fn port -> port + 1 end)
%ExLiveUrl.Url{
  scheme: :https,
  host: "google.com",
  port: 444,
  path: "/abc",
  params: %{}
}
Link to this function

with_scheme(url, scheme)

(since 0.2.0)
@spec with_scheme(
  t(),
  scheme_or_updater :: :http | :https | (:http | :https -> :http | :https)
) :: t()

This function sets the scheme of an ExLiveUrl.Url either directly or via an updater function.

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_scheme(:http)
%ExLiveUrl.Url{
  scheme: :http,
  host: "google.com",
  port: 443,
  path: "/abc",
  params: %{}
}

iex> "https://google.com/abc"
...> |> ExLiveUrl.Url.from_string()
...> |> ExLiveUrl.Url.with_scheme(fn :https -> :http end)
%ExLiveUrl.Url{
  scheme: :http,
  host: "google.com",
  port: 443,
  path: "/abc",
  params: %{}
}