Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linsen fixes once again #138

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Model/Linsen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ where
import Control.Monad ( (>=>)
, (<=<)
, zipWithM
, filterM
, ap )
import Control.Monad.Catch ( MonadThrow )
import Control.Monad.IO.Class ( MonadIO )
Expand All @@ -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
Expand All @@ -36,6 +39,7 @@ import Network.HTTP.Req
import Model.Types ( NoMenu(..)
, Menu(..)
, Restaurant ( Restaurant ) )
import Prelude hiding ( all )
import Util ( menusToEitherNoLunch )

swedishTimeLocale :: TimeLocale
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
Loading