Skip to content

Instantly share code, notes, and snippets.

@pfitz
Created September 29, 2016 07:13
Show Gist options
  • Save pfitz/1e17b48c6b8d34ff39f748f184b30058 to your computer and use it in GitHub Desktop.
Save pfitz/1e17b48c6b8d34ff39f748f184b30058 to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Html exposing (..)
import Json.Decode exposing (int, string, Decoder)
import String
import List
import Array exposing (..)
import Date
import Json.Decode.Pipeline exposing (decode, required, requiredAt)
type alias Availability =
{ date : Date.Date, availabiltyCount : Int, minStays : Int, maxStays : Int, arrivalPossible : Bool, departurePossible : Bool }
type alias ProductAvailability =
{ av : List Availability, productId : String }
type alias AvailabilityCalendar =
{ productList : ProductAvailability }
avDecoder =
Json.Decode.map
(\list ->
List.map (\item -> availabilityFromString (String.split "," item)) list
)
(Json.Decode.list Json.Decode.string)
value =
Json.Decode.decodeString availabilityCalendarDecoder str2
availabilityCalendarDecoder : Decoder AvailabilityCalendar
availabilityCalendarDecoder =
decode AvailabilityCalendar
|> required "AvailabilityCalendar" (Json.Decode.list productAvailabilityDecoder)
productAvailabilityDecoder : Decoder ProductAvailability
productAvailabilityDecoder =
decode ProductAvailability
|> required "AV" avDecoder
|> required "_ProductId" string
str2 : String
str2 =
"""
{"AvailabilityCalendar": [
{
"AV": [
"2016-09-30,0,2,999,1,1,1",
"2016-10-01,0,2,999,1,0,1",
"2016-10-02,0,1,999,1,1,1",
"2016-10-03,0,1,999,1,1,1",
"2016-10-04,0,1,999,1,1,1",
"2016-10-05,0,1,999,1,1,1",
"2016-10-06,0,1,999,1,1,1",
"2016-10-07,0,2,999,1,1,1",
"2016-10-08,0,2,999,1,0,1",
"2016-10-09,0,1,999,1,1,1",
"2016-10-10,0,1,999,1,1,1",
"2016-10-11,0,1,999,1,1,1",
"2016-10-12,0,1,999,1,1,1",
"2016-10-13,0,1,999,1,1,1",
"2016-10-14,0,2,999,1,1,1",
"2016-10-15,0,2,999,1,0,1",
"2016-10-16,0,1,999,1,1,1",
"2016-10-17,0,1,999,1,1,1",
"2016-10-18,0,1,999,1,1,1",
"2016-10-19,0,1,999,1,1,1",
"2016-10-20,0,1,999,1,1,1",
"2016-10-21,0,2,999,1,1,1",
"2016-10-22,0,2,999,1,0,1",
"2016-10-23,0,1,999,1,1,1",
"2016-10-24,0,1,999,1,1,1",
"2016-10-25,0,1,999,1,1,1",
"2016-10-26,0,1,999,1,1,1",
"2016-10-27,0,1,999,1,1,1",
"2016-10-28,0,2,999,1,1,1",
"2016-10-29,0,2,999,1,0,1",
"2016-10-30,0,1,999,1,1,1",
"2016-10-31,0,1,999,1,1,1"
],
"_ProductId": "2f4f90e7-993c-4fd4-97d9-22c4933a6e94"
}
]
}
"""
main =
text <| toString value
availabilityFromString : List String -> Availability
availabilityFromString avList =
let
avArray =
Array.fromList avList
date =
Result.withDefault (Date.fromTime 0) (Date.fromString (Maybe.withDefault "unknown" (Array.get 0 avArray)))
avlCount =
Result.withDefault 0 (String.toInt (Maybe.withDefault "0" (Array.get 1 avArray)))
minStays =
Result.withDefault 0 (String.toInt (Maybe.withDefault "0" (Array.get 2 avArray)))
maxStays =
Result.withDefault 0 (String.toInt (Maybe.withDefault "0" (Array.get 3 avArray)))
arrPossible =
stringToBool (Maybe.withDefault "0" (Array.get 4 avArray))
depPossible =
stringToBool (Maybe.withDefault "0" (Array.get 5 avArray))
in
{ date = date
, availabiltyCount = avlCount
, minStays = minStays
, maxStays = maxStays
, arrivalPossible = arrPossible
, departurePossible = depPossible
}
stringToBool : String -> Bool
stringToBool string =
case string of
"1" ->
True
_ ->
False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment