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
@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
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"]}
}
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"]}
}
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"
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: %{}
}
@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"]}
}
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: %{}
}
@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: %{}
}
@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: %{}
}