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 chose to change their api D: #134

Merged
merged 4 commits into from
Sep 24, 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
54 changes: 48 additions & 6 deletions src/Model/Linsen.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleContexts, OverloadedStrings, LambdaCase #-}
{-# LANGUAGE FlexibleContexts, OverloadedStrings, LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}

module Model.Linsen
(
Expand Down Expand Up @@ -26,7 +27,7 @@ import Data.Text.Lazy ( Text
, replace
, strip )
import Data.Thyme ( parseTime
, defaultTimeLocale )
, TimeLocale (..) )
import Data.Thyme.Calendar ( Day )
import Data.Thyme.Calendar.WeekDate ( weekDate
, _wdDay )
Expand All @@ -37,6 +38,44 @@ import Model.Types ( NoMenu(..)
, Restaurant ( Restaurant ) )
import Util ( menusToEitherNoLunch )

swedishTimeLocale :: TimeLocale
swedishTimeLocale = TimeLocale
{ wDays =
[ ("Söndag", "Sön")
, ("Måndag", "Mån")
, ("Tisdag", "Tis")
, ("Onsdag", "Ons")
, ("Torsdag", "Tors")
, ("Fredag", "Fre")
, ("Lördag", "Lör")
]
, months =
[ ("Januari", "Jan")
, ("Februari", "Feb")
, ("Mars", "Mar")
, ("April", "Apr")
, ("Maj", "Maj")
, ("Juni", "Jun")
, ("Juli", "Jul")
, ("Augusti", "Aug")
, ("September", "Sep")
, ("Oktober", "Oct")
, ("November", "Nov")
, ("December", "Dec")
]
, amPm = ("fm", "em")
, dateTimeFmt = "%a %f %e %H:%M:%S %Z %Y"
, dateFmt = "%d/%m/%y"
, timeFmt = "%H:%M:%S"
, time12Fmt = "%I:%M:%S %p"
, knownTimeZones = []
}

pattern MeatDish, FishDish, VegDish :: Integer
pattern MeatDish = 3
pattern FishDish = 8
pattern VegDish = 13

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vad är oddsen att de kommer ändra dessa igen...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T-T

fetch
:: (MonadHttp m, MonadIO m, MonadThrow m)
=> m Value -- ^ A JSON response or horrible crash
Expand Down Expand Up @@ -78,7 +117,10 @@ parse day =
[] -> fail "Failed to index into richtext"
(v:_) -> pure v)
>=> (.: "text")
>=> \s -> if pure day == parseTime defaultTimeLocale "%d-%m-%Y" s && length v' >= 9
>=> \s -> if
pure day ==
parseTime swedishTimeLocale "%A %d-%m-%Y" s &&
length v' >= 9
then pure v'
else pure mempty))
>=> menuParser
Expand All @@ -92,9 +134,9 @@ parse day =

menuParser :: [Value] -> Parser [Menu]
menuParser = pure . (zip [0 :: Integer ..] >=> \case
(2 ,vs) -> [vs] -- Index of Meat dish
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic indices <3

These are hard to make less magic. :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong! :D

(6 ,vs) -> [vs] -- Index of Fish dish
(10,vs) -> [vs] -- Index of Veg dish
(MeatDish, vs) -> [vs]
(FishDish, vs) -> [vs]
(VegDish , vs) -> [vs]
_ -> [])
<=< ap (zipWithM sumFood) tail

Expand Down
14 changes: 7 additions & 7 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ main = hspec $ do
s1 <- BL.readFile "test/linsen1.json"
testFun
(Right [ Menu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the updating of the example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should eat lunch.

(T.pack "Natt Överbakad Högrev.")
(T.pack "Rotfrukter, Timjansky, Persilja, Pommes Chateau.")
(T.pack "Raggmunk.")
(T.pack "Stekt Fläsk, Lingon, Persilja.")
, Menu
(T.pack "Stekt Fisk.")
(T.pack "Remouladsås, Citron, Dill, Picklade Morötter, Rostad Potatis.")
(T.pack "Pocherad Dagens Fångst.")
(T.pack "Sandefjordsås, Brocoli, Brynt Lime, Gräslök Stekt Potatis.")
, Menu
(T.pack "Chana Masala.")
(T.pack "Kikärtor, Grönsaker, Potatis Pakora, Nannbröd, Ris")
(T.pack "Dagens Pasta.")
(T.pack "Tortelioni, Tryffelsås, Baby Spenat, Grana Padano, Roccola.")
]) (L.parse
(fromGregorian 2024 05 31)
(fromGregorian 2024 09 24)
(fromJust $ decode s1))
)

Expand Down
Loading
Loading