# `Smee.Entity`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L1)

`Smee` wraps up metadata for individual entities in %Entity{} structs, and the `Smee.Entity` module contains
  functions that may be useful when working with them.

Many of the functions mirror those in the `Smee.Metadata` module - the same actions but on a smaller unit of XML.

Like %Metadata{} structs the XML in entities can be compressed and decompressed, but unlike Metadata structs they have
  parsed xmerl data in them by default too.

Wherever possible use `Entity.update/2` to make changes, do not write to the Entity struct directly. If you must write directly
  you can use `Entity.update/1` to resync the state of the record.

# `t`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L25)

```elixir
@type t() :: %Smee.Entity{
  bilateral: boolean(),
  changes: integer(),
  compressed: boolean(),
  data: nil | binary(),
  data_hash: nil | binary(),
  downloaded_at: nil | DateTime.t(),
  fedid: nil | binary(),
  id: nil | binary(),
  label: nil | binary(),
  local: boolean(),
  metadata_uri: nil | binary(),
  metadata_uri_hash: nil | binary(),
  modified_at: nil | DateTime.t(),
  priority: integer(),
  size: integer(),
  tags: [binary()],
  trustiness: float(),
  uri: nil | binary(),
  uri_hash: nil | binary(),
  valid_until: nil | DateTime.t(),
  xdoc: nil | tuple()
}
```

# `assurance`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L523)

```elixir
@spec assurance(entity :: t()) :: list()
```

Returns the assurance certification URIs of the entity as a list

The most common and useful assurance profile is almost certainly SIRTFI, but many others exist.

# `bulkup`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L222)

```elixir
@spec bulkup(entity :: t()) :: t()
```

  Returns an entity that contains parsed XML data, greatly increasing its size but possibly making future processing faster.

# `categories`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L498)

```elixir
@spec categories(entity :: t()) :: list()
```

Returns a list of entity categories for this entity

Entity categories are official tags indicating that a service fits certain criteria. For instance, Research and Scholarship
(indicated with the URI "http://refeds.org/category/research-and-scholarship") shows that a service will behave according to
  certain rules.

You can see a dump of all entity attributes, including entity categories, using `Smee.Extract.list_entity_attrs/1`

# `category_support`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L511)

```elixir
@spec category_support(entity :: t()) :: list()
```

Returns a list of *supported* entity categories

Category support is the counterpart of entity categories, indicating that a service will make use of those categories if
  an entity possesses them.

# `check_date!`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L406)

```elixir
@spec check_date!(entity :: t()) :: t()
```

Raises an exception if the entity has expired (based on valid_until datetime), otherwise returns the entity.

If no valid_until has been set (if it's nil) then the entity will always be returned.

# `compress`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L238)

```elixir
@spec compress(entity :: t()) :: t()
```

Returns a compressed entity, containing gzipped XML. This greatly reduces the size of the entity record.

# `compressed?`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L230)

```elixir
@spec compressed?(entity :: t()) :: boolean()
```

Returns true if the XML data in an entity has been compressed

# `decompress`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L252)

```elixir
@spec decompress(entity :: t()) :: t()
```

Returns a decompressed entity, with plain-text XML data. This makes the struct much larger.

# `derive`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L143)

```elixir
@spec derive(data :: binary(), metadata :: Smee.Metadata.t(), options :: keyword()) ::
  t()
```

Returns a new %Entity{} struct if passed XML data for an entity *and* parent %Metadata{}.

Defaults values are set using the parent metadata where possible.

You can set or override various parts of the struct by passing options:

* md_uri - a URI that identifies a parent
* downloaded_at - A DateTime to record when the record was downloaded
* modified_at - A DateTime to record when the record was updated *upstream*
* valid_until - A DateTime to indicate when an entity expires
* priority - An integer between 0 and 9 to show priority
* trustiness - a Float between 0.0 and 0.9 to indicate, well, trustiness.
* fedid - a name or tag for the publishing federation or collection
* tags - a list of arbitrary tags that you can use for sorting and marking data
* local - a boolean to indicate local ownership of the metadata (they're *your* services)
* bilateral - a boolean to indicate that the metadata is not from an external full federation

You won't normally need to do this yourself as entities can be extracted from `Smee.Metadata`.

# `expired?`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L392)

```elixir
@spec expired?(entity :: t()) :: boolean()
```

Returns true if the entity has expired (based on valid_until datetime)

If no valid_until has been set (if it's nil) then false will be returned

# `filename`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L332)

```elixir
@spec filename(entity :: t()) :: binary()
```

Returns a suggested filename for the entity.

# `filename`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L343)

```elixir
@spec filename(entity :: t(), format :: atom()) :: binary()
```

Returns a suggested filename for the entity in the specified format.

Two formats can be specified: :sha1 and :uri

# `id`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L267)

```elixir
@spec id(entity :: t()) :: binary()
```

Returns the entityID of the entity struct.

(This is the same as the URI)

# `idp?`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L296)

```elixir
@spec idp?(entity :: t()) :: boolean()
```

Returns true if the entity has an IdP role.

An entity may have more than one role.

# `new`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L90)

```elixir
@spec new(data :: binary(), options :: keyword()) :: t()
```

Returns a new %Entity{} struct if passed XML data.

You can set or override various parts of the struct by passing options:

* md_uri - a URI that identifies a parent
* downloaded_at - A DateTime to record when the record was downloaded
* modified_at - A DateTime to record when the record was updated *upstream*
* valid_until - A DateTime to indicate when an entity expires
* priority - An integer between 0 and 9 to show priority
* trustiness - a Float between 0.0 and 0.9 to indicate, well, trustiness.

You won't normally need to do this yourself as entities can be extracted from `Smee.Metadata`.

# `priority`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L375)

```elixir
@spec priority(entity :: t()) :: integer()
```

Returns the priority of the entity as a value between 0 and 9

# `registered_at`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L473)

```elixir
@spec registered_at(entity :: t()) :: Date.t() | nil
```

Returns the Date of registration, if present, or nil.

See `registration_authority/1` for more information

# `registration_authority`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L461)

```elixir
@spec registration_authority(entity :: t()) :: binary() | nil
```

Returns the URI for the entity's registration authority, if available, or nil.

Interfederation means that entities may be initially published in a different federation to the one *this* record is
  currently published in. The registration authority URI, if present, shows that the entity was officially registered
elsewhere.

# `slim`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L214)

```elixir
@spec slim(entity :: t()) :: t()
```

Returns an entity with parsed XML data removed, greatly reducing its size but possibly making future processing slower.

# `sp?`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L307)

```elixir
@spec sp?(entity :: t()) :: boolean()
```

Returns true if the entity has an SP role.

An entity may have more than one role.

# `tag`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L449)

```elixir
@spec tag(entity :: t(), tags :: list() | nil | binary()) :: t()
```

Tags an entity with one or more tags, replacing existing tags.

Tags are arbitrary classifiers, initially inherited from source and metadata

# `tags`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L439)

```elixir
@spec tags(entity :: t()) :: [binary()]
```

Returns the tags of the entity struct, a list of binary strings

Tags are arbitrary strings, which may be initially inherited from source and metadata

# `transformed_id`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L277)

```elixir
@spec transformed_id(entity :: t()) :: binary()
```

Returns the MDQ hashed style entityID of the entity struct.

(This is the same as the URI)

# `trustiness`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L360)

```elixir
@spec trustiness(entity :: t()) :: float()
```

Returns the trustiness level of the entity as a float between 0.0 and 0.9.

# `update`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L183)

```elixir
@spec update(entity :: t()) :: t()
```

Resyncs the internal state of an %Entity{} struct

If changes have been made using `Entity.update/2` then this will not be needed - it's there for when the struct
  has been changed directly

# `update`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L194)

```elixir
@spec update(entity :: t(), xml :: binary()) :: t()
```

Returns an updates %Entity{} struct with new XML, refreshing various parts of the struct correctly.

This should be the only way updated Entities are produced - the raw struct should not be changed directly.

# `validate!`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L422)

```elixir
@spec validate!(entity :: t()) :: t()
```

Raises an exception if the entity has invalid XML, otherwise returns the entity.

# `xdoc`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L286)

```elixir
@spec xdoc(entity :: t()) :: tuple()
```

Returns a parsed Erlang `xmerl` structure representing the entities XML, for use with `xmerl`, `SweetXML` and other
  tools.

# `xml`
[🔗](https://github.com/Digital-Identity-Labs/smee/blob/main/lib/smee/entity.ex#L316)

```elixir
@spec xml(entity :: t()) :: binary()
```

Returns the plain-text XML of the entity, whether or not it has been compressed.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
