resx v0.0.4 Resx.Resource

The resource representation.

Link to this section Summary

Functions

Check if two resources or resource references point to the same resource

Retrieve the attribute for a resource or resource reference

Retrieve the attribute keys for a resource or resource reference

Retrieve the attributes for a resource or resource reference

Discard the resource

Check whether a resource or resource reference exists

Compute a hash of the resource content using the default hashing function

Compute a hash of the resource content

Check what kind of reference this resource or resource reference is

Open a resource from a pre-existing resource or a resource reference

Open a resource from a pre-existing resource or a resource reference

Get the source of the current resource or resource reference

Stream a resource from a pre-existing resource or a resource reference

Stream a resource from a pre-existing resource or a resource reference

Retrieve the URI for a resource or resource reference

Link to this section Types

Link to this type attribute_key()
attribute_key() :: atom() | String.t()
Link to this type hash_state()
hash_state() :: any()
Link to this type t(content)
t(content) :: %Resx.Resource{
  content: content,
  meta: keyword(),
  reference: Resx.Resource.Reference.t()
}

Link to this section Functions

Link to this function alike?(resource_a, resource_b)
alike?(t() | Resx.ref(), t() | Resx.ref()) :: boolean()

Check if two resources or resource references point to the same resource.

Link to this function attribute(resource, field)
attribute(t() | Resx.ref(), attribute_key()) ::
  {:ok, any()}
  | Resx.error(Resx.resource_error() | Resx.reference_error() | :unknown_key)

Retrieve the attribute for a resource or resource reference.

Link to this function attribute_keys(resource)
attribute_keys(t() | Resx.ref()) ::
  {:ok, [attribute_key()]}
  | Resx.error(Resx.resource_error() | Resx.reference_error())

Retrieve the attribute keys for a resource or resource reference.

Link to this function attributes(resource)
attributes(t() | Resx.ref()) ::
  {:ok, %{optional(attribute_key()) => any()}}
  | Resx.error(Resx.resource_error() | Resx.reference_error())

Retrieve the attributes for a resource or resource reference.

Link to this function discard(resource, opts \\ [])

Discard the resource.

Only resources or resource references that implement the Resx.Storer behaviour should be passed to this.

Link to this function exists?(resource)
exists?(t() | Resx.ref()) ::
  {:ok, boolean()} | Resx.error(Resx.reference_error())

Check whether a resource or resource reference exists.

Compute a hash of the resource content using the default hashing function.

The default hashing function can be configured by giving a :hash option in your config.

config :resx,
    hash: { :crc32, { :erlang, :crc32, 1 } }

See hash/2 for more information.

Compute a hash of the resource content.

Meta information and resource references are not included in the hash.

Hashing algorithms can take the form of either an atom that is a valid option to :crypto.hash/2, or a tuple of type hasher or streamable_hasher to provide a custom hashing function. Valid function formats are any callback variant, see Callback for more information.

Note: If the resource content is streamable and a hasher is provided for the algo, then the entire content will be decomposed first. If the algo is a streamable_hasher then no decomposition will take place.

The inputs to the initialiser function of a streamable_hasher are optional. The rest are all required.

If the requested hash is the same as the checksum found in the resource, then that checksum will be returned without rehashing the resource content.

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, { :crc32, { :erlang, :crc32, 1 } })
{ :crc32, 4157704578 }

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, { :crc32, { :erlang, :crc32, [] } })
{ :crc32, 4157704578 }

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, { :md5, { :crypto, :hash, [:md5] } })
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }

iex> Resx.Resource.hash(%Resx.Resource.Content.Stream{ type: ["text/plain"], data: ["He", "l", "lo"] }, { :md5, { :crypto, :hash, [:md5] } })
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, { :md5, { :crypto, :hash_init, 1 }, { :crypto, :hash_update, 2 }, { :crypto, :hash_final, 1 } })
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }

iex> Resx.Resource.hash(%Resx.Resource.Content.Stream{ type: ["text/plain"], data: ["He", "l", "lo"] }, { :md5, { :crypto, :hash_init, 1 }, { :crypto, :hash_update, 2 }, { :crypto, :hash_final, 1 } })
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, :md5)
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }

iex> Resx.Resource.hash(%Resx.Resource.Content.Stream{ type: ["text/plain"], data: ["He", "l", "lo"] }, :md5)
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, { :hmac_md5_5, { :crypto, :hmac, [:md5, "secret", 5], 2 } })
{ :hmac_md5_5, <<243, 134, 128, 59, 99>> }

iex> Resx.Resource.hash(%Resx.Resource.Content.Stream{ type: ["text/plain"], data: ["He", "l", "lo"] }, { :hmac_md5_5, { :crypto, :hmac, [:md5, "secret", 5], 2 } })
{ :hmac_md5_5, <<243, 134, 128, 59, 99>> }

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, { :hmac_md5_5, { :crypto, :hmac_init, [:md5, "secret"], nil }, { :crypto, :hmac_update, 2 }, { :crypto, :hmac_final_n, [5], 0 } })
{ :hmac_md5_5, <<243, 134, 128, 59, 99>> }

iex> Resx.Resource.hash(%Resx.Resource.Content.Stream{ type: ["text/plain"], data: ["He", "l", "lo"] }, { :hmac_md5_5, { :crypto, :hmac_init, [:md5, "secret"], nil }, { :crypto, :hmac_update, 2 }, { :crypto, :hmac_final_n, [5], 0 } })
{ :hmac_md5_5, <<243, 134, 128, 59, 99>> }

iex> Resx.Resource.hash(%Resx.Resource.Content{ type: ["text/plain"], data: "Hello" }, { :base64, &Base.encode64/1 })
{ :base64, "SGVsbG8=" }

iex> Resx.Resource.hash(%Resx.Resource{ reference: %Resx.Resource.Reference{ integrity: %Resx.Resource.Reference.Integrity{ timestamp: 0 }, adapter: nil, repository: nil }, content: %Resx.Resource.Content{ type: ["text/plain"], data: "Hello" } }, :md5)
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }

iex> Resx.Resource.hash(%Resx.Resource{ reference: %Resx.Resource.Reference{ integrity: %Resx.Resource.Reference.Integrity{ checksum: { :foo, 1 }, timestamp: 0 }, adapter: nil, repository: nil }, content: %Resx.Resource.Content{ type: ["text/plain"], data: "Hello" } }, :foo)
{ :foo, 1 }

iex> Resx.Resource.hash(%Resx.Resource{ reference: %Resx.Resource.Reference{ integrity: %Resx.Resource.Reference.Integrity{ checksum: { :foo, 1 }, timestamp: 0 }, adapter: nil, repository: nil }, content: %Resx.Resource.Content{ type: ["text/plain"], data: "Hello" } }, :md5)
{ :md5, <<139, 26, 153, 83, 196, 97, 18, 150, 168, 39, 171, 248, 196, 120, 4, 215>> }
Link to this function kind?(resource, behaviour)
kind?(t() | Resx.ref(), module()) :: boolean()

Check what kind of reference this resource or resource reference is.

iex> Resx.Resource.open!("data:,foo") |> Resx.Resource.kind?(Resx.Producer)
true

iex> Resx.Resource.kind?("data:,foo", Resx.Producer)
true

iex> Resx.Resource.kind?("data:,foo", Resx.Producers.Data)
true

iex> Resx.Resource.kind?("data:,foo", Resx.Storer)
false

iex> Resx.Resource.kind?("data:,foo", Resx.Transformer)
false
Link to this function open!(resource, opts \\ [])

Open a resource from a pre-existing resource or a resource reference.

Raises a Resx.Resource.OpenError if the resource could not be opened.

For more details see Resx.Resource.open/2.

Open a resource from a pre-existing resource or a resource reference.

Link to this function source(resource)
source(t() | Resx.ref()) ::
  {:ok, Resx.ref() | nil} | Resx.error(Resx.reference_error())

Get the source of the current resource or resource reference.

It will return { :ok, nil } if there is no source.

Link to this function store!(resource, storer, opts \\ [])
store!(t() | Resx.ref(), module(), keyword()) :: t() | no_return()

Store the resource.

If a resource reference is given, a stream will be opened to that resource.

Raises a Resx.Storer.StoreError if the resource cannot be saved, or a Resx.Resource.OpenError if the resource could not be opened.

For more details see Resx.Storer.save!/2.

Link to this function store(resource, storer, opts \\ [])
store(t() | Resx.ref(), module(), keyword()) ::
  {:ok, t()} | Resx.error(Resx.resource_error() | Resx.reference_error())

Store the resource.

If a resource reference is given, a stream will be opened to that resource.

For more details see Resx.Storer.save/2.

Link to this function stream!(resource, opts \\ [])

Stream a resource from a pre-existing resource or a resource reference.

Raises a Resx.Resource.OpenError if the resource could not be streamed.

For more details see Resx.Resource.stream/2.

Stream a resource from a pre-existing resource or a resource reference.

Link to this function transform!(resource, transformer, opts \\ [])
transform!(t() | Resx.ref(), module(), keyword()) :: t() | no_return()

Transform the resource.

If a resource reference is given, a stream will be opened to that resource.

Raises a Resx.Transformer.TransformError if the transformation cannot be applied, or a Resx.Resource.OpenError if the resource could not be opened.

For more details see Resx.Transformer.apply!/2.

Link to this function transform(resource, transformer, opts \\ [])
transform(t() | Resx.ref(), module(), keyword()) ::
  {:ok, t()} | Resx.error(Resx.resource_error() | Resx.reference_error())

Transform the resource.

If a resource reference is given, a stream will be opened to that resource.

For more details see Resx.Transformer.apply/2.

Retrieve the URI for a resource or resource reference.