The snap-predicates package

[Tags: library]

Provides a definition of a predicate type-class together with several concrete implementations which are used to constrain the set of possible Snap handlers in a type-safe way.


[Skip to ReadMe]

Properties

Versions0.1.0, 0.2.0, 0.3.0, 0.3.1
Change logNone available
Dependenciesbase (==4.*), bytestring (>=0.9), case-insensitive (>=1.0), containers (>=0.5), snap-core (>=0.9), transformers (>=0.3) [details]
LicenseMIT
CopyrightCopyright (c) 2013 Toralf Wittner, Brendan Hay
AuthorToralf Wittner, Brendan Hay
MaintainerToralf Wittner <tw@dtex.org>
Stabilityexperimental
CategorySnap
Source repositoryhead: git clone https://github.com/twittner/snap-predicates
UploadedWed Mar 13 20:50:34 UTC 2013 by ToralfWittner
DistributionsNixOS:0.3.1
Downloads543 total (27 in last 30 days)
Votes
0 []
StatusDocs uploaded by user
Build status unknown [no reports yet]

Modules

[Index]

Downloads

Maintainers' corner

For package maintainers and hackage trustees

Readme for snap-predicates-0.1.0

Snap-Predicates

This library provides a definition of a type-class Predicate together with several concrete implementations which are used to constrain the set of possible Snap handlers in a type-safe way.

Example

{-# LANGUAGE OverloadedStrings, TypeOperators #-}
module Main where

import Data.ByteString (ByteString)
import Data.Monoid
import Data.Predicate
import Snap.Core
import Snap.Routes
import Snap.Predicates
import Snap.Http.Server

main :: IO ()
main = do
    mapM_ putStrLn (showRoutes sitemap)
    quickHttpServe (route . expandRoutes $ sitemap)

sitemap :: Routes Snap ()
sitemap = do
    get "/a" getUser $
        AcceptJson :&: (Param "name" :|: Param "nick") :&: Param "foo"

    get "/b" getUser' $
        AcceptJson :&: (Param "name" :||: Param "nick") :&: Param "foo"

    get  "/status" status      $ Fail (410, Just "Gone.")
    post "/"       createUser  $ AcceptThrift
    post "/"       createUser' $ AcceptJson

getUser :: AcceptJson :*: ByteString :*: ByteString -> Snap ()
getUser (_ :*: name :*: foo) =
    writeBS $ "getUser: name or nick=" <> name <> " foo=" <> foo

getUser' :: AcceptJson :*: (ByteString :+: ByteString) :*: ByteString -> Snap ()
getUser' (_ :*: name :*: foo) =
    case name of
        Left  a -> writeBS $ "getUser: name=" <> a <> " foo=" <> foo
        Right b -> writeBS $ "getUser: nick=" <> b <> " foo=" <> foo

createUser :: AcceptThrift -> Snap ()
createUser _ = writeBS "createUser"

createUser' :: AcceptJson -> Snap ()
createUser' _ = writeBS "createUser'"

status :: AcceptJson :*: Char -> Snap ()
status _ = writeBS "status"