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

Now doesn't show food at Linsen if it is not the same week #132

Merged
merged 5 commits into from
Jul 31, 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
35 changes: 22 additions & 13 deletions src/Model/Linsen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,32 @@ where
import Control.Monad ( (>=>)
, (<=<)
, zipWithM
, ap
)
, ap )
import Control.Monad.Catch ( MonadThrow )
import Control.Monad.IO.Class ( MonadIO )
import Data.Aeson ( (.:)
, withObject
, Value
)
, Value )
import Data.Aeson.Types ( Parser
, parseEither
)
, parseEither )
import Data.Bifunctor ( first )
import qualified Data.ByteString.Lazy.Char8 as BL8
import Data.Functor ( (<&>) )
import Data.List.Extra ( (!?) )
import Data.Text.Lazy ( Text
, replace
, strip )
import Data.Thyme ( parseTime
, defaultTimeLocale )
import Data.Thyme.Calendar ( Day )
import Data.Thyme.Calendar.WeekDate ( weekDate
, _wdDay )
import Lens.Micro.Platform ( (^.) )
import Network.HTTP.Req
import Model.Types ( NoMenu(..)
, Menu(..)
, Restaurant
( Restaurant
)
)
, Restaurant ( Restaurant ) )
import Util ( menusToEitherNoLunch )
import Data.Thyme.Calendar.WeekDate ( weekDate
, _wdDay)

fetch
:: (MonadHttp m, MonadIO m, MonadThrow m)
Expand Down Expand Up @@ -72,7 +68,20 @@ parse day =
>=> (.: "richText")
>=> (.: "root")
>=> (.: "children")
>=> menuParser . (\v' -> if length v' >= 9 then v' else mempty)
>=> (\v' ->
(case v' !? 1 of
Nothing -> fail "failed to index into food"
Just v -> pure v) >>=
withObject "Parse day" (
(.: "children")
>=> (\case
[] -> fail "Failed to index into richtext"
(v:_) -> pure v)
>=> (.: "text")
>=> \s -> if pure day == parseTime defaultTimeLocale "%d-%m-%Y" s && length v' >= 9
then pure v'
else pure mempty))
>=> menuParser
)
)
>=> menusToEitherNoLunch
Expand Down
18 changes: 17 additions & 1 deletion test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ main = hspec $ do
)

describe "Cafe Linsen" $ it
"parses two blob of JSON without error"
"parses a blob of JSON without error"
(do
s1 <- BL.readFile "test/linsen1.json"
testFun
Expand All @@ -73,13 +73,29 @@ main = hspec $ do
]) (L.parse
(fromGregorian 2024 05 31)
(fromJust $ decode s1))
)

describe "Cafe Linsen" $ it
"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 NoLunch)
(L.parse
(fromGregorian 2024 06 06)
(fromJust $ decode s2))
)


describe "Cafe Linsen" $ it
"parses a blob of JSON without error, that has the wrong week"
(do
s3 <- BL.readFile "test/linsen3.json"
testFun (Left NoLunch)
(L.parse
(fromGregorian 2024 06 26)
(fromJust $ decode s3))
)

describe "The Wijkander's"
$ it "Parses two blobs of HTML correctly on fridays"
$ do
Expand Down
Loading
Loading