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
Store the resource
Store the resource
Stream a resource from a pre-existing resource or a resource reference
Stream a resource from a pre-existing resource or a resource reference
Transform the resource
Transform the resource
Retrieve the URI for a resource or resource reference
Link to this section Types
content() :: Resx.Resource.Content.t() | Resx.Resource.Content.Stream.t()
hasher() :: {Resx.Resource.Reference.Integrity.algo(), Callback.callback(binary(), any())}
streamable_hasher() :: {Resx.Resource.Reference.Integrity.algo(), initializer :: Callback.callback(Resx.Resource.Reference.Integrity.algo(), hash_state()), updater :: Callback.callback(hash_state(), binary(), hash_state()), finaliser :: Callback.callback(hash_state(), any())}
t(content) :: %Resx.Resource{ content: content, meta: keyword(), reference: Resx.Resource.Reference.t() }
Link to this section Functions
Check if two resources or resource references point to the same resource.
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.
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.
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.
discard(t() | Resx.ref(), keyword()) :: :ok | Resx.error(Resx.resource_error() | Resx.reference_error())
Discard the resource.
Only resources or resource references that implement the Resx.Storer
behaviour should be passed to this.
exists?(t() | Resx.ref()) :: {:ok, boolean()} | Resx.error(Resx.reference_error())
Check whether a resource or resource reference exists.
hash(t() | content()) :: Resx.Resource.Reference.Integrity.checksum()
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.
hash( t() | content(), Resx.Resource.Reference.Integrity.algo() | hasher() | streamable_hasher() ) :: Resx.Resource.Reference.Integrity.checksum()
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>> }
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
open!(t() | Resx.ref(), keyword()) :: Resx.Resource.t(Resx.Resource.Content.t()) | no_return()
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(t() | Resx.ref(), keyword()) :: {:ok, Resx.Resource.t(Resx.Resource.Content.t())} | Resx.error(Resx.resource_error() | Resx.reference_error())
Open a resource from a pre-existing resource or a resource reference.
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.
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
.
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
.
stream!(t() | Resx.ref(), keyword()) :: Resx.Resource.t(Resx.Resource.Content.Stream.t()) | no_return()
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(t() | Resx.ref(), keyword()) :: {:ok, Resx.Resource.t(Resx.Resource.Content.Stream.t())} | Resx.error(Resx.resource_error() | Resx.reference_error())
Stream a resource from a pre-existing resource or a resource reference.
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
.
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
.
uri(t() | Resx.Resource.Reference.t()) :: {:ok, Resx.uri()} | Resx.error(Resx.resource_error() | Resx.reference_error())
Retrieve the URI for a resource or resource reference.