-
Notifications
You must be signed in to change notification settings - Fork 9
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, you have arrived #126
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
02128e7
Started on linsen
The1Penguin f25bd67
Now fetches first part of the food
The1Penguin a3c9089
Now works
The1Penguin d9d1f6d
Mjahaja
The1Penguin f99f05e
Return the cabal
The1Penguin f4818cc
We have gathered here today
The1Penguin 30700ea
Added test and now strips whitespace
The1Penguin 6bfd82e
Based on some comments of Ekeroot
The1Penguin 5b4e3c7
In case the index of food doesn't work
The1Penguin b6fa09e
Fix one issue
The1Penguin 92d28ab
Now shouldn't produce errors
The1Penguin 8feb0a9
More tests
The1Penguin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
{-# LANGUAGE FlexibleContexts, OverloadedStrings, LambdaCase #-} | ||
|
||
module Model.Linsen | ||
( | ||
parse | ||
, fetchAndCreateLinsen | ||
) | ||
where | ||
|
||
import Control.Monad ( (>=>) | ||
, (<=<) | ||
, zipWithM | ||
, ap | ||
) | ||
import Control.Monad.Catch ( MonadThrow ) | ||
import Control.Monad.IO.Class ( MonadIO ) | ||
import Data.Aeson ( (.:) | ||
, withObject | ||
, Value | ||
) | ||
import Data.Aeson.Types ( Parser | ||
, 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.Calendar ( Day ) | ||
import Lens.Micro.Platform ( (^.) ) | ||
import Network.HTTP.Req | ||
import Model.Types ( NoMenu(..) | ||
, Menu(..) | ||
, Restaurant | ||
( Restaurant | ||
) | ||
) | ||
import Util ( menusToEitherNoLunch ) | ||
import Data.Thyme.Calendar.WeekDate ( weekDate | ||
, _wdDay) | ||
|
||
fetch | ||
:: (MonadHttp m, MonadIO m, MonadThrow m) | ||
=> m Value -- ^ A JSON response or horrible crash | ||
fetch = | ||
req | ||
GET | ||
(https "cafe-linsen.se" /: "api" /: "menu") | ||
NoReqBody | ||
jsonResponse | ||
mempty | ||
<&> responseBody | ||
|
||
parse | ||
:: Day -- ^ Day to parse | ||
-> Value -- ^ JSON result from `fetch` | ||
-> Either NoMenu [Menu] -- ^ Either list of parsed `Menu`s or `NoMenu` error | ||
parse day = | ||
failWithNoMenu | ||
(parseEither | ||
(let index = 6 - day ^. weekDate . _wdDay in | ||
if index `notElem` [1..5] then | ||
pure . const [] | ||
else | ||
withObject "Parse meals" | ||
$ (.: "docs") | ||
>=> (\case | ||
Nothing -> fail "Failed to index into days" | ||
Just v -> pure v) . (!? index) | ||
>=> (.: "richText") | ||
>=> (.: "root") | ||
>=> (.: "children") | ||
>=> menuParser . (\v' -> if length v' >= 9 then v' else mempty) | ||
) | ||
) | ||
>=> menusToEitherNoLunch | ||
where | ||
failWithNoMenu :: Show a => (a -> Either String b) -> a -> Either NoMenu b | ||
failWithNoMenu action x = | ||
first (\msg -> NMParseError msg . BL8.pack . show $ x) (action x) | ||
|
||
menuParser :: [Value] -> Parser [Menu] | ||
menuParser = pure . (zip [0 :: Integer ..] >=> \case | ||
(2 ,vs) -> [vs] -- Index of Meat dish | ||
(6 ,vs) -> [vs] -- Index of Fish dish | ||
(10,vs) -> [vs] -- Index of Veg dish | ||
_ -> []) | ||
<=< ap (zipWithM sumFood) tail | ||
|
||
sumFood :: Value -> Value -> Parser Menu | ||
sumFood a b = Menu <$> getFood a <*> getFood b | ||
|
||
getFood :: Value -> Parser Text | ||
getFood = withObject "Menu Object" | ||
$ (.: "children") | ||
>=> \case | ||
[] -> pure mempty | ||
vs -> strip . replace "/ " ", " | ||
<$> last vs .: "text" | ||
|
||
fetchAndCreateLinsen | ||
:: (MonadHttp m, MonadIO m, MonadThrow m) | ||
=> Day -- ^ Day | ||
-> m Restaurant -- ^ Fetched Restaurant | ||
fetchAndCreateLinsen day = | ||
Restaurant | ||
"Café Linsen" | ||
"https://plateimpact-screen.azurewebsites.net/menu/week/" | ||
<$> fmap (parse day) fetch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
import qualified Data.ByteString.Lazy as BL | ||
import qualified Data.ByteString.Lazy.Char8 as BL8 | ||
import qualified Data.Text.Lazy as T | ||
import Data.Aeson ( decode ) | ||
import Data.Maybe ( fromJust ) | ||
import Data.Thyme.Time.Core ( fromGregorian ) | ||
import Model.Karen ( parse ) | ||
import qualified Model.Linsen as L ( parse ) | ||
import Model.Types ( Menu(..) | ||
, NoMenu | ||
, NoMenu( NoLunch ) | ||
) | ||
import Model.Wijkanders ( getWijkanders | ||
, hasDate | ||
|
@@ -19,10 +21,16 @@ import Test.HUnit ( (@?=) | |
, assertFailure | ||
) | ||
|
||
testFun :: [Menu] -> Either NoMenu [Menu] -> IO () | ||
testFun expected = either | ||
(assertFailure . mappend "This is not expected, input was:\n" . show) | ||
(@?= expected) | ||
testFun :: Either NoMenu [Menu] -> Either NoMenu [Menu] -> IO () | ||
testFun = \case | ||
Right e -> | ||
either | ||
(assertFailure . mappend "This is not expected, input was:\n" . show) | ||
(@?= e) | ||
Left e -> | ||
either | ||
(@?= e) | ||
(assertFailure . mappend "This is not expected, input was:\n" . show) | ||
|
||
main :: IO () | ||
main = hspec $ do | ||
|
@@ -31,7 +39,7 @@ main = hspec $ do | |
describe "The Karen Express" $ it | ||
"parses a blob of JSON without error" | ||
( testFun | ||
[ Menu | ||
(Right [ Menu | ||
(T.pack "Street food") | ||
(T.pack "Chicken africana, banan, mango raja, ris") | ||
, Menu | ||
|
@@ -40,20 +48,44 @@ main = hspec $ do | |
, Menu | ||
(T.pack "Nordic") | ||
(T.pack "F\228rskost bakad sej, vitvinss\229s, broccoli, potatis") | ||
] | ||
]) | ||
$ parse | ||
"Swedish" | ||
(fromJust . decode $ BL8.pack | ||
"{\"data\":{\"dishOccurrencesByTimeRange\":[{\"displayNames\":[{\"name\":\"Chicken africana, banan, mango raja, ris\",\"categoryName\":\"Swedish\"},{\"name\":\"Chicken africana, banana, mango raja, rice\",\"categoryName\":\"English\"}],\"startDate\":\"09/25/2023 00:00:00\",\"dishType\":{\"name\":\"Street food\"},\"dish\":{\"name\":\"Kyckling, het paprikas\195\165s & ris\"}},{\"displayNames\":[{\"name\":\"Indian linseed stew, zucchini, aubergine, ginger, coriander\",\"categoryName\":\"English\"},{\"name\":\"Indisklinsgryta, zucchini, aubergin, ingef\195\164ra, koriander, ris\",\"categoryName\":\"Swedish\"}],\"startDate\":\"09/25/2023 00:00:00\",\"dishType\":{\"name\":\"Greens\"},\"dish\":{\"name\":\"Vegan, pasta, linsbolognese\"}},{\"displayNames\":[{\"name\":\"F\195\164rskost bakad sej, vitvinss\195\165s, broccoli, potatis\",\"categoryName\":\"Swedish\"},{\"name\":\"Cream cheese baked saithe, whitewine sauce, broccoli, potatoes\",\"categoryName\":\"English\"}],\"startDate\":\"09/25/2023 00:00:00\",\"dishType\":{\"name\":\"Nordic\"},\"dish\":{\"name\":\"Bakad fisk, vitvinss\195\165s, potatispur\195\169\"}}]}}\n" | ||
) | ||
) | ||
|
||
describe "Cafe Linsen" $ it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests <3 |
||
"parses two blob of JSON without error" | ||
(do | ||
s1 <- BL.readFile "test/linsen1.json" | ||
testFun | ||
(Right [ Menu | ||
(T.pack "Natt Överbakad Högrev.") | ||
(T.pack "Rotfrukter, Timjansky, Persilja, Pommes Chateau.") | ||
, Menu | ||
(T.pack "Stekt Fisk.") | ||
(T.pack "Remouladsås, Citron, Dill, Picklade Morötter, Rostad Potatis.") | ||
, Menu | ||
(T.pack "Chana Masala.") | ||
(T.pack "Kikärtor, Grönsaker, Potatis Pakora, Nannbröd, Ris") | ||
]) (L.parse | ||
(fromGregorian 2024 05 31) | ||
(fromJust $ decode s1)) | ||
s2 <- BL.readFile "test/linsen2.json" -- Test that has no lunch | ||
testFun (Left NoLunch) | ||
(L.parse | ||
(fromGregorian 2024 06 06) | ||
(fromJust $ decode s2)) | ||
) | ||
|
||
describe "The Wijkander's" | ||
$ it "Parses two blobs of HTML correctly on fridays" | ||
$ do | ||
s1 <- BL.readFile "test/190517 wijkanders.html" | ||
testFun | ||
[ Menu | ||
(Right [ Menu | ||
(T.pack "Fisk") | ||
(T.pack | ||
"Havets Wallenbergare, kallpressad rapsolja, ärtor, dill & potatismos" | ||
|
@@ -63,11 +95,11 @@ main = hspec $ do | |
(T.pack | ||
"Helstekt kotlettred, potatisgratäng, skysås & örtbakad tomat" | ||
) | ||
] | ||
]) | ||
(getWijkanders (fromGregorian 2019 05 17) s1) | ||
s2 <- BL.readFile "test/190913 wijkanders.html" | ||
testFun | ||
[ Menu | ||
(Right [ Menu | ||
(T.pack "Vegetarisk ") | ||
(T.pack | ||
"Pasta, svamp, grädde, citron, grana padano & rotfruktschips" | ||
|
@@ -80,5 +112,5 @@ main = hspec $ do | |
(T.pack | ||
"Helstekt kotlettrad, rostad potatis, svampsås & inlagd gurka" | ||
) | ||
] | ||
]) | ||
(getWijkanders (fromGregorian 2019 09 13) s2) |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<&>
<- pretty!