Skip to content

Instantly share code, notes, and snippets.

@dpwiz
Created November 1, 2012 09:19
Show Gist options
  • Save dpwiz/3992670 to your computer and use it in GitHub Desktop.
Save dpwiz/3992670 to your computer and use it in GitHub Desktop.
-- * Stuff
import Database.SQL (connectDB, dbquery)
import Database.Cache (cacheGet, cacheSet)
import Network.SOAP (soap)
import Control.Monad (guard)
type Oven = String
type Product = String
type HaveGas = Bool
data Recipe = Recipe { getOven :: Oven
, getProduct :: Product
, executeWith :: HaveGas -> Maybe Product
}
haveOven :: Oven -> IO Bool
haveOven oven = (elem oven . lines) `fmap` readFile "ovens.txt"
haveProduct :: Product -> IO Bool
haveProduct prod = connectDB >>= ((> 0) . length) `fmap` dbquery "SELECT 1 FROM products WHERE alias=%s" [prod]
gasReady :: IO HaveGas
gasReady = cached "gas" 10 $ soap "GasProvider" "gasReady"
create :: Recipe -> IO (Maybe Product)
create recipe = do
haveOven (getOven recipe) >>= guard
haveProduct (getProduct recipe) >>= guard
gas <- gasReady
return $ recipe `executeWith` gas
-- * Helpers
cached :: String -> Int -> IO a -> a
cached key time refresh = do
cached <- cacheGet key
case cached of
Just value -> return value
Nothing -> refresh >>= cacheSet key time >>= return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment