This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

CoinMarketCap.Models

Contains model definitions for the response from the coinmarket cap api

Models

type alias Crypto = { id : String , name : String , symbol : String , rank : String , price_usd : Maybe String , price_btc : Maybe String , volume_24h_usd : Maybe String , market_cap_usd : Maybe String , available_supply : Maybe String , total_supply : Maybe String , max_supply : Maybe String , percent_change_1h : Maybe String , percent_change_24h : Maybe String , percent_change_7d : Maybe String , last_updated : Maybe String }

A single record for a cryptocurrency

emptyCrypto : Crypto

Returns an empty crypto record

JSON

decodeCrypto : Json.Decode.Decoder Crypto

Decodes JSON string into Crypto record

decodeCryptos : Json.Decode.Decoder (List Crypto)

Decodes a list of Crypto from JSON

encodeCrypto : Crypto -> Json.Encode.Value

Encode a single Crypto record into JSON

module CoinMarketCap.Models exposing (..)

{-| Contains model definitions for the response from the coinmarket cap api

# Models
@docs Crypto, emptyCrypto

# JSON
@docs decodeCrypto, decodeCryptos, encodeCrypto
-}

import Json.Encode
import Json.Encode.Extra
import Json.Decode
import Json.Decode.Pipeline

{-| A single record for a cryptocurrency
-}
type alias Crypto =
    { id : String
    , name : String
    , symbol : String
    , rank : String
    , price_usd : Maybe String
    , price_btc : Maybe String
    , volume_24h_usd : Maybe String
    , market_cap_usd : Maybe String
    , available_supply : Maybe String
    , total_supply : Maybe String
    , max_supply : Maybe String
    , percent_change_1h : Maybe String
    , percent_change_24h : Maybe String
    , percent_change_7d : Maybe String
    , last_updated : Maybe String
    }

{-| Decodes JSON string into Crypto record
-}
decodeCrypto : Json.Decode.Decoder Crypto
decodeCrypto =
    Json.Decode.Pipeline.decode Crypto
        |> Json.Decode.Pipeline.required "id" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "symbol" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "rank" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "price_usd" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "price_btc" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "24h_volume_usd" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "market_cap_usd" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "available_supply" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "total_supply" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "max_supply" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "percent_change_1h" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "percent_change_24h" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "percent_change_7d" (Json.Decode.nullable Json.Decode.string)
        |> Json.Decode.Pipeline.required "last_updated" (Json.Decode.nullable Json.Decode.string)


{-| Decodes a list of Crypto from JSON
-}
decodeCryptos : Json.Decode.Decoder (List Crypto)
decodeCryptos =
    Json.Decode.list decodeCrypto

{-| Encode a single Crypto record into JSON
-}
encodeCrypto : Crypto -> Json.Encode.Value
encodeCrypto record =
    Json.Encode.object
        [ ( "id", Json.Encode.string <| record.id )
        , ( "name", Json.Encode.string <| record.name )
        , ( "symbol", Json.Encode.string <| record.symbol )
        , ( "rank", Json.Encode.string <| record.rank )
        , ( "price_usd", Json.Encode.Extra.maybe Json.Encode.string record.price_usd )
        , ( "price_btc", Json.Encode.Extra.maybe Json.Encode.string record.price_btc )
        , ( "24h_volume_usd", Json.Encode.Extra.maybe Json.Encode.string record.volume_24h_usd )
        , ( "market_cap_usd", Json.Encode.Extra.maybe Json.Encode.string record.market_cap_usd )
        , ( "available_supply", Json.Encode.Extra.maybe Json.Encode.string record.available_supply )
        , ( "total_supply", Json.Encode.Extra.maybe Json.Encode.string record.total_supply )
        , ( "max_supply", Json.Encode.Extra.maybe Json.Encode.string record.max_supply )
        , ( "percent_change_1h", Json.Encode.Extra.maybe Json.Encode.string record.percent_change_1h )
        , ( "percent_change_24h", Json.Encode.Extra.maybe Json.Encode.string record.percent_change_24h )
        , ( "percent_change_7d", Json.Encode.Extra.maybe Json.Encode.string record.percent_change_7d )
        , ( "last_updated", Json.Encode.Extra.maybe Json.Encode.string record.last_updated )
        ]

{-| Returns an empty crypto record
-}
emptyCrypto : Crypto
emptyCrypto =
    { id = ""
    , name = ""
    , symbol = ""
    , rank = ""
    , price_usd = Just ""
    , price_btc = Just ""
    , volume_24h_usd = Just ""
    , market_cap_usd = Just ""
    , available_supply = Just ""
    , total_supply = Just ""
    , max_supply = Just ""
    , percent_change_1h = Just ""
    , percent_change_24h = Just ""
    , percent_change_7d = Just ""
    , last_updated = Just ""
    }