View Source CloudflareApi.Utils (cloudflare_api v0.1.0)

Link to this section Summary

Functions

Macro that makes a function public in test, private in non-test

If val is explicitly (and therefore unambiguously) false, then returns true. Otherwise false

If val is explicitly (and therefore unambiguously) true, then returns false. Otherwise true

Using either key or extract_func, extract the specified thing.

If val is explicitly true, output is true. Otherwise false

Runs IO.inspect/2 with pretty printing, colors, and unlimited size.

Retrieve syntax colors for embedding into :syntax_colors of Inspect.Opts

Quick regex check to see if the supplied string is a valid UUID

Convert a list to a String, suitable for printing

Takes a list of strings or atoms and returns a list with string and atoms.

Convert a map with String keys into a map with Atom keys.

Convert a map with String keys into a map with Atom keys.

Convert a map to a String, suitable for printing.

Takes a map and a list of keys whose values should be masked

Replaces the caracters in str with asterisks "*", thus "masking" the value.

Checks if the passed item is nil or empty string.

Easy drop-in to a pipe to inspect the return value of the previous function.

if value (value of the argument) is nil, this will raise CloudflareApi.CantBeNil

if value (value of the argument) is nil, this will raise CloudflareApi.CantBeNil

Converts a struct to a regular map by deleting the :__meta__ key

Convert the value, map, or list to a string, suitable for printing or storing.

If val is explicitly false, output is false. Otherwise true

Convert a tuple to a String, suitable for printing

Link to this section Types

Specs

tuple_key_value() :: binary() | atom()

Link to this section Functions

Link to this macro

defp_testable(head, body \\ nil)

View Source (macro)

Macro that makes a function public in test, private in non-test

See: https://stackoverflow.com/a/47598190/2062384

If val is explicitly (and therefore unambiguously) false, then returns true. Otherwise false

Explicitly false values are case-insensitive, "f", "false", "no", "n"

If val is explicitly (and therefore unambiguously) true, then returns false. Otherwise true

Explicitly true values are case-insensitive, "t", "true", "yes", "y"

Specs

extract(
  Access.t() | list() | tuple() | any(),
  integer() | String.t() | (... -> any())
) :: any()

Using either key or extract_func, extract the specified thing.

Alias is process() and transform()

This is very useful for converting some value into another in a pipeline, such as unwrapping a structure or transforming it. It's essentially like Enum.map/2 but only operates on a single object rather than an Enumerable

Example:

some_function_returns_a_map()
|> CloudflareApi.Utils.extract(:data)  # extract the 'data' key from map
|> Enum.map(...)

get_user()
|> CloudflareApi.Utils.extract(:age)
|> handle_age()

get_user()
|> CloudflareApi.Utils.extract(%{name: "Jeb", age: 37}, fn {:ok, user} -> user)
|> extract()

iex examples:

iex> CloudflareApi.Utils.extract(%{name: "Jeb", age: 37}, :age) 37

iex> CloudflareApi.Utils.extract(%{name: "Jeb", age: 37}, fn arg -> arg[:age] * 2 end) 74

Link to this function

false_or_explicitly_true?(val)

View Source

If val is explicitly true, output is true. Otherwise false

The effect of this is that if the string isn't explicitly true then it is considered false. This is useful for example with an env var where the default should be false

Link to this function

inspect(val, opaque_struct \\ true, limit \\ 50)

View Source

Runs IO.inspect/2 with pretty printing, colors, and unlimited size.

If opaque_struct is false, then structs will be printed as Maps, which allows you to see any opaque fields they might have set

Link to this function

inspect_format(opaque_struct \\ true, limit \\ 50)

View Source

Get Inspect.Opts for Kernel.inspect or IO.inspect

If opaque_struct is false, then structs will be printed as Maps, which allows you to see any opaque fields they might have set

limit is the max number of stuff printed out. Can be an integer or :infinity

Retrieve syntax colors for embedding into :syntax_colors of Inspect.Opts

You probably don't want this directly. You probably want inspect_format

Quick regex check to see if the supplied string is a valid UUID

Check is done by simple regular expression and is not overly sophisticated.

Return true || false

Examples

iex> CloudflareApi.Utils.is_uuid?(nil)
false
iex> CloudflareApi.Utils.is_uuid?("hello world")
false
iex> CloudflareApi.Utils.is_uuid?("4c2fd8d3-a6e3-4e4b-a2ce-3f21456eeb85")
true
Link to this function

list_to_string(list, mask_keys \\ [])

View Source

Specs

list_to_string(list :: list() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert a list to a String, suitable for printing

Will raise a String.chars error if can't coerce part to a String

mask_keys is used to mask the values in any keys that are in maps in the list

Link to this function

list_to_strings_and_atoms(list)

View Source

Takes a list of strings or atoms and returns a list with string and atoms.

Examples

iex> list_to_strings_and_atoms([:circle])
[:circle, "circle"]

iex> list_to_strings_and_atoms([:circle, :square])
[:square, "square", :circle, "circle"]

iex> list_to_strings_and_atoms(["circle", "square"])
["square", :square, "circle", :circle]
Link to this function

map_atom_keys_to_strings(map)

View Source

Convert a map with String keys into a map with Atom keys.

Examples

iex> CloudflareApi.Utils.map_atom_keys_to_strings(%{one: "one", two: "two"})
%{"one" => "one", "two" => "two"}
Link to this function

map_string_keys_to_atoms(map)

View Source

Convert a map with String keys into a map with Atom keys.

Examples

iex> CloudflareApi.Utils.map_string_keys_to_atoms(%{"one" => "one", "two" => "two"})
%{one: "one", two: "two"}m
Link to this function

map_to_string(map, mask_keys \\ [])

View Source

Specs

map_to_string(map :: map() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert a map to a String, suitable for printing.

Optionally pass a list of keys to mask.

Examples

iex> map_to_string(%{michael: "knight"})
"michael: 'knight'"

iex> map_to_string(%{michael: "knight", kitt: "karr"})
"kitt: 'karr', michael: 'knight'"

iex> map_to_string(%{michael: "knight", kitt: "karr"}, [:kitt])
"kitt: '****', michael: 'knight'"

iex> map_to_string(%{michael: "knight", kitt: "karr"}, [:kitt, :michael])
"kitt: '****', michael: '******'"

iex> map_to_string(%{"michael" => "knight", "kitt" => "karr", "carr" => "hart"}, ["kitt", "michael"])
"carr: 'hart', kitt: '****', michael: '******'"
Link to this function

mask_map_key_values(map, mask_keys)

View Source

Takes a map and a list of keys whose values should be masked

Examples

iex> CloudflareApi.Utils.mask_map_key_values(%{name: "Ben, title: "Lord"}, [:title])
%{name: "Ben", title: "****"}

iex> CloudflareApi.Utils.mask_map_key_values(%{name: "Ben, age: 39}, [:age])
%{name: "Ben", age: "**"}

Replaces the caracters in str with asterisks "*", thus "masking" the value.

If argument is nil nothing will change nil will be returned. If argument is not a binary(), it will be coerced to a binary then masked.

Link to this function

nil_or_empty?(str_or_nil)

View Source

Checks if the passed item is nil or empty string.

The param will be passed to Kernel.to_string() and then String.trim() and checked for empty string

Examples

iex> CloudflareApi.Utils.nil_or_empty?("hello")
false
iex> CloudflareApi.Utils.nil_or_empty?("")
true
iex> CloudflareApi.Utils.nil_or_empty?(nil)
true
Link to this function

not_nil_or_empty?(str_or_nil)

View Source

Specs

process(
  Access.t() | list() | tuple() | any(),
  integer() | String.t() | (... -> any())
) :: any()
Link to this function

pry_pipe(retval, arg1 \\ nil, arg2 \\ nil, arg3 \\ nil, arg4 \\ nil)

View Source

Easy drop-in to a pipe to inspect the return value of the previous function.

Examples

conn
|> put_status(:not_found)
|> put_view(CloudflareApiWeb.ErrorView)
|> render(:"404")
|> pry_pipe()

Alternatives

You may also wish to consider using IO.inspect/3 in pipelines. IO.inspect/3 will print and return the value unchanged. Example:

conn
|> put_status(:not_found)
|> IO.inspect(label: "after status")
|> render(:"404")

if value (value of the argument) is nil, this will raise CloudflareApi.CantBeNil

argn (name of the argument) will be passed to allow for more helpful error messages that tell you the name of the variable that was nil

Examples

iex> CloudflareApi.Utils.raise_if_nil!("someval")
"someval"
iex> CloudflareApi.Utils.raise_if_nil!(nil)
** (CloudflareApi.CantBeNil) variable 'somevar' was nil but cannot be
    (malan 0.1.0) lib/malan/utils.ex:142: CloudflareApi.Utils.raise_if_nil!/1
Link to this function

raise_if_nil!(varname, value)

View Source

if value (value of the argument) is nil, this will raise CloudflareApi.CantBeNil

argn (name of the argument) will be passed to allow for more helpful error messages that tell you the name of the variable that was nil

Examples

iex> CloudflareApi.Utils.raise_if_nil!("somevar", "someval")
"someval"
iex> CloudflareApi.Utils.raise_if_nil!("somevar", nil)
** (CloudflareApi.CantBeNil) variable 'somevar' was nil but cannot be
    (malan 0.1.0) lib/malan/utils.ex:135: CloudflareApi.Utils.raise_if_nil!/2
Link to this function

struct_to_map(struct, mask_keys \\ [])

View Source

Converts a struct to a regular map by deleting the :__meta__ key

Examples

CloudflareApi.Utils.struct_to_map(%Something{hello: "world"})
%{hello: "world"}
Link to this function

to_string(value, mask_keys \\ [])

View Source

Specs

to_string(input :: map() | list() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert the value, map, or list to a string, suitable for printing or storing.

If the value is not a map or list, it must be a type that implements the String.Chars protocol, otherwise this will fail.

The reason to offer this util function rather than implementing String.Chars for maps and lists is that we want to make sure that we never accidentally convert those to a string. This conversion is somewhat destructive and is irreversable, so it should only be done intentionally.

Specs

transform(
  Access.t() | list() | tuple() | any(),
  integer() | String.t() | (... -> any())
) :: any()
Link to this function

true_or_explicitly_false?(val)

View Source

If val is explicitly false, output is false. Otherwise true

The effect of this is that if the string isn't explicitly false then it is considered true. This is useful for example with an env var where the default should be true

Link to this function

trunc_str(str, length \\ 255)

View Source
Link to this function

tuple_to_string(tuple, mask_keys \\ [])

View Source

Specs

tuple_to_string(
  tuple :: {tuple_key_value(), tuple_key_value()} | String.Chars.t(),
  mask_keys :: [binary()]
) :: binary()
tuple_to_string(tuple :: tuple() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert a tuple to a String, suitable for printing

Will raise a String.chars error if can't coerce part to a String

mask_keys is used to mask the values in any keys that are in maps in the tuple