driebit/elm-ginger/1.0.1

NOTE: the latest version is 5.0.1

Ginger.Resource

Definitions

type alias Resource a =
{ a
| id : ResourceId
, title : Translation
, body : Translation
, subtitle : Translation
, summary : Translation
, path : String
, category : NonEmpty Category
, properties : Value
, publicationDate : Maybe Posix
, media : Media
, blocks : List Block
}

An Elm representation of a Ginger resource.

Note the a in the record definition, this an extensible record. This means it includes at least all of these fields but may have others as well. This lets us reason about whether the edges are included in the data, compile time. The Ginger REST API includes edges nested only one level deep, but since the edges are also resources we can re-use this datatype like Resource {}. This tells use there are no other fields besides the ones here.

So you'll see this used in function signatures like:

Resource WithEdges -- has edges


Resource {} -- does not have the edges


Resource a -- might have them but the code that's using this doesn't really care

Note: the Resource {} might actually have edges, they are just not fetched.

type alias WithEdges =
{ edges : List Edge }

The record we use to extend Resource a.

You can render a list of resource depictions like so:

viewDepictions : Resource WithEdges -> List (Html msg)
viewDepictions resource =
    List.map viewImage <|
        depictions Media.Medium resource

This next example won't compile because you need a Resource WithEdges and this signature indicates they are missing.

viewDepictions : Resource {} -> List (Html msg)
viewDepictions resource =
    List.map viewImage <|
        depictions Media.Medium resource
type alias Edge =
{ predicate : Predicate
, resource : Resource {}
}

A connection to a resource named by Predicate

type alias Block =
{ body : Translation
, name : String
, type_ : BlockType
, relatedRscId : Maybe ResourceId
, properties : Value
}
= Text
| Header
| Page
| Custom String

Access data

category : Resource a -> Category

The category of a Resource.

Every resource has one category, but can be part of a hierarchy of other categories. For example news is part of text > article > news. This function will always return the Category stored with the Resource.

Note: there hasn't been the need to expose any of the parent categories so far, if there is please file an issue.

depiction : MediaClass -> Resource WithEdges -> Maybe String

The image url of the Resource depiction.

Returns the image url if there is a depiction and the mediaclass exists.

depictions : MediaClass -> Resource WithEdges -> List String

The image urls of the Resource depictions

Returns a list of image urls if there is a depiction and the mediaclass exists.

edgesWithPredicate : Predicate -> Resource WithEdges -> List (Resource {})

Return all edges with a given predicate.

The returned resources won't have any edges themselves, indicated by the {} in Resource {}.

Decode

fromJsonWithEdges : Decoder (Resource WithEdges)

Decode a Resource that has edges.

fromJsonWithoutEdges : Decoder (Resource {})

Decode a Resource that does not have edges.