View Source Bonfire.Common.URIs (Bonfire v0.9.11-social-beta.6)

URI/URL/path helpers

Summary

Functions

Returns the base domain from the given URI or endpoint.

Returns the homepage URI (as struct) of the local instance.

Return the homepage URL (as string) of the local instance

Returns the canonical URL (i.e., the one used for ActivityPub federation) of an object.

Removes the scheme from a URL to get the display URL.

Returns the path (URL on the local instance) for the given object/struct (eg. a User), view or schema module, or path name (atom defined in routes), along with optional arguments.

Generates a static path based on the given path and endpoint module.

Returns the full URL (including domain and path) for a given object, module, or path name.

Validates a URI string and returns a boolean.

Validates a URI string and returns a tuple.

Functions

base_domain(uri_or_endpoint_or_conn \\ nil)

Returns the base domain from the given URI or endpoint.

iex> base_domain(%URI{host: "example.com", port: 443})
"example.com"

base_uri(conn_or_socket \\ nil)

Returns the homepage URI (as struct) of the local instance.

> %URI{scheme: "http", host: "localhost"} = base_uri(:my_endpoint)

base_url(conn_or_socket_or_uri \\ nil)

Return the homepage URL (as string) of the local instance

based_url(url, conn \\ nil)

TODOC

canonical_url(object)

Returns the canonical URL (i.e., the one used for ActivityPub federation) of an object.

Examples

iex> canonical_url(%{canonical_uri: "http://example.com"})
"http://example.com"

iex> canonical_url(%{canonical_url: "http://example.com"})
"http://example.com"

iex> canonical_url(%{"canonicalUrl" => "http://example.com"})
"http://example.com"

iex> canonical_url(%{peered: %{canonical_uri: "http://example.com"}})
"http://example.com"

iex> canonical_url(%{character: %{canonical_uri: "http://example.com"}})
"http://example.com"

iex> canonical_url(%{character: %{peered: %{canonical_uri: "http://example.com"}}})
"http://example.com"

iex> canonical_url(%{peered: %Ecto.Association.NotLoaded{}})
nil

iex> canonical_url(%{created: %Ecto.Association.NotLoaded{}})
nil

iex> canonical_url(%{character: %Ecto.Association.NotLoaded{}})
nil

iex> canonical_url(%{character: %{peered: %{}}})
nil

iex> canonical_url(%{path: "http://example.com"})
"http://example.com"

iex> canonical_url(%{other: "data"})
nil

display_url(url)

Removes the scheme from a URL to get the display URL.

iex> display_url("https://example.com/path")
"example.com/path"

iex> display_url("http://example.com/path")
"example.com/path"

iex> display_url("/path")
"/path"

fallback(args)

fallback(id, args)

fallback(id, type, args)

maybe_generate_canonical_url(id)

path(view_module_or_path_name_or_object, args \\ [], opts \\ [])

Returns the path (URL on the local instance) for the given object/struct (eg. a User), view or schema module, or path name (atom defined in routes), along with optional arguments.

Returns the path (URL on the local instance) for the given object/struct (e.g., a User), view or schema module, or path name (atom defined in routes), along with optional arguments.

Examples

> path(:user, [1], [])
"/users/1"

> path(User, [1], [])
"/users/1"

> path(%{id: "1"}, :show, [])
"/users/1/show"

> path(%{id: "1"}, [some: :args], [])
"/users/1/some_args"

iex> path("12345", [some: :args], [])
nil

path_by_id(id, args, object, opts)

remote_canonical_url(object)

static_path(path, endpoint_module \\ Bonfire.Common.Config.endpoint_module())

Generates a static path based on the given path and endpoint module.

> static_path("/assets/image.png")
"/assets/image.png"

url_path(view_module_or_path_name_or_object, args \\ [])

Returns the full URL (including domain and path) for a given object, module, or path name.

> url_path(:user, [1])
"http://localhost:4000/discussion/user/1"

valid_url?(str)

Validates a URI string and returns a boolean.

Examples

iex> true == validate_uri("http://example.com")

iex> false == validate_uri("invalid_uri")

validate_uri(str)

Validates a URI string and returns a tuple.

Examples

iex> {:ok, %URI{scheme: "http", host: "example.com"}} = validate_uri("http://example.com")

iex> {:error, %URI{scheme: nil, host: nil}} = validate_uri("invalid_uri")