diff --git a/src/Model/Linsen.hs b/src/Model/Linsen.hs index 9f06a6d..e0797f6 100644 --- a/src/Model/Linsen.hs +++ b/src/Model/Linsen.hs @@ -11,6 +11,7 @@ where import Control.Monad ( (>=>) , (<=<) , zipWithM + , filterM , ap ) import Control.Monad.Catch ( MonadThrow ) import Control.Monad.IO.Class ( MonadIO ) @@ -21,9 +22,11 @@ import Data.Aeson.Types ( Parser , parseEither ) import Data.Bifunctor ( first ) import qualified Data.ByteString.Lazy.Char8 as BL8 +import Data.Char ( isSpace ) import Data.Functor ( (<&>) ) import Data.List.Extra ( (!?) ) import Data.Text.Lazy ( Text + , all , replace , strip ) import Data.Thyme ( parseTime @@ -36,6 +39,7 @@ import Network.HTTP.Req import Model.Types ( NoMenu(..) , Menu(..) , Restaurant ( Restaurant ) ) +import Prelude hiding ( all ) import Util ( menusToEitherNoLunch ) swedishTimeLocale :: TimeLocale @@ -72,9 +76,9 @@ swedishTimeLocale = TimeLocale } pattern MeatDish, FishDish, VegDish :: Integer -pattern MeatDish = 3 -pattern FishDish = 8 -pattern VegDish = 13 +pattern MeatDish = 2 +pattern FishDish = 6 +pattern VegDish = 10 fetch :: (MonadHttp m, MonadIO m, MonadThrow m) @@ -107,6 +111,13 @@ parse day = >=> (.: "richText") >=> (.: "root") >=> (.: "children") + >=> filterM (withObject "filter whitespace" + $ (.: "children") + >=> \case + [] -> fail "Empty list" + (v:_) -> pure v + >=> (.: "text") + >=> (pure . not . all isSpace)) >=> (\v' -> (case v' !? 1 of Nothing -> fail "failed to index into food" diff --git a/test/Main.hs b/test/Main.hs index 9cfdf7d..c95b7da 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -105,7 +105,7 @@ main = hspec $ do "parses a blob of JSON without error, that has no lunch" (do s2 <- BL.readFile "test/linsen2.json" -- Test that has no lunch - testFun (Left (NMParseError "Error in $: Unable to parse day" undefined)) + testFun (Left (NMParseError "Error in $: Empty list" undefined)) (L.parse (fromGregorian 2024 06 06) (fromJust $ decode s2)) @@ -116,7 +116,7 @@ main = hspec $ do "parses a blob of JSON without error, that has the wrong week" (do s3 <- BL.readFile "test/linsen3.json" - testFun (Left (NMParseError "Error in $: Unable to parse day" undefined)) + testFun (Left (NMParseError "Error in $: Empty list" undefined)) (L.parse (fromGregorian 2024 06 26) (fromJust $ decode s3))